Updated April 1, 2023
Introduction to NumPy Cross Product
The cross-function provided by NumPy to perform cross-product of vectors is called the NumPy cross-product function. And a vector that is at right angles to the plane that is formed by the input vectors, is produced by performing the cross product of the two vectors whose magnitude and the area created by the parallelogram created by these input vectors are proportional. To compute the cross product of the given two arrays, those two arrays are passed as the parameters to the cross product function. This cross-product function can be performed on three-dimensional arrays and two-dimensional arrays as well.
Syntax:
numPy.cross(array1, array2)
Where array1 is the first array and array2 is the second array whose cross-product operation must be performed.
Working of Cross Product Function
- The cross-function to perform cross-product of vectors is called the NumPy cross-product function.
- A vector that is at right angles to the plane that is formed by the input vectors, is produced by performing the cross product of the two vectors whose magnitude and the area created by the parallelogram created by these input vectors are proportional.
- To compute the cross product of the given two arrays, those two arrays are passed as the parameters to the cross product function.
- The cross-product function can be performed on three-dimensional arrays and two-dimensional arrays as well.
Examples
Given below are the examples mentioned:
Example #1
Python program to demonstrate function to find the cross product of the given two arrays and display the result.
Code:
#Importing the package NumPy to be able to use cross production function
import numpy as num
#creating two arrays and storing it in variables called array1 and array2 respectively
array1 = num.array([1, 2])
array2 = num.array([3, 4])
#cross product of the given two arrays is performed using cross product function
resultingarray = num.cross(array1, array2)
print("The result after performing the cross product on the given two arrays are:")
#displaying the resultant array as the output on the screen
print(resultingarray)
Output:
In the above program, we are importing the package NumPy to make use of the cross-product function and array function. The two arrays are created using the array function and stored in the variables called array1 and array2 respectively. Then the cross-product function is used on the created two arrays and their result is stored in the variable called resultingarray. Then the result is displayed as the output on the screen.
Example #2
Python program to demonstrate function to find the cross product of the given two arrays and display the result.
Code:
#Importing the package NumPy to be able to use cross production function
import numpy as num
#creating two arrays and storing it in variables called array1 and array2 respectively
array1 = num.array([2, 3, 4])
array2 = num.array([5, 6, 7])
#cross product of the given two arrays is performed using cross product function
resultingarray = num.cross(array1, array2)
print("The result after performing the cross product on the given two arrays are:")
#displaying the resultant array as the output on the screen
print(resultingarray)
Output:
In the above program, we are importing the package NumPy to make use of the cross product function and array function. The two arrays are created using array function and stored in the variables called array1 and array2 respectively. Then the cross product function is used on the created two arrays and their result is stored in the variable called resultingarray. Then the result is displayed as the output on the screen.
Example #3
Python program to demonstrate function to find the cross product of the given two arrays and display the result.
Code:
#Importing the package NumPy to be able to use cross production function
import numpy as num
#creating two arrays and storing it in variables called array1 and array2 respectively
array1 = num.array([10, 9, 8])
array2 = num.array([4, 3, 2])
#cross product of the given two arrays is performed using cross product function
resultingarray = num.cross(array1, array2)
print("The result after performing the cross product on the given two arrays are:")
#displaying the resultant array as the output on the screen
print(resultingarray)
Output:
In the above program, we are importing the package NumPy to make use of the cross-product function and array function. The two arrays are created using the array function and stored in the variables called array1 and array2 respectively. Then the cross-product function is used on the created two arrays and their result is stored in the variable called resultingarray. Then the result is displayed as the output on the screen.
Example #4
Python program to demonstrate function to find the cross product of the given two arrays and display the result.
Code:
#Importing the package NumPy to be able to use cross production function
import numpy as num
#creating two arrays and storing it in variables called array1 and array2 respectively
array1 = num.array([6, 4])
array2 = num.array([2, 0])
#cross product of the given two arrays is performed using cross product function
resultingarray = num.cross(array1, array2)
print("The result after performing the cross product on the given two arrays are:")
#displaying the resultant array as the output on the screen
print(resultingarray)
Output:
In the above program, we are importing the package NumPy to make use of the cross-product function and array function. The two arrays are created using the array function and stored in the variables called array1 and array2 respectively. Then the cross-product function is used on the created two arrays and their result is stored in the variable called resultingarray. Then the result is displayed as the output on the screen.
Recommended Articles
This is a guide to NumPy Cross Product. Here we discuss the introduction, working of cross-product function in NumPy, and examples respectively. You may also have a look at the following articles to learn more –