I have two NumPy arrays (of equal length), each with (equally-sized, square) NumPy matrices as elements. I want to do elementwise matrix multiplication of these two arrays, i.e. get back a single array where the i-th element is the matrix product of the i-th elements of my two arrays.
When I simply try to multiply the arrays together, it seems that the program tries to calculate the matrix product of the arrays, and then fails because their dimensionality is too high (1 for the array + 2 for the matrices which are its elements).
The problem could of course be solved with a for-loop, but I was hoping that there was some way in which it could be done that keeps everything internal to NumPy, in order to take full advantage of its increased efficiency.f
EDIT:
To clarify, say I have two arrays np.array([A, B, C]) and np.array([X, Y, Z]) where A, B, C, X, Y and Z are all 3x3 square matrices, what I need is a function that will return np.array([A*X, B*Y, C*Z]), where * is matrix multiplication.
(n, 3, 3)shaped. Prior to providing the@(np.matmul), the best solution would have been:np.einsum('ijk,ikl->ijl', [A,B,C], [X,Y,Z]). It is still useful as a way of expressing, and visualizing, complex matrix products. - hpaulj