[Numpy-discussion] Who uses matrix?

Tim Hochberg tim.hochberg at cox.net
Fri May 12 08:06:32 EDT 2006


Bill Baxter wrote:

> On 5/11/06, *Robert Hetland* <hetland at tamu.edu 
> <mailto:hetland at tamu.edu>> wrote:
>
>
>     Is it worth it to convert the arrays to matrices in order to do this
>     handful of calculation?  Almost.  I covet the shorthand .T notation
>     in matrix object while getting RSI typing in t-r-a-n-s-p-o-s-e.
>     Also, for involved calculations inverse, transpose et. al are long
>     enough words such that the line always wraps, resulting in less-
>     readable code.
>
Note that T(a) is only one character longer than a.T and is trivially 
implementable today. If you're doing enough typing of t-r-a-n-s-p-o-s-e 
to induce RSI, it's surely not going to hurt you to type:

    def T(a): return a.transpose()

etc, somewhere at the top of your module.

>
> +1 on a .T shortcut for arrays.
> +1 on a .H shortcut for arrays, too.  (Instead of .conj().transpose())

-1.

These are really only meaningul for 2D arrays. Without the axis keyword 
they generally do the wrong things for arrays of other dimensionality 
(there I usually, but not always, want to transpose the first two or 
last two axes). In addition, ndarray is already overstuffed with methods 
and attributes, let's not add any more without careful consideration.

> I'm not wild about the .I shortcut.  I prefer to see big expensive 
> operations like a matrix inverse to stand out a little more when I'm 
> looking at the code.  And I hardly ever need an inverse anyway 
> (usually an lu_factor or SVD or something like that will do what I 
> need more efficiently and robustly than directly taking an inverse).

Agreed. In addition, inverse is another thing that's specific to 2D arrays.

> I just finished writing my first few hundred lines of code using array 
> instead of matrix.  It went fine.  Here are my impressions: it was 
> nice not having to worry so much about whether my vectors were row 
> vectors or column vectors all the time, or whether this or that was 
> matrix type or not.  It felt much less like I was fighting with numpy 
> than when I was using matrix.  I also ported a little bit of matlab 
> code that was full of apostrophes (matlab's transpose operator).  With 
> numpy arrays, all but one of the transposes went away.    I realized 
> also that most of what I end up doing is wrangling data to get it in 
> the right shape and form so that I can to do just a few lines of 
> linear algebra on it, similar to what you observe, Rob, so it makes 
> little sense to have everything be matrices all the time.
>
> So looks like I'm joining the camp of "matrix -- not worth the bother" 
> folks.  The .T shortcut for arrays would still be nice though. 
>
> And some briefer way to make a new axis than 'numpy.newaxis' would be 
> nice too (I'm trying to keep my global namespace clean these days).
> --- Whoa I just noticed that a[None,:] has the same effect as 
> 'newaxis'.  Is that a blessed way to do things?

numpy.newaxis *is* None. I don't think that I'd spell it that way since 
None doesn't really have the same conotations as newaxis, so I think 
I'll stick with np.newaxis, which is how I spell these things.

For an even weirder example, which only exists in a parallel universe, I 
thought about proposing alls as another alias for None. This was so 
that, for instance, one could do:

    sum(a, axes=all)

instead of sum(a, axes=None). That's clearer, but the cognitive 
disonance of 'all=None' scared me away.

-tim







More information about the NumPy-Discussion mailing list