NumPy cross()

The numpy.cross() function computes the cross product of two vectors or arrays of vectors. The cross product of two 3D vectors results in a third vector perpendicular to both. For 2D vectors, the function returns the scalar z-component of the cross product.

Syntax

</>
Copy
numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)

Parameters

ParameterTypeDescription
aarray_likeFirst input vector or array of vectors.
barray_likeSecond input vector or array of vectors.
axisaint, optionalSpecifies the axis in a that represents the vector(s). Default is the last axis.
axisbint, optionalSpecifies the axis in b that represents the vector(s). Default is the last axis.
axiscint, optionalSpecifies the axis in the output array where the result should be stored. Ignored for 2D vectors.
axisint, optionalIf provided, overrides axisa, axisb, and axisc. Specifies the axis along which vectors are defined.

Return Value

Returns an array representing the cross product of the input vectors. If the input vectors are 3D, the result is a 3D vector. If both vectors are 2D, the function returns a scalar value representing the z-component of the cross product.


Examples

1. Computing the Cross Product of Two 3D Vectors

This example calculates the cross product of two 3D vectors.

</>
Copy
import numpy as np

# Define two 3D vectors
vector_a = np.array([1, 2, 3])
vector_b = np.array([4, 5, 6])

# Compute the cross product
result = np.cross(vector_a, vector_b)

# Print the result
print("Cross product of vector_a and vector_b:", result)

Output:

Cross product of vector_a and vector_b: [-3  6 -3]

2. Computing the Cross Product of Two 2D Vectors

For 2D vectors, the function returns the scalar z-component of the cross product.

</>
Copy
import numpy as np

# Define two 2D vectors
vector_a = np.array([1, 2])
vector_b = np.array([3, 4])

# Compute the cross product (scalar z-component)
result = np.cross(vector_a, vector_b)

# Print the result
print("Z-component of the cross product:", result)

Output:

Z-component of the cross product: -2

You may get a DeprecationWarning: Arrays of 2-dimensional vectors are deprecated. Use arrays of 3-dimensional vectors instead. (deprecated in NumPy 2.0).

Solutions to handle “DeprecationWarning: Arrays of 2-dimensional vectors are deprecated”

3. Computing the Cross Product for Arrays of Vectors

Computing the cross product for multiple vectors stored in arrays.

</>
Copy
import numpy as np

# Define arrays of 3D vectors
vectors_a = np.array([[1, 2, 3], [4, 5, 6]])
vectors_b = np.array([[7, 8, 9], [10, 11, 12]])

# Compute the cross product for each pair of vectors
result = np.cross(vectors_a, vectors_b)

# Print the results
print("Cross products of vector arrays:")
print(result)

Output:

Cross products of vector arrays:
[[-6 12 -6]
 [-6 12 -6]]

4. Using the axis Parameter

Setting the axis parameter to define the vector orientation in multidimensional arrays.

</>
Copy
import numpy as np

# Define 3D vectors in a multidimensional array
vectors_a = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
vectors_b = np.array([[[3, 2, 1], [6, 5, 4]], [[9, 8, 7], [12, 11, 10]]])

# Compute the cross product along a specific axis
result = np.cross(vectors_a, vectors_b, axis=2)

# Print the results
print("Cross product along axis=2:")
print(result)

Output:

Cross product along axis=2:
[[[ -4   8  -4]
  [-10  20 -10]]

 [[-16  32 -16]
  [-22  44 -22]]]

Using the axis parameter helps control how vectors are interpreted in multi-dimensional arrays, enabling efficient computation of cross products along different orientations.