[SciPy-user] more help with sparse matrix: creating

Dominique Orban dominique.orban@gmail....
Mon Oct 8 12:02:04 CDT 2007

On 10/8/07, Robin <robince@gmail.com> wrote:
> On 10/8/07, Robin <robince@gmail.com> wrote:
> > On further reading I realized I was providing the arguments incorrectly -
> specifying them by name worked.
> >
> > Now I am just stuck on the part where I convert the allocated csr array to
> lil format, to fill it and then convert it back to csr. I really need this
> to be in place, since there won't be enough memory to hold two copies of the
> array. Is this possible?
> >
> So it seems this isn't possible to do with scipy.sparse unless I'm missing
> something.
> I see from this post that Ed Schofield made a seperate branch to address
> this and other issues with sparse
> http://projects.scipy.org/pipermail/scipy-user/2006-February/007192.html
> but it doesn't look like that has seen any activity for more than a year.
> Were those changes ever merged?
> Is there any chance they will be?
> Alternatively I think it might be possible with PySparse so I am continuing
> to look at that....
> Any advice on comments from more experienced folk on whether this is
> possible or whether I'm better off just sticking with MATLAB for this
> project would be appreciated.


I believe what you want to do is quite easy with PySparse:

from pysparse import spmatrix
M = spmatrix.ll_mat( ncol, nrow, nnz )

will allocate room for an nrow-by-ncol sparse matrix with nnz nonzeros
in linked-list format. Alternatively, if you know that your matrix is
symmetric (and hence, square), you only keep one triangle in memory
and allocate it by saying:

M = spmatrix.ll_mat_sym( n, nnz )

Then you can assign values to slices of M using M[:k,:p] = ..., etc.

If your guess for nnz was an under-estimate, M expands dynamically
(nnz is "just a hint"). The spmatrix manual, that you can find in the
source distribution of PySparse, is a great handbook.

The default format in PySparse is ll_mat. If you need to, you can
convert your matrix to csr format with M.to_csr(). You can save your
matrix to file in MatrixMarket format using M.export_mtx() or read in
a matrix using ll_mat_from_mtx().

I hope this helps.

More information about the SciPy-user mailing list