[SciPy-user] csr_matrix

Sam frizou excellent.frizou at gmail.com
Wed Nov 1 06:52:42 CST 2006


Thank you Robert.

In fact, I need to do row and columns basic operations on my matrices
(switch 2 rows, switch 2 cols, add a row with another into the matrix
(eg. m[1,:]=m[1,:]+m[3,:]), and same things for columns.

Moreover I do not need that my program is extremly fast. In fact my
matrices are vrey sparse and classical matrix simply do not fit into
the memory.

So, if I well understand what you told me. I would better use DOK matrices ?

Thank you very much.



On 11/1/06, Robert Cimrman <cimrman3 at ntc.zcu.cz> wrote:
> Sam frizou wrote:
> > Thanks a lot.
> >
> > I have another problem with sparse matrices please. I do not find how
> > to apply an operation directly to an entire row. For example,
> > m[1,:]=dot(m[1,:],4) does not work and I didnt found a "setrow" method
> > in the documentation.
>
> What do you mean by dot(m[1,:],4)? m[1,:] * 4?
> Anyway, you should use lil_matrix for this kind of computation
> (e.g. b[1,:] = b[1,:] * 2).
>
> In general, setting/getting whole rows/columns is not supported for
> sparse matrices (with exceptions) yet. It is partly due to fact that
> each sparse matrix format has its own limitations - e.g. getting a
> column of a matrix stored in CSR is utterly inefficient when compared to
> getting a row - that is why you can use m[1,:] to get a row, but not
> m[:,1] to get a column. Analogously, CSC matrix (just exchange the roles
> of rows/columns).
>
> To set a row, one generally needs to add new elements - this would
> involve large memory reallocations in the CSR case, so it is not
> implemented yet - use LIL matrix instead.
>
> To set a column, even LIL cannot be used (it is row oriented..., just as
> CSR), so use the DOK matrix (the most general, but also the slowest for
> certain operations)...
>
> To conclude - construct your matrix in LIL format (reasonably fast for
> adding new elements), then convert the matrix into CSR/CSC which are
> good for sparse linear solvers (if that is what you want).
>
> It would be possible to allow getting/setting items just like with fancy
> indexing of the dense arrays, but one would loose the purpose of the
> sparse matrices - the efficiency.
>
> > On 11/1/06, Robert Cimrman <cimrman3 at ntc.zcu.cz> wrote:
> >> Sam frizou wrote:
> >>> Hi,
> >>>
> >>> Is it possible to get the set of indices of non null elements of a
> >>> sparse matrix M ?
> >>> I means I can see them by doing "print M", but I want to use them, so
> >>> I would like something like a list of (i,j).
> >> A general way (should work for any kind of sparse matrix) is:
> >> [M.rowcol(ii) for ii in xrange(M.size)].
> >>
> >> for csr_matrix, you can use directly M.colind (array of all column
> >> indices), M.indptr (array pointing to first index of a row in colind)
> >> attributes, but I would not recommend it, unless the first approach is
> >> too slow.
> >>
> >> cheers,
> >> r.
> >>
> >> _______________________________________________
> >> SciPy-user mailing list
> >> SciPy-user at scipy.org
> >> http://projects.scipy.org/mailman/listinfo/scipy-user
> >>
> >
> >
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>


-- 
On obtient beaucoup plus avec un mot gentil et une arme qu'avec
seulement un mot gentil - "Al Capone".


More information about the SciPy-user mailing list