The cross product, also known as the vector product, is a mathematical operation that takes two vectors and produces another vector. It is a fundamental concept in linear algebra and is used in various applications in deep learning, computer graphics, and physics. In this chapter, we will explain what the cross product is, provide an example, and demonstrate how to compute it using NumPy.
Given two vectors, $ \mathbf{a} $ and $ \mathbf{b} $, the cross product $ \mathbf{c} $ is defined as follows:
$ \mathbf{c} = \mathbf{a} \times \mathbf{b} $
Where:
– $ \mathbf{c} $ is the resulting vector.
– $ \mathbf{a} $ and $ \mathbf{b} $ are the input vectors.
– $ \times $ represents the cross product operation.
The formula to compute the cross product of two 3-dimensional vectors $ \mathbf{a} $ and $ \mathbf{b} $ is given by:
$ \mathbf{c} = \begin{vmatrix} \mathbf{i} & \mathbf{j} & \mathbf{k} \\ a_x & a_y & a_z \\ b_x & b_y & b_z \end{vmatrix} $
Where:
– $ \mathbf{i} $, $ \mathbf{j} $, and $ \mathbf{k} $ are the unit vectors along the x, y, and z axes.
– $ a_x, a_y, a_z $ are the components of vector $ \mathbf{a} $.
– $ b_x, b_y, b_z $ are the components of vector $ \mathbf{b} $.
Let’s compute the cross product of two vectors:
$ \mathbf{a} = [2, 3, -1] $ and $ \mathbf{b} = [4, -2, 5] $.
Using the formula, we have:
$ \mathbf{c} = \begin{vmatrix} \mathbf{i} & \mathbf{j} & \mathbf{k} \\ 2 & 3 & -1 \\ 4 & -2 & 5 \end{vmatrix} $
Now, let’s calculate $ \mathbf{c} $:
$ \mathbf{c}_x = (3 \times 5) – (-1 \times -2) = 15 – 2 = 13 $
$ \mathbf{c}_y = (-1 \times 4) – (2 \times 5) = -4 – 10 = -14 $
$ \mathbf{c}_z = (2 \times -2) – (3 \times 4) = -4 – 12 = -16 $
So, the cross product $ \mathbf{c} $ is:
$ \mathbf{c} = [13, -14, -16] $
You can easily compute the cross product of two vectors using NumPy in Python. Here’s the code for the example above:
import numpy as np
# Define the vectors
a = np.array([2, 3, -1])
b = np.array([4, -2, 5])
# Compute the cross product
c = np.cross(a, b)
# Print the result
print("Cross product c =", c)