[Numpy-discussion] einsum and broadcasting

Jaakko Luttinen jaakko.luttinen@aalto...
Thu Apr 4 08:56:33 CDT 2013


I don't quite understand how einsum handles broadcasting. I get the
following error, but I don't understand why:

In [8]: import numpy as np
In [9]: A = np.arange(12).reshape((4,3))
In [10]: B = np.arange(6).reshape((3,2))
In [11]: np.einsum('ik,k...->i...', A, B)
---------------------------------------------------------------------------
ValueError: operand 0 did not have enough dimensions to match the
broadcasting, and couldn't be extended because einstein sum subscripts
were specified at both the start and end

However, if I use explicit indexing, it works:

In [12]: np.einsum('ik,kj->ij', A, B)
Out[12]:
array([[10, 13],
       [28, 40],
       [46, 67],
       [64, 94]])

It seems that it also works if I add '...' to the first operand:

In [12]: np.einsum('ik...,k...->i...', A, B)
Out[12]:
array([[10, 13],
       [28, 40],
       [46, 67],
       [64, 94]])

However, as far as I understand, the syntax
np.einsum('ik,k...->i...', A, B)
should work. Have I misunderstood something or is there a bug?

Thanks for your help!
Jaakko


More information about the NumPy-Discussion mailing list