cimrman3 at ntc.zcu.cz
Wed Nov 1 06:36:34 CST 2006
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
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
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:
>>> 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.
>> SciPy-user mailing list
>> SciPy-user at scipy.org
More information about the SciPy-user