I have an array of dimension (5,...)
(...
can be anything) and I would like to form the dot product of all dimension after the 5
such that the resulting array has shape (5,)
. I thought the einsum
i...,i...->i
looked promising, but alas
import numpy as np
a = np.random.rand(5)
b = np.einsum("i...,i...->i", a, a)
assert b.shape == (5,) # okay
a = np.random.rand(5, 2)
b = np.einsum("i...,i...->i", a, a) # fail
assert b.shape == (5,)
a = np.random.rand(5, 2, 3)
b = np.einsum("i...,i...->i", a, a) # fail
assert b.shape == (5,)
in einsum
return c_einsum(*operands, **kwargs)
ValueError: output has more dimensions than subscripts given in einstein sum, but no '...' ellipsis provided to broadcast the extra dimensions.
I could perhaps reshape a
into
b = np.einsum("ij,ij->i", a.reshape(a.shape[0], -1), a.reshape(a.shape[0], -1))
but this looks so messy. Any better suggestion?