Numpy Cross Product

Numpy Cross Product

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.

Example 1: Cross Product of Numpy Arrays

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)
Run

Output

11
Run

Mathematical Proof

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

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.

Example 2: Cross Product of Numpy Arrays in 3D

In this example, we shall take two 2×2 Numpy Arrays 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)
Run

Output

[20 -4 -3]
Run

Mathematical Proof

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

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

Summary

In this tutorial of Python Examples, we learned how to find cross product of two vectors using Numpy cross() function, with the help of well detailed example programs.