dpnp.cross

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

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

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 must have 3 dimensions.

For full documentation refer to numpy.cross.

Parameters:
a{dpnp.ndarray, usm_ndarray}

First input array.

b{dpnp.ndarray, usm_ndarray}

Second input array.

axisaint, optional

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

Default: -1.

axisbint, optional

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

Default: -1.

axiscint, optional

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

Default: -1.

axis{int, None}, optional

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

Default: None.

Returns:
outdpnp.ndarray

Vector cross product(s).

Raises:
ValueError

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

See also

dpnp.linalg.cross

Array API compatible variation.

dpnp.inner

Inner product.

dpnp.outer

Outer product.

Examples

Vector cross-product.

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

Multiple vector cross-products. Note that the direction of the cross product vector is defined by the 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 axisc keyword.

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

Change the vector definition of x and y using axisa and 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]])