I'm trying to evaluate a matrix multiplication with arrays containing multiple matrices to be multiplied together. This can easily be achieved with two matrices using np.dot (or the new @ operator in Py3.5 +), but I'm struggling to extend this to efficiently evaluate my multidimensional arrays.
As an example, let's say I have matrix A with shape (5,3,3) and B with shape (5,3). Now, I want to matrix multiple the later parts for each 5 cases: i.e. do
res[0] = np.dot(A[0], B[0])
res[1] = np.dot(A[1], B[1])
etc
I can successfully achieve this using a loop - e.g.:
A = np.random.random((5,3,3))
B = np.random.random((5,3))
res = np.zeros([5,3])
for i in range(len(A)):
res[i] = np.dot(A[i], B[i])
althoug this is slow because it uses a loop.
Is there a function / approach I could take to fully vectorize this please?
Thanks.