I have two numpy arrays that contain compatible matrices and want to compute the element wise outer product of using numpy.einsum. The shapes of the arrays would be:
A1 = (i,j,k)
A2 = (i,k,j)
Therefore the arrays contain i
matrices of shape (k,j)
and (j,k)
respectively.
So given A1
would contain the matrices A,B,C
and A2
would contain matrices D,E,F
, the result would be:
A3 = (A(x)D,B(x)E,C(x)F)
With (x)
being the outer product operator.
This would yield to my understanding based on this answer an array A3
of the following shape:
A3 = (i,j*k,j*k)
So far I have tried:
np.einsum("ijk, ilm -> ijklm", A1, A2)
But the resulting shapes do not fit correctly.
As a sanity check I am testing for this:
A = np.asarray(([1,2],[3,4]))
B = np.asarray(([5,6],[7,8]))
AB_outer = np.outer(A,B)
A_vec = np.asarray((A,A))
B_vec = np.asarray((B,B))
# this line is not correct
AB_vec = np.einsum("ijk, ilm -> ijklm", A_vec,B_vec)
np.testing.assert_array_equal(AB_outer, AB_vec[0])
This currently throws an assertion error as my einsum notation is not correct. I am also open to any suggestions that can solve this and are faster or equally fast as nymphs einsum.