[Numpy-discussion] matrix indexing question

Joachim Dahl dahl.joachim@gmail....
Tue Mar 27 13:23:17 CDT 2007

I would like to hear your opinion on developing an explicit sparse/dense 2D
matrix class with indexing similar to Matlab, and without significant
differences between sparse and dense matrices from the user's

I know that it's not one of Numpy/Scipy's goals to clone Matlab,  but I
think I represent a potentially large scientific Python user base,  who
would find Python matrices that feel a bit more like Matlab/Octave etc.
extremely useful.  I have a great respect for all the work in Numpy and
but at the same time I feel that numerical linear algebra in Python would
benefit from a more dedicated matrix library,  and I think that Numpy (or
another single package) should provide that
in a homogeneous way - without ties to how Numpy array works.

- Joachim

On 3/27/07, Christopher Barker <Chris.Barker@noaa.gov> wrote:
> Zachary Pincus wrote:
> > rest of linear algebra -- e.g. that m[0] yields a matrix if m is a
> > matrix-- it almost certainly would violate the principle of least
> > surprise for iteration over m (intuitively understood to be choosing m
> > [0] then m[1] and so forth) to yield anything other than a matrix.
> I don't think the OP was suggesting that. Rather, I think the suggestion
> was that for a  mXn matrix, M:
> M[i].shape == (n,)
> M[i:i+1].shape == (1,n)
> that is, indexing (or iterating returns a vector, and slicing returns a
> matrix). This is, indeed exactly how numpy arrays behave!
> The problem with this is:
> numpy matrices were created specifically to support linear algebra
> calculations. For linear algebra, the distinction between row vectors
> and column vectors is critical. By definition, a row vector has shape:
> (1,n), and a column vector has shape (m,1).
> In this case, perhaps the OP is thinking that a shape (n,) array could
> be considered a row vector, but in that case:
> M[1,:].shape == (n,)
> M[:,1].shape == (m,)
> which of these is the row and which the column? This is why matrices
> index this way instead:
> M[1,:].shape == (1, n)
> M[:,1].shape == (m, 1)
> now we know exactly what is a row and what is a column.
> By the way, I think with the way numpy works: M[i] == M[i,:] by
> definition, so those couldn't yield different shaped results. Is that
> right?
> I think we got a bit sidetracked by the example given. If I have a bunch
> of points I want to store, I'm going to use an (m,2) *array*, not a
> matrix, then then A[i] will yield a (2,) array, which makes sense for
> (2-d) points. In fact, I do this a LOT.
> If I happen to need to do some linear algebra on that array of points,
> I'll convert to a matrix, do the linear algebra, then convert back to an
> a array (or just use the linear algebra functions on the array).
> I hope this helps
> -Chris
> > This can't possibly be what you're asking for, right? You aren't
> > suggesting that m[0] and list(iter(m))[0] should be different types?
> >
> > There are many clear and definite use cases for m[0] being a matrix,
> > by the way, in addition to the theoretical arguments -- these aren't
> > hard to come by. Whether or nor there are actual good use-cases for
> > iterating over a matrix, if you believe that m[0] should be a matrix,
> > it's madness to suggest that list(iter(m))[0] should be otherwise.
> >
> > My opinion? Don't iterate over matrices. Use matrices for linear
> > algebra. There's no "iteration" construct in linear algebra. The
> > behavior you find so puzzling is a necessary by-product of the
> > fundamental behavior of the matrix class -- which has been explained
> > and which you offered no resistance to.
> >
> > Zach
> >
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion@scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> --
> Christopher Barker, Ph.D.
> Oceanographer
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
> Chris.Barker@noaa.gov
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070327/c7c7fa03/attachment.html 

More information about the Numpy-discussion mailing list