
Here is a concrete example for what I am after
A = np.arange(12).reshape(2,3,2)
B = np.arange(12,24).reshape(2,2,3)
C = np.arange(32).reshape(2,4,4)
result = np.zeros((12,12))
for i in range(2):
result += np.kron(A[i,:,:] @ B[i,:,:], C[i,:,:])
How can I implement this more efficiently?
Many thanks for your help!
As suggested, I had a look into numpy.einsum. This turned out to be quite nice. A solution is:
np.einsum('ijk,imn->jmkn', np.einsum('ijk,ikm->ijm', A, B), C).reshape(A.shape[1] * C.shape[1], B.shape[2] * C.shape[2])
np.einsum() produces a 3d array of the products of the 2d "slices" of A and Bnp.einsum() mimics (after appropriate reshaping) the kronecker product of this 3d matrix and C and summation.I found the following two posts very helpful:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With