I have two matrix arrays A and B such with identical shape: A.shape = B.shape = (M,N,P)
I would like to compute the Kronecker product along the axis 0, so that:
KP[ii,:,:] = A[ii,:,:]⊗B[ii,:,:]
Is there a way of doing this in numpy without using for loops?
Thanks!
Example:
A = np.array([ [[1,0],
[0,1]],
[[1,0],
[0,1]]
])
B = np.array([ [[1,0],
[0,-1]],
[[0,1],
[1,0]]
])
KP = np.array( [
[[1,0,0,0],
[0,-1,0,0],
[0,0,1,0],
[0,0,0,-1]],
[[0,1,0,0],
[1,0,0,0],
[0,0,0,1],
[0,0,1,0]]
] )
which would be equivalent to:
KP= np.zeros( (A.shape[0],
A.shape[1]**2,
A.shape[2]**2) )
for ii in range(A.shape[0]):
KP[ii,:,:] = np.kron(A[ii,:,:],B[ii,:,:])