A cross product is a mathematical tool to find the perpendicular vector component of two vector coordinates.
Suppose in a 3D space, there are two points:
- ‘a’ with coordinates (1,2,3)
- ‘b’ with coordinates (4,5,6).
So the vector component of the two coordinates will be the cross-product of the determinant of this vector matrix.
The cross product will be a non-commutative perpendicular vector product of the two matrix points.
Numpy Cross Product
The numpy.cross() is a mathematical function in the Python library that finds out the cross product between two arrays (Dimension of 2&3) and the result can be displayed with the print function.
Syntax of Numpy Cross Product
The basic syntax for implementing cross product is:
where M and N are array variables storing vector coordinates, but we can specify certain parameters as per our suitability and needs.
How To Calculate Cross Product Using Numpy Python?
Let’s look at a functional code over how cross-product is found in python.
1. Cross product of 2X2 matrix
Let’s suppose there are two arrays, X= [2,3], and Y= [4,3]. To find the vector product, we need to find the difference between the product of i1-j2 and i2-j1. The vector-product of two 2-Dimensional arrays will always be a single-dimensional integer.
The final result is (3*2) – (4*3) = -6.
Note: In this case, X and Y dimensions are defined while the z component is not there, so the final output is scalar.
import numpy as pr #initialize arrays X = pr.array([2, 3]) Y = pr.array([4, 3]) #calculating cross product vector_result= pr.cross(X,Y) print(vector_result)
2. Cross product of a 2X3 array
Let’s take two 3-Dimensional arrays and find the cross-product of it.
Let’s take X= [1,3,5] and Y= [1,2,1]
Here, the final output will be = (-7, 4, -1)
import numpy as pr #initialize arrays X = pr.array([1, 3, 5]) Y = pr.array([1, 2, 1]) #calculating cross product cross_product= pr.cross(X,Y) print(cross_product)
Note: The numpy cross-product supports matrices of 2 & 3 dimensions and any matrix with the higher dimension will throw error outputs.
Let’s take another example where, let’s suppose, M=[5,6,4] and N=[2,1]
import numpy as pr #initialize arrays X = pr.array([5, 6, 4]) Y = pr.array([2, 1]) #calculating cross product cross_product= pr.cross(X,Y) print(cross_product)
Here, the compiler automatically assigns the z component of array N as zero and calculates the final output based on that parameter.
Final result = [-4, 8, -7]
In this article we learned how to find the cross product of two vector arrays by using the python mathematical function ‘numpy.cross’. We also learned about different case scenarios and parameters through which numpy.cross can be implemented on different sets of array values.