Single-vector geometry
Compute a perpendicular direction from two 3D vectors in geometry, physics, or graphics.
NumPy · Function
Computes the cross product of two vectors or arrays of vectors in R^3. By default, it treats the last axis of each input as the vector-component axis and returns the resulting vector cross product(s).
np.cross computes the cross product of vector components in R^3. It treats the final axis as the vector axis by default, supports arrays containing multiple vectors, and lets you override the vector and output axes with axis-related arguments.
numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)| Name | Type | Description |
|---|---|---|
a | array_like | Components of the first vector or vectors. |
b | array_like | Components of the second vector or vectors. |
axisa | int, optional | Axis of a defining its vectors; defaults to the last axis. |
axisb | int, optional | Axis of b defining its vectors; defaults to the last axis. |
axisc | int, optional | Axis of the result containing the cross-product vectors; defaults to the last axis. |
axis | int, optional | Shared axis of a, b, and the result defining the vectors and cross products. |
ndarray: vector cross product(s).
import numpy as np
a = np.array([1, 0, 0])
b = np.array([0, 1, 0])
print(np.cross(a, b)) # [0 0 1]Computes the vector perpendicular to the two supplied three-dimensional vectors.
Compute a perpendicular direction from two 3D vectors in geometry, physics, or graphics.
Apply the same cross-product operation across batches of vectors stored in arrays.
Work with arrays whose vector components are not on the final dimension.
It returns the cross product of two vectors in R^3, producing a vector perpendicular to both inputs. For array inputs, vectors are identified along the last axis by default.
Follow-up: How would you handle vectors stored along another dimension? Set axisa and axisb for the input vector axes and axisc for the output vector axis, or use axis when one axis applies to all three.
a and b are array-like inputs containing the first and second vector components. The function returns an ndarray containing the vector cross product or products.
Follow-up: What constraint applies to the vector axes in the current stable documentation? They must have three dimensions.
axisa, axisb, and axisc independently select the vector axes of a, b, and the result; each defaults to -1, meaning the last axis. The axis argument provides one shared axis selection for the inputs and output.
Follow-up: Why might explicit axis arguments be needed? A batch array may store vector components on a non-final dimension, so the defaults would identify the wrong axis.
numpy.linalg.crossnumpy.cross