[Numpy-discussion] matrix indexing question

Colin J. Williams cjw@sympatico...
Mon Mar 26 06:39:40 CDT 2007

```Bill Baxter wrote:
> On 3/26/07, Colin J. Williams <cjw@sympatico.ca> wrote:
>> Bill Baxter wrote:
>>> This may sound silly, but I really think seeing all those brackets is
>>> what makes it feel wrong.   Matlab's  output doesn't put it in your
>>> face that your 4 is really a matrix([[4]]), even though that's what it
>>> is to Matlab.  But I don't see a good way to change that behavior.
>>>
>>> The other thing I find problematic about matrices is the inability to
>>> go higher than 2d.  To me that means that it's impossible to go "pure
>>> matrix" in my code because I'll have to switch back to arrays any time
>>> I want more than 2d (or use a mixed solution like a list of matrices).
>>>  Matlab allows allows >2D.
>>>
>>> --bb
>> "pure matrix" seems to me an area of exploration, does it have any
>> application in numerical computation at this time?
>
> I'm not sure what you thought I meant, but all I meant by going "pure
> matrix" was having my Numpy code use the 'matrix' type exclusively
> instead of some mix of 'matrix' and the base 'ndarray' type.
It was a term I had not come across before but I assumed that you were
referring to something like this link - beyond my comprehension.

http://72.14.203.104/search?q=cache:Yu9gbUQEfWkJ:math.ca/Events/winter05/abs/pdf/ma-df.pdf+pure+matrix&hl=en&ct=clnk&cd=4&gl=ca&lr=lang_en
Things
> become messy when you mix and match them because you don't know any
> more if an expression like A[1] is going to give you a 1-D thing or a
> 2-D thing, and you can't be sure what A * B will do without always
> coercing A and B.

Yes, to my mind it's best to consider the multi-dimensional array and
the matrix to be two distinct data types.  In most cases, it's best that
conversions between the two should be explicit.
>
>> A list of matrices seems to be a logical structure.
>
> Yes, and it's the only option if you want to make a list of matrices
> of different shapes, but I frequently have a need for things like a
> list of per-point transformation matrices.  Each column from each of
> those matrices can be thought of as a vector.  Sometimes its
> convenient to consider all the X basis vectors together, for instance,
> which is a simple and efficient M[:,:,0] slice if I have all the data
> in a 3-D array, but it's a slow list comprehension plus matrix
> constructor if I have the matrices in a list -- something like
>     matrix([m[:,0] for m in M])
> but that line is probably incorrect.

Logically, this makes sense, where M is a list of matrices.

My guess is that it would be a little faster to build one larger matrix
and then slice it as needed.
>
>> PyMatrix deals with
>> lists in building a larger matrix from sub-matrices.
>>
>> Suppose that we have matrices A (3, 4), B (3, 6), C (4, 2) and D (4, 8).
>>
>> Then E= M([[A, B], [C, D]]) gives E (7, 10).
>
> Numpy generally tries to treat all lists and tuples as array literals.
>  That's not likely to change.
That need no be a problem is there is clarity of thinking about the
essential difference between the matrix data type (even if is is built
as a sub-type of the array) and the multi-dimensional array.
>
> --bb

Colin W.

```