In NumPy, you can compute the cross product of two given vector arrays using the numpy.cross()
function. The cross product is a vector that is perpendicular to the plane formed by two other vectors. 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.
# Quick examples of cross product
# 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 specified, it overridesaxisa
,axisb
, andaxisc
. It determines the axis along which the cross-product is computed.
2.2 Return Value of cross()
It returns the cross-product of two arrays of vectors.
3. Use numpy.cross() Function
You can use the two arrays, arr
, and arr1
to cross vector product, you 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.
In the below example, arr
and arr1
are 1-D arrays representing vectors, and np.cross()
is used to compute their cross-product, resulting in the 1-D array 6.
# Import numpy
import numpy as np
# Creating input array
arr = np.array([2, 4])
print("First array:",arr)
arr1 = np.array([1, 5])
print("Second array:",arr1)
# Use numpy.cross() function
# To cross product of 2X2 matrix
arr2 = np.cross(arr, arr1)
print("Cross product:",arr2)
Yields below output.
Mathematical Proof.
# Output:
# Mathematical Proof
cross(arr, arr1) = 2*5 - 4*1
= 6
4. Cross Product of a 2X3 Matrix
If you have a 2×3 matrix, you can treat each row of the matrix as a vector and compute the cross-product between these vectors. 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("Cross product:",arr2)
# Output:
# Cross product: [-22 14 -2]
Yields below output.
# Output:
# Mathematical Proof
cross(arr,arr1) = [(4*2-5*6), -(2*2-6*3), (2*5-4*3)]
= [-22, -14, -2]
This above program extracts two vectors from a 2×3 matrix (arr
, arr1
) and computes their cross product using numpy.cross()
. The result will be the cross product of the two vectors.
Alternatively, you can also use it to compute the cross product, both vectors must have the same number of elements, typically 3 for 3D vectors. If you are working with 2D vectors, you can pad them with zeros to make them 3D. For example, arr
is a 2D vector padded with a zero to make it 3D, and arr1
is a 3D vector. The numpy.cross()
function is then used to compute their cross product.
import numpy as np
# Creating input arrays
arr = np.array([2, 4, 0])
arr1 = np.array([3, 5, 2])
# One Vector 2D
arr2 = np.cross(arr, arr1)
print("Cross product:",arr2)
# Output:
# Cross product: [ 8 -4 -2]
# Output:
# Cross product: [ 8 -4 -2]
5. Get Cross Product of NumPy Arrays in 2D
The np.cross()
function is used to to compute the cross product of vectors represented by 2D NumPy arrays. The cross()
function takes arr
and arr1
as arguments and returns the cross product of two arrays of vectors. Here, two 2D NumPy arrays (arr
and arr1
) are created, each representing a set of 3D vectors. The first row in each array represents the first 3D vector, and the second row represents the second 3D vector.
The np.cross()
function is used to compute the cross product between corresponding vectors in the arrays arr
and arr1
. The resulting array, arr2
, contains the cross products of the pairs 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("Cross product:\n",arr2)
The following calculation is shown a 2-D matrix cross-product.
# Output:
# Cross product:
# [[-2 4 -2]
# [ 2 -4 2]]
Frequently Asked Questions
In NumPy, the cross product is a mathematical operation that takes two vectors as input and produces a third vector that is perpendicular to the plane formed by the input vectors. This operation is available through the numpy.cross()
function.
The numpy.cross()
function takes two arrays as input and returns their cross-product.
The numpy.cross()
function can handle 1D and 2D arrays. If the input vectors are 2D arrays, the cross-product is computed along the last axis by default. You can specify the axis using the axisa
and axisb
parameters.
You can specify the axis along which the cross-product is computed using the axisa
and axisb
parameters. The default is to compute the cross-product along the last axis.
The numpy.cross()
function is designed for the cross-product of two vectors. If you want to compute the cross-product of more than two vectors, you can apply the function iteratively or use a loop.
The cross-product is used to find a vector that is perpendicular to the plane formed by two other vectors. It has applications in physics, computer graphics, and various engineering fields.
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!!
Related Articles
- How To Compute Average Of NumPy Array?
- Get the cumulative sum of numpy
- How to Check NumPy Array Equal
- How to Use NumPy Argsort() in Python
- How to use NumPy divide() Function
- NumPy broadcast() Function in Python
- NumPy Norm of Vector
- How to Use NumPy log() in Python?
- NumPy Inverse Matrix in Python
- NumPy Empty Array With Examples
- How to use NumPy vstack() in Python
- How to get Diagonal of NumPy Array Using diag()