[Numpy-discussion] .T Transpose shortcut for arrays again

Bill Baxter wbaxter at gmail.com
Wed Jul 5 00:03:34 EDT 2006


Just wanted to make one last effort get a .T attribute for arrays, so that
you can flip axes with a simple "a.T" instead of "a.transpose()", as with
numpy matrix objects.

If I recall, the main objection raised before was that there are lots of
ways to transpose n-dimensional data.

Fine, but the fact is that 2D arrays are pretty darn common, and so are a
special case worth optimizing for.
Furthermore transpose() won't go away if you do need to do some specific
kind of axes swapping other than the default, so noone is really going to be
harmed by adding it.

I propose to make .T a synonym for .swapaxes(-2,-1) {*}, i.e. the last two
axes are interchanged.  This should also make it useful in many N-d array
cases (whereas the default of .transpose() -- to completely reverse the
order of all the axes -- is seldom what you want).  Part of the thinking is
that when you print an N-d array it's the last two dimensions that get
printed like 2-d matrices separated by blank likes.  You can think of it as
some number of stacks of 2-d matrices.  So this .T would just transpose
those 2-d matrices in the printout.  Those are the parts that are generally
most contiguous in memory also, so it makes sense for 2-d matrix bits to be
stored in those last two dimensions.

Then, if there is a .T, it makes sense to also have  .H which would
basically be equivalent to .T.conjugate().

Finally, the matrix class has .A to get the underlying array -- it would
also be nice to have a .M on array as a shortcut for  asmatrix().  This one
would be very handy for matrix users, I think, but I could go either way on
that, having abandoned matrix myself.  Ex: ones([4,4]).M

Other possibilities:
- Make .T a function, so that you can pass it the same info as
.transpose().  Then the shortcut becomes a.T(), which isn't as nice, and
isn't consistent with matrix's .T any more.
- Just make .T raise an error for ndim>2.  But I don't really see any
benefit in making it an error as opposed to defining a reasonable default
behavior.
- Make .T on a 1-dim array return a 2-dim Nx1 array.  (My default suggestion
is to just leave it alone if ndim < 2, an exception would be another
possiblility).  Would make an easy way to create column vectors from arrays,
but I can think of nothing else in Numpy that acts that way.


This is not a 1.0 must have, as it introduces no backward compatibility
issues.  But it would be trivial to add if the will is there.


{*} except that negative axes for swapaxes doesn't seem work currently, so
instead it would need to be something like:
      a.transpose( a.shape[:-2] + (a.shape[-1],a.shape[-2]) )
   with a check for "if ndim > 1", of course.



--Bill
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060705/34b6cb76/attachment.html>


More information about the NumPy-Discussion mailing list