[SciPy-Dev] Sparse Matrix Prototype

Daniel Smith smith.daniel.br@gmail....
Tue Mar 5 08:47:34 CST 2013

> On Tue, Mar 5, 2013 at 1:57 PM, Daniel Smith <smith.daniel.br@gmail.com> wrote:
>> Hi,
>> I've been working on adding fancy indexing to the LIL sparse matrix
>> class and have run into a problem. Judging from the tests, no decision
>> has been made as to what NumPy structure the class should replicate.
>> In particular, some of the tests assume ndarray behavior, and others
>> assume matrix behavior. The biggest difference is in row/column vector
>> behavior. Take for example:
>> A = np.zeros((5, 5))
>> B = np.matrix(A)
>> A[:, 1]
>>     array([0, 0, 0, 0, 0])
>> B[:, 1]
>>     matrix([ [0], [0], [0], [0], [0] ])
>> Since NumPy is encouraging people to use ndarrays over matrices, it
>> might make sense to reproduce the ndarray behavior. However, since the
>> class has other matrix-specific behavior, e.g. being only
>> two-dimensional, it might be confusing to have the class behave like
>> an ndarray in other ways. Until a decision is made, no version of the
>> class will pass all the tests as currently written. Any input would be
>> greatly appreciated.
> I find the "matrixness" of sparse matrices to be constantly annoying
> and a source of tons of special cases in code that wants to handle
> both sparse and dense matrices... but it is what it is. If they can't
> act like ndarrays, better they act like np.matrix's than like some
> weird mash-up of the two. And in particular the key thing for this
> indexing example is that we *can't* return a sparse 1-d ndarray-alike,
> right, because we have no structure to represent sparse 1-d things?
> -n

We have at least one sparse 1-d ndarray like thing in a sparse column
vector, a 1xN matrix. This code would output exactly what you want:

B = sparse.lil_matrix(np.reshape(np.arange(25), (5, 5)))
vector = np.squeeze(B[:, 0].A)
    np.array([0, 5, 10, 15, 20])

Another option is to leave the built-in __getitem__ and __setitem__
behavior like a matrix and add get_array, set_array which would mirror
ndarray behavior. We already have the method .A which returns a dense
ndarray to go along with .todense() which returns a dense matrix.


More information about the SciPy-Dev mailing list