Numpy Cross Product – A Complete Guide

NUMPY Cross Product

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:

np.cross[M,N]

where M and N are array variables storing vector coordinates, but we can specify certain parameters as per our suitability and needs.

Numpy Crossa B Axisa 1 Axisb 1 Axisc 1 AxisNone
Numpy Cross Syntax

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.

cross_product_2x2.png
2X2 cross product

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.

Example code:

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]

cross-product-2x3matrix.png
2X3 matrix cross product

Here, the final output will be = (-7, 4, -1)

Example code:

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]

Example code:

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]

Conclusion

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.