NumPy: Cross Product in 3D Space

In this tutorial, we will learn how to find the cross product of two vectors using NumPy's numpy.cross() function.

Consider two vectors $\mathbf{u}$ and $\mathbf{v}$, where

$$ \mathbf{u} = a_{1}\mathbf{i} + a_{2}\mathbf{j} + a_{3}\mathbf{k} \\ \mathbf{v} = b_{1}\mathbf{i} + b_{2}\mathbf{j} + b_{3}\mathbf{k} $$

We may also represent them as linear arrays $\mathbf{u} = (a_{1}, a_{2}, a_{3})$ and $\mathbf{v} = (b_{1}, b_{2}, b_{3})$.

The cross product of two three-dimensional vectors $\mathbf{u}$ and $\mathbf{v}$ is denoted by

$$ \mathbf{u} \times \mathbf{v} $$

and is defined by

$$ \mathbf{u} \times \mathbf{v} = (a_{2}b_{3} - a_{3}b_{2})\mathbf{i} + (a_{3}b_{1} - a_{1}b_{3})\mathbf{j} + (a_{1}b_{2} - a_{2}b_{1})\mathbf{k} $$

which is

$$ \mathbf{u} \times \mathbf{v} = \begin{vmatrix} a_{2} & a_{3} \\ b_{2} & b_{3} \\ \end{vmatrix} \mathbf{i} - \begin{vmatrix} a_{1} & a_{3} \\ b_{1} & b_{3} \\ \end{vmatrix} \mathbf{j} + \begin{vmatrix} a_{1} & a_{2} \\ b_{1} & b_{2} \\ \end{vmatrix} \mathbf{k} $$

This whole cross product can actually be expressed as a single $3 \times 3$ determinant.

$$ \mathbf{u} \times \mathbf{v} = \begin{vmatrix} \mathbf{i} & \mathbf{j} & \mathbf{k} \\ a_{1} & a_{2} & a_{3} \\ b_{1} & b_{2} & b_{3} \\ \end{vmatrix} $$

Vector cross product is defined only in $R^{3}$.

Python has a numerical library called NumPy, which has a function called numpy.cross() to compute the cross product of two vectors.

Now we pick two vectors from an example in the book Linear Algebra (4th Ed.) by Seymour Lipschutz and Marc Lipson1.

$$ 4\mathbf{i} + 3\mathbf{j} + 6\mathbf{k} \\ 2\mathbf{i} + 5\mathbf{j} - 3\mathbf{k} $$

Below we compute their cross product using NumPy's numpy.cross() function.

				
					import numpy as np
					u = [4,3,6]	
					v = [2,5,-3]			
					crossproduct = np.cross(u,v)
					print(crossproduct)
				
			

On executing the above Python script, we get the resulting vector as

				
					[-39 24 14]
				
			

which is

$$ -39\mathbf{i} + 24\mathbf{j} + 14\mathbf{k} $$

Now one important note on array representation here. Sometimes it may seem like the cross product is being carried out on a vectors of dimension lower than 3, and even NumPy does not seem to have any problem processing it either. To see what I mean, even if you input vectors $\mathbf{u}$ and $\mathbf{v}$ as follows

				
					import numpy as np
					u = [1,2]	
					v = [4,5]			
					crossproduct = np.cross(u,v)
					print(crossproduct)
				
			

NumPy still evaluates and gives a result. That is because, if the third component of the input vector is missing, it is assumed to be zero. So, in the context of NumPy's numpy.cross() function, the vector $\mathbf{u} = (1,2)$ is assumed to be the vector $\mathbf{u} = (1,2,0)$ and the vector $\mathbf{v} = (4,5)$ is assumed to be the vector $\mathbf{v} = (4,5,0)$. So it is actually the cross product

$$ (\mathbf{i} + 2\mathbf{j} + 0\mathbf{k}) \times (4\mathbf{i} + 5\mathbf{j} + 0\mathbf{k}) $$

which results in

$$ -3\mathbf{k} $$

Notes

  • 1) Seymour Lipschutz and Marc Lipson, Linear Algebra. McGraw-Hill Companies, Inc, 2009. Chapter 1: Vectors in Rn and Cn, Spatial Vectors, p. 10, Ex. 1.9.