Cross Product in NumPy | Python

NumPy cross() function in Python is used to compute the cross-product of two given vector arrays. In other words. A cross product is a mathematical tool to get the perpendicular vector component of two vector coordinates. In this article, I will explain how to use numpy.cross() function and get the cross product of two arrays of vectors.

1. Quick Examples of Cross Product

If you are in a hurry, below are some quick examples of how to use NumPy cross product in Python.


# Below are the quick examples

# Example 1: Use numpy.cross() function
arr = np.array([2, 4])
arr1 = np.array([1, 5])
arr2 = np.cross(arr, arr1)

# Example 2: cross product of a 2X3 array
arr = np.array([2,4,6])
arr1 = np.array([3,5,2])
arr2 = np.cross(arr, arr1)

# Example 3: One Vector 2D
arr = np.array([2,4])
arr1 = np.array([3,5,2])
arr2 = np.cross(arr, arr1)

# Example 4: Get cross product of numpy arrays in 2D
arr =  np.array([[2,4,6], [3,5,7]])
arr1 = np.array([[3,5,7], [2,4,6]])
arr2 = np.cross(arr, arr1)

2. Syntax of NumPy cross()

Following is the syntax to create cross() function.


 # Syntax of numpy.cross()
numpy.cross(arr, arr1, axisa=- 1, axisb=- 1, axisc=- 1, axis=None)

2.1 Parameters of NumPy cross()

Following are the parameters of the NumPy cross().

  • arr – This is an array_like component of the first vector(s).
  • arr1 – This is an array_like component of the second vector(s).
  • axisa – This is the axis of arr that defines the vector(s). Its default value is the last axis.
  • axisb – This is the Axis of arr1 that defines the vector(s). Its default value is the last axis.
  • axisc – This is the axis of the third vector that contains the cross-product vector. Ignored if both input vectors have two dimensions, as the return is scalar.
  • axis – If defined, the axis of the first, second and third that defines the vector(s) and cross products. It overrides axisa, axisb and axisc.

2.2 Return Value of cross()

It returns the cross product of two arrays of vectors.

3. Use numpy.cross() Function

Using two arrays, arr= [2,4], and arr1= [1,5] to cross vector product, we need to get 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 (2*5)–(4*1) = 6.


import numpy as np

# creating input array
arr = np.array([2, 4])
arr1 = np.array([1, 5])

# Use numpy.cross() function to cross product of 2X2 matrix
arr2 = np.cross(arr, arr1)
print(arr2)

# Output
# 6

Yields below output.


# Mathematical Proof
cross(arr, arr1) = 2*5 - 4*1
                 = 6

4. Cross Product of a 2X3 Matrix

To compute the cross product of two vectors, use the numpy.cross() function. it will return the cross product of two arrays of vectors. Let’s take an example,


import numpy as np

# creating input array
arr = np.array([2,4,6])
arr1 = np.array([3,5,2])

# cross product of a 2X3 array
arr2 = np.cross(arr, arr1)
print(arr2)

# Output
# [-22  14  -2]

Yields below output.


# Mathematical Proof
cross(arr,arr1) = [(4*2-5*6), -(2*2-6*3), (2*5-4*3)]
                = [-22, -14, -2]

One vector with two dimensions.


import numpy as np

# creating input array
arr = np.array([2,4])
arr1 = np.array([3,5,2])

# One Vector 2D
arr2 = np.cross(arr, arr1)
print(arr2)

# Output
# [ 8 -4 -2]

5. Get Cross Product of NumPy Arrays in 2D

The np.cross() function is used to find out the cross product of two arrays. The cross() function takes arr and arr1 as arguments and returns the cross product of two arrays of vectors.


import numpy as np

# creating an 2D input array
arr =  np.array([[2,4,6], [3,5,7]])
arr1 = np.array([[3,5,7], [2,4,6]])

# Get cross product of numpy arrays in 2D
arr2 = np.cross(arr, arr1)
print(arr2)

The following calculation is shown a 2-D matrix cross product.


# [[-2  4 -2]
# [ 2 -4  2]]

6. Conclusion

In this article, I have explained how to use Python numpy.cross() function to perform cross product of two given vector arrays with examples.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing Cross Product in NumPy | Python