[Numpy-discussion] matrix indexing question

Zachary Pincus zpincus at stanford.edu
Tue Mar 27 16:44:09 EDT 2007


> The use case I requested was for iteration over a
> matrix where it is desirable that matrices are yielded.
> That is not what you offered.

No, I offered a thorough discussion of why the design of numpy, as I  
imperfectly understand it, make the trade-offs to achieve your  
desired goal unpalatable.

> The context for this request is my own experience:
> whenever I have needed to iterate over matrices,
> I have always wanted the arrays.  So I am simply
> interested in seeing an example of the opposite desire.

I'm glad that Robert provided such a nice example.

Nevertheless, I think that even absent any example it should be  
pretty clear at this point that if you want iteration over matrices  
to return arrays, either:
(1) you need to be willing to accept that list(iter(M))[i] != M[i]
or
(2) M[i], which equals M[i,:], should be an array.
(or 3, that M[i] and iteration over M is not defined)

Clearly, (1) is fundamentally ridiculous. Clearly use-cases (as well  
as the mathematical definition of matrices) abound to illustrate why  
(2) is no good. Option (3) helps nobody. So which is it? You have to  
choose one!

It is my assertion that if you are iterating over matrices and hoping  
to get arrays, you are misusing the matrix class. Almost by  
definition if you expect M[i,:] or M[i] (or equivalently, the output  
of an iterator over M) to be an array, then M is being used outside  
of a linear algebra context -- and thus, in my view, misused. In  
those cases, the matrix should be cast to an array, which has the  
desired behavior. Robert nicely illustrated a linear-algebraic  
context for matrix iteration.

Now, Bill offers up a different suggestion: indexing M yields neither  
a matrix nor an array, but a class that operates more or less like an  
array, except insofar as it interacts with other matrix objects, or  
other objects of similar classes. I'm interested in hearing more  
about this, what trade-offs or other compromises it might involve.

Zach


>
> Cheers,
> Alan Isaac
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list