Cookie Policy

We use cookies to operate this website, improve usability, personalize your experience, and improve our marketing. Privacy Policy.

By clicking "Accept" or further use of this website, you agree to allow cookies.

Accept
Learn Machine Learning by Doing Learn Now
You are reading glossary
Fatih-Karabiber-profile-photo.jpg
Author: Fatih Karabiber
Ph.D. in Computer Engineering, Data Scientist
Brendan Martin
Author: Brendan Martin
Founder of LearnDataSci

Cross Product: Step-by-Step Calculations and Python Examples

LearnDataSci is reader-supported. When you purchase through links on our site, earned commissions help support our team of writers, researchers, and designers at no extra cost to you.

What is a cross product?

In three-dimensional space, the cross product between two vectors, $\vec{a}$ and $\vec{b}$, produces a third vector, $\vec{c}$, perpendicular to both $\vec{a}$ and $\vec{b}$. Geometrically, the magnitude of $\vec{c}$ is equal to the area of a parallelogram whose sides are $\vec{a}$ and $\vec{b}$(see visualization below).

Cross Product vs. Dot Product

The result of a cross product is a vector, whereas the result of the dot product is a scalar (a number).

Mathematical definition

The cross product operation is denoted by the '$\times$' symbol. Below, we'll describe both the coordinate definition and geometric definition of the cross product.

Coordinate definition

The cross product of the vectors $ \vec{a} = a_x + a_y + a_z$ and $ \vec{b} = b_x + b_y + b_z$ is derived from the determinant of the 3x3 matrix:

$$ \begin{align} \vec{a} \times \vec{b} &= \begin{vmatrix} i&j&k \\ a_x&a_y&a_z \\ b_x&b_y&b_z \end{vmatrix} \\[1em] &= \begin{vmatrix}a_y&a_z \\ b_y&b_z\end{vmatrix} i - \begin{vmatrix}a_x&a_z \\ b_x&b_z\end{vmatrix} j + \begin{vmatrix}a_x&a_y \\ b_x&b_y\end{vmatrix} k \\[1em] &= (a_yb_z-a_zb_y)\vec{i} + (a_zb_x-a_xb_z)\vec{j} + (a_xb_y-a_yb_x)\vec{k} \\[1em] \end{align} $$

Where $\vec{i} = (1, 0, 0)$, $\vec{j} = (0, 1, 0)$, and $\vec{k} = (0, 0, 1)$ are the standard unit vectors that point parallel to the x-axis, y-axis, and z-axis respectively.

Thus, the general formula for the cross product between two vectors is:

$$ \vec{a} \times \vec{b} = (a_yb_z-a_zb_y)\vec{i} + (a_zb_x-a_xb_z)\vec{j} + (a_xb_y-a_yb_x)\vec{k} $$

Example by hand

Given the two vectors $\vec{a} = (1, 2, 3)$ and $\vec{b} = (4, 5, 6)$, the cross product is as follows:

\begin{align} \vec{a} \times \vec{b} &= \begin{vmatrix} i&j&k \\ 1&2&3 \\ 4&5&6 \end{vmatrix} \\[1em] &= (2\times6-3\times5)\vec{i} + (3\times4-1\times6)\vec{j} + (1\times5-2\times4)\vec{k} \\[1em] &= -3\vec{i} + 6\vec{j} -3\vec{k} \end{align}

So, the cross product of $\vec{a}$ and $\vec{b}$ is a new vector with coordinates $(-3, 6, -3)$

Geometric definition

Given two vectors $\vec{a}$ and $\vec{b}$, the cross product is defined as:

$$ \vec{a} \times \vec{b} = \|\vec{a}\|\|\vec{b}\| \sin \theta \hat{n} $$

Where:

  • $\|\vec{a}\|$ and $\|\vec{b}\|$ represent the magnitudes (L2 norm) of the vectors, meaning the root of the squared elements. For example, $\|\vec{a}\| = \sqrt{a_1^2 + a_2^2 + a_3^2}$
  • $\theta$ (theta) is the angle between the vectors.
  • $\hat{n}$ (n hat) is the unit vector perpendicular to the given vectors.

Visualization

The following is an interactive geometric visualization of the cross product. Drag the vector points around to see how the cross product is affected.

The right-hand rule

Source: Wikipedia

The right-hand rule is used to find the direction of the cross product's resulting vector. If you point your index finger towards the vector $\vec{a}$ and your middle finger towards the vector $\vec{b}$, your thumb will point you in the direction of $\vec{a} \times \vec{b}$.

When is the cross product zero?

The cross product of two vectors is zero when:

  • They have the same direction
  • They have the exact opposite direction
  • Either vector has zero length

Algebraic properties

Cross products are not commutative: $\vec{a} \times \vec{b} \neq \vec{b} \times \vec{a}$, but $ \vec{a} \times \vec{b} = - (\vec{b} \times \vec{a}) $.

The cross product is distributive over addition: $ \vec{a} \times (\vec{b} + \vec{c}) = \vec{a} \times \vec{b} + \vec{a} \times \vec{c}$

Python Example

First, we will create our own function for calculating a cross product, then we'll use the cross function from Numpy as an alternative.

Let's create two vectors as simple Python lists:

a = [1, 2, 3]
b = [4, 5, 6]

And now using the determinant formula, we can make a function that calculates the cross product:

def cross_product(a, b):
    c = [a[1] * b[2] - a[2] * b[1],
         a[2] * b[0] - a[0] * b[2],
         a[0] * b[1] - a[1] * b[0]]
    return c

Passing both vectors in, we can see we've calculated the cross product calculated successfully:

c = cross_product(a, b)
print('The cross product is:\n', c)
Out:
The cross product is:
 [-3, 6, -3]

Instead of writing your own function, you can simply use the cross function from Numpy.

Make two numpy vectors using np.array and then call np.cross:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

c = np.cross(a, b)
print('The cross product using Numpy is:\n', c)
Out:
The cross product using Numpy is:
 [-3  6 -3]

Showing algebraic properties

We can easily use Numpy to show the algebraic properties listed above.

Showing cross products are not commutative:

a_x_b = np.cross(a, b)
b_x_a = np.cross(b, a)

print(f"a x b = {a_x_b}")
print(f"b x c = {b_x_a}")

print("Commutative? ", np.array_equal(a_x_b.all, b_x_a.all))
Out:
a x b = [-3  6 -3]
b x c = [ 3 -6  3]
Commutative?  False

Showing cross products are distributive over addition:

undistributed = np.cross(a, b + c)
distributed = np.cross(a, b) + np.cross(a, c)

print(f"a x (b + c) = {undistributed}")
print(f"a x b + a x c = {distributed}")

print("Distributive over addition? ", np.array_equal(undistributed, distributed))
Out:
a x (b + c) = [-27   0   9]
a x b + a x c = [-27   0   9]
Distributive over addition?  True

Meet the Authors

Fatih-Karabiber-profile-photo.jpg

Associate Professor of Computer Engineering. Author/co-author of over 30 journal publications. Instructor of graduate/undergraduate courses. Supervisor of Graduate thesis. Consultant to IT Companies.

Brendan Martin

Chief Editor at LearnDataSci and Software Engineer

Get updates in your inbox

Join over 7,500 data science learners.