einsum and broadcasting
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
On Thu, 2013-04-04 at 16:56 +0300, Jaakko Luttinen wrote:
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?
My guess is, it is by design because the purpose of the ellipsis is more to allow extra dimensions that are not important to the problem itself. A vector product is np.einsum('i,i->i') and if I write np.einsum('...i,...i->...i') I allow generalizing that arrays of 1-d arrays (like the new gufunc linalg stuff). I did not check the code though, so maybe thats not the case. But in any case I don't see a reason why it should not be possible to only allow extra dims for some inputs (I guess it can also make sense to not give ... for the output). So I would say, if you want to generalize it, go ahead ;). - Sebastian
Thanks for your help! Jaakko _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (2)
-
Jaakko Luttinen -
Sebastian Berg