@@ -9544,10 +9544,82 @@ def outer(a: ArrayLike, b: ArrayLike, out: None = None) -> Array:
95449544 a , b = util .promote_dtypes (a , b )
95459545 return ravel (a )[:, None ] * ravel (b )[None , :]
95469546
9547- @ util . implements ( np . cross )
9547+
95489548@partial (jit , static_argnames = ('axisa' , 'axisb' , 'axisc' , 'axis' ))
95499549def cross (a , b , axisa : int = - 1 , axisb : int = - 1 , axisc : int = - 1 ,
95509550 axis : int | None = None ):
9551+ r"""Compute the (batched) cross product of two arrays.
9552+
9553+ JAX implementation of :func:`numpy.cross`.
9554+
9555+ This computes the 2-dimensional or 3-dimensional cross product,
9556+
9557+ .. math::
9558+
9559+ c = a \times b
9560+
9561+ In 3 dimensions, ``c`` is a length-3 array. In 2 dimensions, ``c`` is
9562+ a scalar.
9563+
9564+ Args:
9565+ a: N-dimensional array. ``a.shape[axisa]`` indicates the dimension of
9566+ the cross product, and must be 2 or 3.
9567+ b: N-dimensional array. Must have ``b.shape[axisb] == a.shape[axisb]``,
9568+ and other dimensions of ``a`` and ``b`` must be broadcast compatible.
9569+ axisa: specicy the axis of ``a`` along which to compute the cross product.
9570+ axisb: specicy the axis of ``b`` along which to compute the cross product.
9571+ axisc: specicy the axis of ``c`` along which the cross product result
9572+ will be stored.
9573+ axis: if specified, this overrides ``axisa``, ``axisb``, and ``axisc``
9574+ with a single value.
9575+
9576+ Returns:
9577+ The array ``c`` containing the (batched) cross product of ``a`` and ``b``
9578+ along the specified axes.
9579+
9580+ See also:
9581+ - :func:`jax.numpy.linalg.cross`: an array API compatible function for
9582+ computing cross products over 3-vectors.
9583+
9584+ Examples:
9585+ A 2-dimensional cross product returns a scalar:
9586+
9587+ >>> a = jnp.array([1, 2])
9588+ >>> b = jnp.array([3, 4])
9589+ >>> jnp.cross(a, b)
9590+ Array(-2, dtype=int32)
9591+
9592+ A 3-dimensional cross product returns a length-3 vector:
9593+
9594+ >>> a = jnp.array([1, 2, 3])
9595+ >>> b = jnp.array([4, 5, 6])
9596+ >>> jnp.cross(a, b)
9597+ Array([-3, 6, -3], dtype=int32)
9598+
9599+ With multi-dimensional inputs, the cross-product is computed along
9600+ the last axis by default. Here's a batched 3-dimensional cross
9601+ product, operating on the rows of the inputs:
9602+
9603+ >>> a = jnp.array([[1, 2, 3],
9604+ ... [3, 4, 3]])
9605+ >>> b = jnp.array([[2, 3, 2],
9606+ ... [4, 5, 6]])
9607+ >>> jnp.cross(a, b)
9608+ Array([[-5, 4, -1],
9609+ [ 9, -6, -1]], dtype=int32)
9610+
9611+ Specifying axis=0 makes this a batched 2-dimensional cross product,
9612+ operating on the columns of the inputs:
9613+
9614+ >>> jnp.cross(a, b, axis=0)
9615+ Array([-2, -2, 12], dtype=int32)
9616+
9617+ Equivalently, we can independently specify the axis of the inputs ``a``
9618+ and ``b`` and the output ``c``:
9619+
9620+ >>> jnp.cross(a, b, axisa=0, axisb=0, axisc=0)
9621+ Array([-2, -2, 12], dtype=int32)
9622+ """
95519623 # TODO(jakevdp): NumPy 2.0 deprecates 2D inputs. Follow suit here.
95529624 util .check_arraylike ("cross" , a , b )
95539625 if axis is not None :
0 commit comments