[SciPy-user] spare matrices

Nathan Bell wnbell@gmail....
Tue Mar 17 08:20:38 CDT 2009

On Tue, Mar 17, 2009 at 7:11 AM, Eric Friedman <ejf27@cornell.edu> wrote:
> 1)How do I find the detailed descriptions of functions like coo?
> Is there a good
> document (which I haven't been able to find) or do I need
> to look inside the code?
> For example, I don't see how to access individual elements in a coo.
> Also, I'd like to be able to pull out a submatrix from a
> subset of the rows and
> find the set of rows which are nonzero.

The docstrings in scipy.sparse are the best source of information.  I
generally use IPython to read docstrings:
In [1]: from scipy.sparse import *
In [2]: csr_matrix? <press enter>

The same information is available here:

To extract submatrices you'll need to use either the CSR or CSC
formats.  These formats support most of the fancy indexing tricks that
numpy provides for arrays.  The find() function in scipy.sparse may
also be helpful.

The sparse documentation doesn't currently inform users about "best
practices" for efficiency, so feel free to ask on this list for help
with specific operations.

> 2) I tried linsolve.spsolve and it says I should use
> sparse.linalg.dsolve but
> when I try that directly I can't get it to work.
> Also, is there any
> documentation on dsolve or spsolve? My matrix is
> singular, but the matrix
> equation is still solvable -- can it deal with that?

Yes, scipy.sparse.spsolve() can solve some consistent but singular systems.

>>> from scipy import rand
>>> from scipy.sparse.linalg import spsolve
>>> from scipy.sparse import *
>>> A = identity(10, format='csc')
>>> A[9,9] = 0
>>> b = A*rand(10)
>>> spsolve(A,b)

However spsolve() is known to fail in some cases:

spsolve() uses an outdated version of SuperLU.  We should update this
by SciPy 0.8.

If you find spsolve() inadequate, then consider the UMFPACK scikit instead:

FWIW, UMFPACK is the same solver that MATLAB uses for solving sparse
linear systems.  Also, I believe it is generally faster than SuperLU.

Nathan Bell wnbell@gmail.com

More information about the SciPy-user mailing list