Cross Product - NumPy


NumPy - Cross Product of Arrays

Cross product of two vectors yield a vector that is perpendicular to the plane formed by the input vectors and its magnitude is proportional to the area spanned by the parallelogram formed by these input vectors.

In this tutorial, we shall learn how to compute cross product using numpy.cross() function.

Examples

1. Cross product of vectors in 2D plane

In this example, we shall take two points in XY plane as numpy arrays and find their cross product.

Python Program

import numpy as np

#initialize arrays
A = np.array([2, 3])
B = np.array([1, 7])

#compute cross product
output = np.cross(A, B)

print(output)

Output

11

Mathematical Proof

cross(A,B) = 2*7 - 3*1
           = 11

Consider that vectors [2,3] and [1,7] are in [X,Y] plane. Then the cross product [11] is in the axis perpendicular to [X,Y], say Z with magnitude 11.

2. Cross product of vectors in 3D plane

In this example, we shall take two NumPy Arrays, each of length 3 (representing a point in 3D space), and find their cross product.

Python Program

import numpy as np

#initialize arrays
A = np.array([2, 7, 4])
B = np.array([3, 9, 8])

#compute cross product
output = np.cross(A, B)

print(output)

Output

[20 -4 -3]

Mathematical Proof

cross(A,B) = [(7*8-9*4), -(2*8-4*3), (2*9-7*3)]
           = [20, -4, -3]

Output vector [20, -4, -3] is perpendicular to the plane formed by the input vectors [2, 7, 4], [3, 9, 8].

Summary

In this NumPy Tutorial, we learned how to find cross product of two vectors using numpy.cross() function, with the help of well detailed example programs.