[SciPy-User] Indexing sparse matrices with step

Pascal Lamblin lamblinp@iro.umontreal...
Thu Feb 2 10:52:44 CST 2012

Hi everybody,

I've noticed that if I have a scipy.sparse matrix (csr or csc), and I
try to index into it with slices, the "step" component of my slice seems
to be silently ignored.

Is it an expected behaviour? I would have expected an error saying only
a step of None (or not providing a step at all) is supported.

Here is a small test case:

import numpy, scipy.sparse

sm = scipy.sparse.csc_matrix([[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 1, 0]])

# True, expected
numpy.all(sm[:1,:].toarray() == sm.toarray()[:1,:])

# False, expected
numpy.all(sm.toarray()[:1,:] == sm.toarray()[:1:-1,:])

# True, unexpected
numpy.all(sm[:1:-1,:].toarray() == sm[:1,:].toarray())

# False, unexpected
numpy.all(sm[:1:-1,:].toarray() == sm.toarray()[:1:-1,:])

Thanks in advance,

More information about the SciPy-User mailing list