[SciPy-Dev] Sparse Matrix Prototype

Daniel Smith smith.daniel.br@gmail....
Tue Mar 5 10:34:31 CST 2013

```Nathaniel Smith <njs <at> pobox.com> writes:

>
> On Tue, Mar 5, 2013 at 2:47 PM, Daniel Smith <smith.daniel.br <at> gmail.com> wrote:
> >> On Tue, Mar 5, 2013 at 1:57 PM, Daniel Smith <smith.daniel.br <at> 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.
>
> I thought the whole point of your example was that for the given sort
> of indexing, ndarray's return an object with shape (N,) and
> np.matrix's return an object with shape (N, 1), and the question was
> which should sparse matrices do? And my point was that there's no such
> thing as a sparse matrix with shape (N,) so the choice was sort of
> obvious? Maybe I'm just missing something.
>
> -n
>

A couple more examples might be instructive:

A = np.zeros((5, 5))
B = np.matrix(A)
B[1].shape
(1, 5)
A[1].shape
(5, )
B[:, 1].shape
(5, 1)
A[:, 1].shape
(5, )
A[:, 1] = A[1, :]
None
B[:, 1] = B[1, :]
ValueError: output operand requires a reduction, but reduction is
not enabled

Basically, matrices differentiate between row and column vectors, and
ndarrays don't.

Daniel
```