[Numpy-discussion] ndarray.T2 for 2D transpose

Ian Henriksen insertinterestingnamehere at gmail.com
Fri Apr 8 19:37:40 EDT 2016

On Fri, Apr 8, 2016 at 4:04 PM Alan Isaac <alan.isaac at gmail.com> wrote:

> On 4/8/2016 5:13 PM, Nathaniel Smith wrote:
> > he doesn't want 2d matrices, he wants
> > tools that make it easy to work with stacks of 2d matrices stored in
> > 2-or-more-dimensional arrays.
> Like `map`?
> Alan Isaac
Sorry if there's any misunderstanding here.

Map doesn't really help much. That'd only be good for dealing with three
dimensional cases and you'd get a list of arrays, not a view with the
axes swapped.

np.einsum('...ji', a)
np.swapaxes(a, -1, -2)
np.rollaxis(a, -1, -2)

all do the right thing, but they are all fairly verbose for such a simple

Here's a simple example of when such a thing would be useful.
With 2D arrays you can write

If you want to have that same operation follow the existing gufunc
semantics you end up having to write one of the following

np.einsum('...ij,...kj', a, b)
a @ np.swapaxes(a, -1, -2)
a @ np.rollaxis(a, -1, -2)

None of those are very concise, and, when I look at them, I don't usually
"that does a.dot(b.T)."

If we introduced the T2 syntax, this would be valid:

a @ b.T2

It makes the intent much clearer. This helps readability even more when
trying to put together something that follows a larger equation while still
broadcasting correctly.

Does this help make the use cases a bit clearer?


-Ian Henriksen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160408/02aced3f/attachment.html>

More information about the NumPy-Discussion mailing list