[Numpy-discussion] question about the documentation of linalg.solve

Joshua Lippai discerptor at gmail.com
Wed Nov 19 10:46:37 EST 2008


If you use iPython and use "numpy.linalg.solve??", you can see the
source code of the file numpy/linalg/linalg.py that corresponds to the
solve(a,b) function, not just the docstring:

def solve(a, b):
    """
    Solve the equation ``a x = b`` for ``x``.

    Parameters
    ----------
    a : array_like, shape (M, M)
        Input equation coefficients.
    b : array_like, shape (M,)
        Equation target values.

    Returns
    -------
    x : array, shape (M,)

    Raises
    ------
    LinAlgError
        If `a` is singular or not square.

    Examples
    --------
    Solve the system of equations ``3 * x0 + x1 = 9`` and ``x0 + 2 * x1 = 8``:

    >>> a = np.array([[3,1], [1,2]])
    >>> b = np.array([9,8])
    >>> x = np.linalg.solve(a, b)
    >>> x
    array([ 2.,  3.])

    Check that the solution is correct:

    >>> (np.dot(a, x) == b).all()
    True

    """
    a, _ = _makearray(a)
    b, wrap = _makearray(b)
    one_eq = len(b.shape) == 1
    if one_eq:
        b = b[:, newaxis]
    _assertRank2(a, b)
    _assertSquareness(a)
    n_eq = a.shape[0]
    n_rhs = b.shape[1]
    if n_eq != b.shape[0]:
        raise LinAlgError, 'Incompatible dimensions'
    t, result_t = _commonType(a, b)
#    lapack_routine = _findLapackRoutine('gesv', t)
    if isComplexType(t):
        lapack_routine = lapack_lite.zgesv
    else:
        lapack_routine = lapack_lite.dgesv
    a, b = _fastCopyAndTranspose(t, a, b)
    pivots = zeros(n_eq, fortran_int)
    results = lapack_routine(n_eq, n_rhs, a, n_eq, pivots, b, n_eq, 0)
    if results['info'] > 0:
        raise LinAlgError, 'Singular matrix'
    if one_eq:
        return wrap(b.ravel().astype(result_t))
    else:
        return wrap(b.transpose().astype(result_t))


If this isn't enough, you may want to look at the whole file yourself.

Josh

On Wed, Nov 19, 2008 at 7:14 AM, Alan G Isaac <aisaac at american.edu> wrote:
> If if look at help(np.linalg.solve) I am
> told what it does but not how it does it.
> If I look at
> http://www.scipy.org/doc/numpy_api_docs/numpy.linalg.linalg.html#solve
> there is even less info.  I'll guess the
> algorithm is Gaussian elimination, but how
> would I use the documentation to confirm this?
> (Shouldn't I be able to?)
> I don't think possible differences from the "lite"
> version are a reason for saying nothing...
>
> Thanks,
> Alan Isaac
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list