# xarray.cross

### xarray.cross(a, b, , dim)[[source]](https://github.com/pydata/xarray/blob/main/xarray/computation/computation.py#L308-L472)

Compute 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. The vectors in a and b are
defined by the values along the dimension dim and can have sizes
1, 2 or 3. Where the size of either a or b is
1 or 2, the remaining components 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, b** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray) or [`Variable`](xarray.Variable.html.md#xarray.Variable)) – Components of the first and second vector(s).
  * **dim** ([hashable](../user-guide/terminology.html.md#term-name)) – The dimension along which the cross product will be computed.
    Must be available in both vectors.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('fe836ae5-217b-4bcb-a3fd-e5572db6af6a','c95b0ab9-8d9f-4fcd-ae8a-1a308bea893b','b19a1ec0-505f-4fb0-a6dd-e2a3f9904c32','../lite/tree/../notebooks/index.html?path=d473bdf1_6add_421a_a2a9_a1b75b9ad62e.ipynb','None')">Try it in your browser!</button></div>

Vector cross-product with 3 dimensions:

```pycon
>>> a = xr.DataArray([1, 2, 3])
>>> b = xr.DataArray([4, 5, 6])
>>> xr.cross(a, b, dim="dim_0")
<xarray.DataArray (dim_0: 3)> Size: 24B
array([-3,  6, -3])
Dimensions without coordinates: dim_0
```

Vector cross-product with 3 dimensions but zeros at the last axis
yields the same results as with 2 dimensions:

```pycon
>>> a = xr.DataArray([1, 2, 0])
>>> b = xr.DataArray([4, 5, 0])
>>> xr.cross(a, b, dim="dim_0")
<xarray.DataArray (dim_0: 3)> Size: 24B
array([ 0,  0, -3])
Dimensions without coordinates: dim_0
```

Multiple vector cross-products. Note that the direction of the
cross product vector is defined by the right-hand rule:

```pycon
>>> a = xr.DataArray(
...     [[1, 2, 3], [4, 5, 6]],
...     dims=("time", "cartesian"),
...     coords=dict(
...         time=(["time"], [0, 1]),
...         cartesian=(["cartesian"], ["x", "y", "z"]),
...     ),
... )
>>> b = xr.DataArray(
...     [[4, 5, 6], [1, 2, 3]],
...     dims=("time", "cartesian"),
...     coords=dict(
...         time=(["time"], [0, 1]),
...         cartesian=(["cartesian"], ["x", "y", "z"]),
...     ),
... )
>>> xr.cross(a, b, dim="cartesian")
<xarray.DataArray (time: 2, cartesian: 3)> Size: 48B
array([[-3,  6, -3],
       [ 3, -6,  3]])
Coordinates:
  * time       (time) int64 16B 0 1
  * cartesian  (cartesian) <U1 12B 'x' 'y' 'z'
```

Cross can be called on Datasets by converting to DataArrays and later
back to a Dataset:

```pycon
>>> ds_a = xr.Dataset(dict(x=("dim_0", [1]), y=("dim_0", [2]), z=("dim_0", [3])))
>>> ds_b = xr.Dataset(dict(x=("dim_0", [4]), y=("dim_0", [5]), z=("dim_0", [6])))
>>> c = xr.cross(
...     ds_a.to_dataarray("cartesian"),
...     ds_b.to_dataarray("cartesian"),
...     dim="cartesian",
... )
>>> c.to_dataset(dim="cartesian")
<xarray.Dataset> Size: 24B
Dimensions:  (dim_0: 1)
Dimensions without coordinates: dim_0
Data variables:
    x        (dim_0) int64 8B -3
    y        (dim_0) int64 8B 6
    z        (dim_0) int64 8B -3
```

<div id="b19a1ec0-505f-4fb0-a6dd-e2a3f9904c32" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('fe836ae5-217b-4bcb-a3fd-e5572db6af6a','b19a1ec0-505f-4fb0-a6dd-e2a3f9904c32')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('fe836ae5-217b-4bcb-a3fd-e5572db6af6a','b19a1ec0-505f-4fb0-a6dd-e2a3f9904c32')">Open In Tab</button></div><div id="c95b0ab9-8d9f-4fcd-ae8a-1a308bea893b" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[`numpy.cross`](https://numpy.org/doc/stable/reference/generated/numpy.cross.html#numpy.cross)
: Corresponding numpy function
