[Numpy-discussion] matrix wart
Alan G Isaac
aisaac@american....
Fri Feb 22 08:42:07 CST 2008
>>> On Thu, Feb 21, 2008 at 12:08:32PM -0500, Alan G Isaac
>>> wrote:
>>>> a matrix behavior that I find bothersome and unnatural::
>>> M = N.mat('1 2;3 4')
>>> M[0]
matrix([[1, 2]])
>>> M[0][0]
matrix([[1, 2]])
On Fri, 22 Feb 2008, Stefan van der Walt apparently wrote:
> Could you explain to me how you'd like this to be fixed? If the
> matrix becomes a container of 1-d arrays, then you can no longer
> expect x[:,0] to return a column vector -- which was one
> of the reasons the matrix class was created. While not
> entirely consistent, one workaround would be to detect
> when a matrix is a "vector", and then do 1-d-like indexing
> on it.
Letting M be a matrix and A=M.A, and i and j are integers.
I would want two principles to be honored.
1. ordinary Python indexing produces unsurprising results,
so that e.g. M[0][0] returns the first element of the matrix
2. indexing that produces a 2-d array when applied to A
will produce the equivalent matrix when applied to M
There is some tension between these two requirements,
and they do not address your specific example.
Various reconciliations can be imagined.
I believe a nice one can be achieved with
a truly minimal change, as follows.
Let M[i] return a 1d array. (Unsurprising!)
This is a change: a matrix becomes a container
of arrays (e.g., when iterating).
Let M[:,i] and M[i,:] behave as now.
In addition, as a consistency measure, one might
ask that M[i,j] return a 1 x 1 matrix. (This is
of secondary importance, but it follows the
principle that the use of multiple indexes
produces matrices.)
Right now I'm operating on caffeine instead of sleep,
but that looks right ...
Alan Isaac
More information about the Numpy-discussion
mailing list