Reference: Official Document of Numpy
numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)
Return the cross product of two (arrays of) vectors.
The cross product of a
and b
in :math: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.
计算两个向量(向量数组)的叉乘。叉乘返回的数组既垂直于a
,又垂直于b
。 如果a
,b
是向量数组,则向量在最后一维定义。该维度可以为2,也可以为3. 为2的时候会自动将第三个分量视作0补充进去计算。
a
that defines the vector(s). By default, the last axis.b
that defines the vector(s). By default, the last axis.c
containing the cross product vector(s). Ignored ifa
, b
and c
that defines the vector(s)axisa
, axisb
and axisc
.axisa, axisb, axisc 分别指定两个输入和输出c
的向量所在的维度。而axis则可以覆盖前三个参数,为全局指定向量所在维度。
a
and/or b
does not当向量所在axis的dimension不为2或者3时,raise ValueError.
… versionadded:: 1.9.0
Supports full broadcasting of the inputs.
支持广播。
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 `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]])