numpy.cross#

numpy.cross(a,b,axisa=-1,axisb=-1,axisc=-1,axis=None)[source]#

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

The cross product ofa andb in\(R^3\) is a vector perpendicularto botha andb. Ifa andb are arrays of vectors, the vectorsare defined by the last axis ofa andb by default, and these axesmust have 3 dimensions.

Parameters:
aarray_like

Components of the first vector(s).

barray_like

Components of the second vector(s).

axisaint, optional

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

axisbint, optional

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

axiscint, optional

Axis ofc containing the cross product vector(s). By default, the last axis.

axisint, optional

If defined, the axis ofa,b andc that defines the vector(s)and cross product(s). Overridesaxisa,axisb andaxisc.

Returns:
cndarray

Vector cross product(s).

Raises:
ValueError

When the dimension of the vector(s) ina orb does not equal 3.

See also

inner

Inner product

outer

Outer product.

linalg.cross

An Array API compatible variation ofnp.cross.

ix_

Construct index arrays.

Notes

Supports full broadcasting of the inputs.

Examples

Vector cross-product.

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

One vector with dimension 2.

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

Both vectors with dimension 2.

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

Multiple vector cross-products. Note that the direction of the crossproduct vector is defined by theright-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 ofc can be changed using theaxisc keyword.

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

Change the vector definition ofx andy usingaxisa andaxisb.

>>>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]])
On this page