Extend A and B to 3D , keeping your first axis aligned and introducing new axes along the third and second, respectively None/np.newaxis , and then multiply with each other. This will allow broadcasting to come into play for a vectorized solution.
So the implementation will be -
A[:,:,None]*B[:,None,:]
We could shorten it a bit using ellipsis for A:: :,: and skip listing the remaining last axis with B , for example -
A[...,None]*B[:,None]
As another vectorized approach, we could also use np.einsum , which can be more intuitive after we go through the syntax of string notation and consider those notations are representatives of iterators involved in a naive looping implementation, for example:
np.einsum('ij,ik->ijk',A,B)
source share