# xarray.dot

### xarray.dot(\*arrays, dim=None, \*\*kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/computation/computation.py#L475-L671)

Generalized dot product for xarray objects. Like `np.einsum`, but
provides a simpler interface based on array dimension names.

* **Parameters:**
  * **\*arrays** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray) or [`Variable`](xarray.Variable.html.md#xarray.Variable)) – Arrays to compute.
  * **dim** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [iterable](https://docs.python.org/3/glossary.html#term-iterable) of [hashable](../user-guide/terminology.html.md#term-name), `"..."` or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – Which dimensions to sum over. Ellipsis (’…’) sums over all dimensions.
    If not specified, then all the common dimensions are summed over.
  * **\*\*kwargs** ([`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Additional keyword arguments passed to `numpy.einsum` or
    `dask.array.einsum`
* **Returns:**
  [`DataArray`](xarray.DataArray.html.md#xarray.DataArray)

#### SEE ALSO
[`numpy.einsum`](https://numpy.org/doc/stable/reference/generated/numpy.einsum.html#numpy.einsum), [`dask.array.einsum`](https://docs.dask.org/en/latest/generated/dask.array.einsum.html#dask.array.einsum), `opt_einsum.contract`

### Notes

We recommend installing the optional `opt_einsum` package, or alternatively passing `optimize=True`,
which is passed through to `np.einsum`, and works for most array backends.

**Coordinate Handling**

Like all xarray operations, `dot` automatically aligns array coordinates.
Coordinates are aligned by their **values**, not their order. By default, xarray uses
an inner join, so only overlapping coordinate values are included. With the default
`arithmetic_join="inner"`, `dot(a, b)` is mathematically equivalent to `(a * b).sum()`
over the specified dimensions. See [Automatic alignment](../user-guide/computation.html.md#math-automatic-alignment) for more details.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('da3c3d2b-0308-45e8-b909-5afb79b1fbba','9ba51898-3cb2-435e-acac-f16c4d75fe82','3823187e-e9cb-4975-8a34-f7bfd61fc56f','../lite/tree/../notebooks/index.html?path=94ad6be7_5c21_4863_8e26_a1eda197b1e4.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> da_a = xr.DataArray(np.arange(3 * 2).reshape(3, 2), dims=["a", "b"])
>>> da_b = xr.DataArray(np.arange(3 * 2 * 2).reshape(3, 2, 2), dims=["a", "b", "c"])
>>> da_c = xr.DataArray(np.arange(2 * 3).reshape(2, 3), dims=["c", "d"])
```

```pycon
>>> da_a
<xarray.DataArray (a: 3, b: 2)> Size: 48B
array([[0, 1],
       [2, 3],
       [4, 5]])
Dimensions without coordinates: a, b
```

```pycon
>>> da_b
<xarray.DataArray (a: 3, b: 2, c: 2)> Size: 96B
array([[[ 0,  1],
        [ 2,  3]],

       [[ 4,  5],
        [ 6,  7]],

       [[ 8,  9],
        [10, 11]]])
Dimensions without coordinates: a, b, c
```

```pycon
>>> da_c
<xarray.DataArray (c: 2, d: 3)> Size: 48B
array([[0, 1, 2],
       [3, 4, 5]])
Dimensions without coordinates: c, d
```

```pycon
>>> xr.dot(da_a, da_b, dim=["a", "b"])
<xarray.DataArray (c: 2)> Size: 16B
array([110, 125])
Dimensions without coordinates: c
```

```pycon
>>> xr.dot(da_a, da_b, dim=["a"])
<xarray.DataArray (b: 2, c: 2)> Size: 32B
array([[40, 46],
       [70, 79]])
Dimensions without coordinates: b, c
```

```pycon
>>> xr.dot(da_a, da_b, da_c, dim=["b", "c"])
<xarray.DataArray (a: 3, d: 3)> Size: 72B
array([[  9,  14,  19],
       [ 93, 150, 207],
       [273, 446, 619]])
Dimensions without coordinates: a, d
```

```pycon
>>> xr.dot(da_a, da_b)
<xarray.DataArray (c: 2)> Size: 16B
array([110, 125])
Dimensions without coordinates: c
```

```pycon
>>> xr.dot(da_a, da_b, dim=...)
<xarray.DataArray ()> Size: 8B
array(235)
```

**Coordinate alignment examples:**

Coordinates are aligned by their values, not their order:

```pycon
>>> x = xr.DataArray([1, 10], coords=[("foo", ["a", "b"])])
>>> y = xr.DataArray([2, 20], coords=[("foo", ["b", "a"])])
>>> xr.dot(x, y)
<xarray.DataArray ()> Size: 8B
array(40)
```

Non-overlapping coordinates are excluded from the computation:

```pycon
>>> x = xr.DataArray([1, 10], coords=[("foo", ["a", "b"])])
>>> y = xr.DataArray([2, 30], coords=[("foo", ["b", "c"])])
>>> xr.dot(x, y)  # only 'b' overlaps: 10 * 2 = 20
<xarray.DataArray ()> Size: 8B
array(20)
```

Dimensions not involved in the dot product keep their coordinates:

```pycon
>>> x = xr.DataArray(
...     [[1, 2], [3, 4]],
...     coords=[("time", [0, 1]), ("space", ["IA", "IL"])],
... )
>>> y = xr.DataArray([10, 20], coords=[("space", ["IA", "IL"])])
>>> xr.dot(x, y, dim="space")  # time coordinates are preserved
<xarray.DataArray (time: 2)> Size: 16B
array([ 50, 110])
Coordinates:
  * time     (time) int64 16B 0 1
```

<div id="3823187e-e9cb-4975-8a34-f7bfd61fc56f" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('da3c3d2b-0308-45e8-b909-5afb79b1fbba','3823187e-e9cb-4975-8a34-f7bfd61fc56f')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('da3c3d2b-0308-45e8-b909-5afb79b1fbba','3823187e-e9cb-4975-8a34-f7bfd61fc56f')">Open In Tab</button></div><div id="9ba51898-3cb2-435e-acac-f16c4d75fe82" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
