performing outer addition with numpy

All universal functions that take two input arguments have an attribute outer:

x = np.array([1, 2, 3])
np.subtract.outer(x, x)

gives:

array([[ 0, -1, -2],
       [ 1,  0, -1],
       [ 2,  1,  0]])

and

np.multiply.outer(x, x)

results in:

array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

Leave a Comment