I have a Vector, M, with size N and a Tensor, d, with size NxNxD.
My aim is to perform the matrix multication M*d[i,:,:] for each i to get a new matrix with size nxD.
Now I could just do it like this:
a = np.zeros((n,D))
for i in range(n):
a[i] = G*np.matmul(M,d[i,:,:])
but I'd prefer a one line solution if it exists. Anyone got any ideas?
Don't use a loop or a comprehension. Use einsum
:
G = 0.5
d = rand.integers(low=0, high=10, size=(3, 3, 2))
M = rand.integers(low=0, high=10, size=3)
a = G*np.einsum('ijk,j->ik', d, M)
print(a)