I have a 3D numpy array with shape (N, 3, 3).
I want to multiply each 3x3 matrix by its transpose and then sum all of them.
This is my code:
m = np.zeros([3, 3])
for matrix in matrices:
m += np.dot(matrix.T, matrix)
but I think it could be done just using numpy.
I was thinking to use np.apply_along_axis, but it works only with 1D arrays.
Is there a way to avoid the for loop and make the product and sum in one go?
With a sample array:
A matmul solution, transposing the trailing (3,3) arrays, and summing on the lead dimension:
And the einsum approach as given in the other answer.
I had to try several einsum indices to get the matching one. Summing on the shared
idimension was easy, but get the 'transpose' right required some trial-n-error or systematic thinking.Seeing the repeated
ikin the einsum, suggests an alternative - reshape to 2d, and do the normal dot:For this small sample case this is 2x faster.