[Numpy-discussion] matrix wart

Alan G Isaac aisaac@american....
Thu Feb 21 18:10:24 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:
> This is exactly what I would expect for matrices: M[0] is 
> the first row of the matrix.

Define what "first row" means!
There is no standard definition that says this is means the
**submatrix** that can be created from the first row.
Someone once pointed out on this list that one might 
consider a matrix to be a container of 1d vectors.  For NumPy, 
however, it is natural that it be a container of 1d arrays.
(See the discussion for the distinction.)

Imagine if a 2d array behaved this way.  Ugh!
Note that it too is 2d; you could have the same 
"expectation" based on its 2d-ness.  Why don't you?

You "expect" this matrix behavior only from experience with 
it, which is why I "expect" it too, while hating it. It is 
not what new users will expect and also not desirable.
As Konrad noted, it is very odd behavior to treat a matrix 
as a container of matrices.  You can only "expect" this 
behavior by learning to expect it (by use), which is 

Nobody has objected to returning matrices when getitem is 
fed multiple arguments: these are naturally interpreted as 
requests for submatrices.  M[0][0] and M[:1,:1] are very 
different kinds of requests: the first should return the 0,0
element but does not, while M[0,0] does!  Bizarre!
How to guess??  If you teach, do your students expect this 
behavior?  Mine don't!

This is a wart.

The example really speaks for itself. Since Konrad is an 
extremely experienced user/developer, his reaction should 
speak volumes.

Alan Isaac

More information about the Numpy-discussion mailing list