If we compute one dot product between a 4-element vector and a 4x4-matrix, we do
import numpy as np
x = np.random.rand(4)
y = np.random.rand(4,4)
print(x.dot(y))
But what if I want to do the same thing, but for 10 different vectors and matrices at the same time (but with the same dimension)? I tried:
import numpy as np
x = np.random.rand(10,4)
y = np.random.rand(10,4,4)
print(x.dot(y))
But it did not work. I also tried other methods but I could never get the desired output dimension (10,4)
. What is the best way to do this without using a for loop?
This seemed to work:
import numpy as np
x = np.random.rand(10,4)
y = np.random.rand(10,4,4)
np.einsum("ij,ijk->ik", x, y)