numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersRaisesReturns
cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)

The cross product of a and b in $R^3$ is a vector perpendicular to both a and b. If a and b are arrays of vectors, the vectors are defined by the last axis of a and b by default, and these axes can have dimensions 2 or 3. Where the dimension of either a or b is 2, the third component of the input vector is assumed to be zero and the cross product calculated accordingly. In cases where both input vectors have dimension 2, the z-component of the cross product is returned.

Notes

versionadded

Supports full broadcasting of the inputs.

Parameters

a : array_like

Components of the first vector(s).

b : array_like

Components of the second vector(s).

axisa : int, optional

Axis of a that defines the vector(s). By default, the last axis.

axisb : int, optional

Axis of b that defines the vector(s). By default, the last axis.

axisc : int, optional

Axis of c containing the cross product vector(s). Ignored if both input vectors have dimension 2, as the return is scalar. By default, the last axis.

axis : int, optional

If defined, the axis of a, b and c that defines the vector(s) and cross product(s). Overrides :None:None:`axisa`, :None:None:`axisb` and :None:None:`axisc`.

Raises

ValueError

When the dimension of the vector(s) in a and/or b does not equal 2 or 3.

Returns

c : ndarray

Vector cross product(s).

Return the cross product of two (arrays of) vectors.

See Also

inner

Inner product

ix_

Construct index arrays.

outer

Outer product.

Examples

Vector cross-product.

>>> x = [1, 2, 3]
... y = [4, 5, 6]
... np.cross(x, y) array([-3, 6, -3])

One vector with dimension 2.

>>> x = [1, 2]
... y = [4, 5, 6]
... np.cross(x, y) array([12, -6, -3])

Equivalently:

>>> x = [1, 2, 0]
... y = [4, 5, 6]
... np.cross(x, y) array([12, -6, -3])

Both vectors with dimension 2.

>>> x = [1,2]
... y = [4,5]
... np.cross(x, y) array(-3)

Multiple vector cross-products. Note that the direction of the cross product vector is defined by the :None:None:`right-hand rule`.

>>> x = np.array([[1,2,3], [4,5,6]])
... y = np.array([[4,5,6], [1,2,3]])
... np.cross(x, y) array([[-3, 6, -3], [ 3, -6, 3]])

The orientation of c can be changed using the :None:None:`axisc` keyword.

>>> np.cross(x, y, axisc=0)
array([[-3,  3],
       [ 6, -6],
       [-3,  3]])

Change the vector definition of x and :None:None:`y` using :None:None:`axisa` and :None:None:`axisb`.

>>> x = np.array([[1,2,3], [4,5,6], [7, 8, 9]])
... y = np.array([[7, 8, 9], [4,5,6], [1,2,3]])
... np.cross(x, y) array([[ -6, 12, -6], [ 0, 0, 0], [ 6, -12, 6]])
>>> np.cross(x, y, axisa=0, axisb=0)
array([[-24,  48, -24],
       [-30,  60, -30],
       [-36,  72, -36]])
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/core/numeric.py#1479
type: <class 'function'>
Commit: