numpy numpy.cross Cross Product of Two Vectors


Numpy provides a cross function for computing vector cross products. The cross product of vectors [1, 0, 0] and [0, 1, 0] is [0, 0, 1]. Numpy tells us:

>>> a = np.array([1, 0, 0])
>>> b = np.array([0, 1, 0])
>>> np.cross(a, b)
array([0, 0, 1])

as expected.

While cross products are normally defined only for three dimensional vectors. However, either of the arguments to the Numpy function can be two element vectors. If vector c is given as [c1, c2], Numpy assigns zero to the third dimension: [c1, c2, 0]. So,

>>> c = np.array([0, 2])
>>> np.cross(a, c)
array([0, 0, 2])

Unlike dot which exists as both a Numpy function and a method of ndarray, cross exists only as a standalone function:

>>> a.cross(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'cross'