Next / Previous / Contents / Shipman's homepage

7. Dot and cross products

To find the matrix (dot) product of two arrays a1 and a2, use the function np.dot(a1, a2).

>>> a1 = np.array([[1, 2, -4], [3, -1, 5]])
>>> a2 = np.array([[6, -3], [1, -2], [2, 4]])
>>> print a1
[[ 1  2 -4]
 [ 3 -1  5]]
>>> print a2
[[ 6 -3]
 [ 1 -2]
 [ 2  4]]
>>> np.dot(a1, a2)
array([[  0, -23],
       [ 27,  13]])

Similarly, np.cross(x, y) returns the cross-product of vectors x and y.

>>> x = (1, 2, 0)
>>> y = (4, 5, 6)
>>> print np.cross(x, y)
[12 -6 -3]