cross#
- ivy.cross(x1, x2, /, *, axisa=-1, axisb=-1, axisc=-1, axis=None, out=None)[source]#
Return cross product of 3-element vectors.
If x1 and x2 are multi- dimensional arrays (i.e., both have a rank greater than 1), then the cross- product of each pair of corresponding 3-element vectors is independently computed.
- Parameters:
x1 (
Union
[Array
,NativeArray
]) – first input array. Should have a numeric data type.x2 (
Union
[Array
,NativeArray
]) –second input array. Must be compatible with
x1
for all non-compute axes. The size of the axis over which to compute the cross product must be the same size as the respective axis inx
. Should have a numeric data type.Note
The compute axis (dimension) must not be broadcasted.
axis (
Optional
[int
], default:None
) – the axis (dimension) of x1 and x2 containing the vectors for which to compute the cross product. Must be an integer on the interval``[-N, N)``, whereN
is the rank (number of dimensions) of the shape. If specified as a negative integer, the function must determine the axis along which to compute the cross product by counting backward from the last dimension (where-1
refers to the last dimension). By default, the function must compute the cross product over the last axis. Default:-1
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – an array containing the cross products. The returned array must have a data type determined by Type Promotion Rules.
This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Container
instances in place of any of the arguments.Examples
With
ivy.Array
inputs:>>> x = ivy.array([1., 0., 0.]) >>> y = ivy.array([0., 1., 0.]) >>> z = ivy.cross(x, y) >>> print(z) ivy.array([0., 0., 1.])
With
ivy.Container
inputs:>>> x = ivy.Container(a=ivy.array([5., 0., 0.]), ... b=ivy.array([0., 0., 2.])) >>> y = ivy.Container(a=ivy.array([0., 7., 0.]), ... b=ivy.array([3., 0., 0.])) >>> z = ivy.cross(x,y) >>> print(z) { a: ivy.array([0., 0., 35.]), b: ivy.array([0., 6., 0.]) }
With a combination of
ivy.Array
andivy.Container
inputs:>>> x = ivy.array([9., 0., 3.]) >>> y = ivy.Container(a=ivy.array([1., 1., 0.]), ... b=ivy.array([1., 0., 1.])) >>> z = ivy.cross(x,y) >>> print(z) { a: ivy.array([-3., 3., 9.]), b: ivy.array([0., -6., 0.]) }
- Array.cross(self, x2, /, *, axis=-1, out=None)[source]#
ivy.Array instance method variant of ivy.cross. This method simply wraps the function, and so the docstring for ivy.cross also applies to this method with minimal changes.
- Parameters:
self (
Array
) – first input array. Should have a numeric data type.x2 (
Union
[Array
,NativeArray
]) – second input array. Must be compatible withself
(see broadcasting). Should have a numeric data type.axis (
int
, default:-1
) – the axis (dimension) of x1 and x2 containing the vectors for which to compute (default: -1) the cross product.vIf set to -1, the function computes the cross product for vectors defined by the last axis (dimension). Default:-1
.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array containing the element-wise products. The returned array must have a data type determined by type-promotion.
Examples
With
ivy.Array
instance inputs:>>> x = ivy.array([1., 0., 0.]) >>> y = ivy.array([0., 1., 0.]) >>> z = x.cross(y) >>> print(z) ivy.array([0., 0., 1.])
- Container.cross(self, x2, /, *, axis=-1, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.cross. This method simply wraps the function, and so the docstring for ivy.cross also applies to this method with minimal changes.
- Parameters:
self (
Container
) – first input array. Should have a numeric data type.x2 (
Union
[Container
,Array
,NativeArray
]) – second input array. Must be compatible withself
(see broadcasting). Should have a numeric data type.axis (
Union
[int
,Container
], default:-1
) – the axis (dimension) of x1 and x2 containing the vectors for which to compute (default: -1) the cross product.vIf set to -1, the function computes the cross product for vectors defined by the last axis (dimension). Default:-1
.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
],Container
]], default:None
) – The key-chains to apply or not apply the method to. Default isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.prune_unapplied (
Union
[bool
,Container
], default:False
) – Whether to prune key_chains for which the function was not applied. Default isFalse
.map_sequences (
Union
[bool
,Container
], default:False
) – Whether to also map method to sequences (lists, tuples). Default isFalse
.out (
Optional
[Container
], default:None
) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container
- Returns:
ret – an array containing the element-wise products. The returned array must have a data type determined by type-promotion.
Examples
>>> x = ivy.Container(a=ivy.array([5., 0., 0.]), b=ivy.array([0., 0., 2.])) >>> y = ivy.Container(a=ivy.array([0., 7., 0.]), b=ivy.array([3., 0., 0.])) >>> z = x.cross(y) >>> print(z) { a: ivy.array([0., 0., 35.]), b: ivy.array([0., 6., 0.]) }