Actually the answer provided by pv. incorrect, since the resulting xy array will have the form (100,3,3). Correct broadcasting:
import numpy as np from numpy import newaxis x = np.random.randn(100, 3) y = np.random.randn(100, 3) xy = x[:,newaxis, :,newaxis] * y[newaxis,:,newaxis,:]
The resulting array xy now has the form (100, 100, 3, 3) and contains the crossed products of all pairs of three-dimensional vectors with respect to x and y:
for i,a in enumerate(x): for j,b in enumerate(y): if not np.alltrue(np.outer(a,b) == xy[i,j]): print("The code is wrong")
does not display the result :)
source share