flopscope.numpy.cross
fnp.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[flopscope source][numpy source]
Return the cross product of two (arrays of) vectors.
Adapted from NumPy docs np.cross
Cross product of two 3-D vectors.
The cross product of a and b in 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.
Parameters
- a:array_like
Components of the first vector(s).
- b:array_like
Components of the second vector(s).
- axisa:int, optional
Axis of
athat defines the vector(s). By default, the last axis.- axisb:int, optional
Axis of
bthat defines the vector(s). By default, the last axis.- axisc:int, optional
Axis of
ccontaining 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,bandcthat defines the vector(s) and cross product(s). Overridesaxisa,axisbandaxisc.
Returns
- c:ndarray
Vector cross product(s).
Raises
- :ValueError
When the dimension of the vector(s) in
aand/orbdoes not equal 2 or 3.
See also
- we.flops.inner Inner product
- we.flops.outer Outer product.
- we.flops.linalg.cross An Array API compatible variation of flops.cross, which accepts (arrays of) 3-element vectors only.
- we.flops.ix_ Construct index arrays.
Notes
Supports full broadcasting of the inputs.
Dimension-2 input arrays were deprecated in 2.0.0. If you do need this functionality, you can use:
def cross2d(x, y):
return x[..., 0] * y[..., 1] - x[..., 1] * y[..., 0]Examples
Vector cross-product.
>>> import flopscope.numpy as fnp
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> flops.cross(x, y)
array([-3, 6, -3])One vector with dimension 2.
>>> x = [1, 2]
>>> y = [4, 5, 6]
>>> flops.cross(x, y)
array([12, -6, -3])Equivalently:
>>> x = [1, 2, 0]
>>> y = [4, 5, 6]
>>> flops.cross(x, y)
array([12, -6, -3])Both vectors with dimension 2.
>>> x = [1,2]
>>> y = [4,5]
>>> flops.cross(x, y)
array(-3)Multiple vector cross-products. Note that the direction of the cross product vector is defined by the right-hand rule.
>>> x = flops.array([[1,2,3], [4,5,6]])
>>> y = flops.array([[4,5,6], [1,2,3]])
>>> flops.cross(x, y)
array([[-3, 6, -3],
[ 3, -6, 3]])The orientation of c can be changed using the axisc keyword.
>>> flops.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 = flops.array([[1,2,3], [4,5,6], [7, 8, 9]])
>>> y = flops.array([[7, 8, 9], [4,5,6], [1,2,3]])
>>> flops.cross(x, y)
array([[ -6, 12, -6],
[ 0, 0, 0],
[ 6, -12, 6]])
>>> flops.cross(x, y, axisa=0, axisb=0)
array([[-24, 48, -24],
[-30, 60, -30],
[-36, 72, -36]])