Numpy Cross Product | Explained With Examples

In mathematics, cross-product is simply a binary operation that is performed on two vectors in 3-dimensional space. The final generated output vector will always be perpendicular to the input vector. Vector products are also referred to as cross products. They are denoted by “A” x ”B”.

Numpy is a popular Python library that provides the “np.cross()” function to calculate the cross product of two vector arrays. This write-up will provide a thorough review of the Python Numpy cross-product with numerous examples. This Python guide discusses the following terms:

So let’s begin this guide!

What is np.cross() in Python?

To calculate the cross product of the vector arrays, the “np.cross()” function of the “Numpy” library is used in Python. The syntax of the “np.cross()” function is shown below:

np.cross(a, b, axisa=- 1, axisb=- 1, axisc=- 1, axis=None)

In the above syntax:

  • The parameters “a” and “b” indicate the first and second vector arrays respectively.
  • The “axisa” parameter default value is the last axis or “-1”. This parameter defines the axis of the vector “a”.
  • The “axisb” parameter default value is the last axis or “-1”. This parameter defines the axis of the vector “b”.
  • The “axisc” parameter defines the axis of the third vector “c”, which contains the cross product of the given vector.
  • The parameter “axis” represents the first, second, and third vector, including the cross product.

Note: The value error will be raised if the dimension of the input vector is not equal to “2” or “3”.

Let’s start with the first example of the “np.cross()” function:

Example 1: Numpy Cross Product of 2×2 Matrix

In the example given below, the “np.cross()” function of the Numpy library is used to calculate the cross product of two-dimensional vectors.

Code:

import numpy as np

val_1 = np.array([5, 4])
val_2 = np.array([5, 12])

#using np.cross()
result = np.cross(val_1, val_2)

print(result)

In the above code:

  • The “numpy” library is imported at the beginning of the program.
  • Two vectors having 2-dimensions are initialized inside the parentheses of the “np.array()” function.
  • Two arrays are created using the “np.array()” function and stored in a variable named “val_1” and “val_2”.
  • The cross product of these arrays is calculated using the “np.cross()” function. The output array is calculated in the axis perpendicular to the input arrays.

Output:

The above snippet shows the Numpy cross product of given arrays.

In General Maths:

The above snippet shows the mathematical calculation of the Numpy cross product.

Example 2: Numpy Cross Product of 2×3 Matrix(2 Rows x 3 Columns)

In the example given below, the “np.cross()” calculates the cross product of the “2×3” matrix.

Code:

import numpy as np

val_1 = np.array([3,6,7])
val_2 = np.array([1,3,8])

# cross product of a 2X3 array
result = np.cross(val_1, val_2)
print(result)

In the above code:

  • The “numpy” library is imported at the beginning of the program.
  • Two vectors having 3-dimensions are initialized inside the parentheses of the “np.array()” function.
  • The “np.array()” is used to create an array, and the values of arrays are stored in a variable named “val_1” and “val_2”.
  • The “np.cross()” takes the variable named “val_1” and “val_2” as an argument and returns the cross product.

Note: The 2×3 Matrix means that the input vector contains 2 rows and 3 columns or two vectors containing three elements each.

Output:

The above output calculates the Numpy cross-product of the given arrays.

In General Maths:

The mathematical calculation of the “2×3” matrix is shown in the above output.

Example 3: Numpy Cross Product of 2-D Input Array

In the example given below, the Numpy cross product of 2-D arrays is calculated using the “np.cross()” function.

Code:

import numpy as np

val_1 = np.array([[3,6,7],[2,2,3]])
val_2 = np.array([[2,4,8],[6,7,8]])

# cross product of a 2D-array
result = np.cross(val_1, val_2)
print(result)

In the above code:

  • Two “2-D” arrays are created with the help of a function named “np.array()”.
  • The “np.cross()” calculates the cross product of the given 2-D arrays and returns the final output in 2-dimension.

Note: The “np.cross()” function also calculates the cross-product value of “3-d” arrays just like the “2-d” array.

Output:

In the above output, the Numpy cross product of 2-D arrays is calculated successfully.

That’s it from this Python Numpy guide!

Conclusion

To get the cross-product of the given vector arrays, the “np.cross()” function of the Numpy library is utilized in Python. The “np.cross()” function calculates the cross product of 1-dimension, 2-dimension, and 3-dimension vector arrays. The cross-product depends on the arrays being used in the program. The np.cross() function will return “1-D”, “2-D”, or “3-D” arrays depending on the input arrays.

The article provided a complete overview of the Python Numpy cross-product with multiple examples.