Skip to content

Commit 32f2da4

Browse files
dhruvak001DHRUVA KUMAR KAUSHALmax-sixty
authored
documenting how xarray.dot() interacts with coordinates (#10958)
* documenting how xarray.dot() interacts with coordinates * minor fixes * simplifying --------- Co-authored-by: DHRUVA KUMAR KAUSHAL <sanjay@MacBook-Air.local> Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com>
1 parent 9aee8b7 commit 32f2da4

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

xarray/computation/computation.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,14 @@ def dot(
515515
We recommend installing the optional ``opt_einsum`` package, or alternatively passing ``optimize=True``,
516516
which is passed through to ``np.einsum``, and works for most array backends.
517517
518+
**Coordinate Handling**
519+
520+
Like all xarray operations, ``dot`` automatically aligns array coordinates.
521+
Coordinates are aligned by their **values**, not their order. By default, xarray uses
522+
an inner join, so only overlapping coordinate values are included. With the default
523+
``arithmetic_join="inner"``, ``dot(a, b)`` is mathematically equivalent to ``(a * b).sum()``
524+
over the specified dimensions. See :ref:`math automatic alignment` for more details.
525+
518526
Examples
519527
--------
520528
>>> da_a = xr.DataArray(np.arange(3 * 2).reshape(3, 2), dims=["a", "b"])
@@ -572,6 +580,37 @@ def dot(
572580
>>> xr.dot(da_a, da_b, dim=...)
573581
<xarray.DataArray ()> Size: 8B
574582
array(235)
583+
584+
**Coordinate alignment examples:**
585+
586+
Coordinates are aligned by their values, not their order:
587+
588+
>>> x = xr.DataArray([1, 10], coords=[("foo", ["a", "b"])])
589+
>>> y = xr.DataArray([2, 20], coords=[("foo", ["b", "a"])])
590+
>>> xr.dot(x, y)
591+
<xarray.DataArray ()> Size: 8B
592+
array(40)
593+
594+
Non-overlapping coordinates are excluded from the computation:
595+
596+
>>> x = xr.DataArray([1, 10], coords=[("foo", ["a", "b"])])
597+
>>> y = xr.DataArray([2, 30], coords=[("foo", ["b", "c"])])
598+
>>> xr.dot(x, y) # only 'b' overlaps: 10 * 2 = 20
599+
<xarray.DataArray ()> Size: 8B
600+
array(20)
601+
602+
Dimensions not involved in the dot product keep their coordinates:
603+
604+
>>> x = xr.DataArray(
605+
... [[1, 2], [3, 4]],
606+
... coords=[("time", [0, 1]), ("space", ["IA", "IL"])],
607+
... )
608+
>>> y = xr.DataArray([10, 20], coords=[("space", ["IA", "IL"])])
609+
>>> xr.dot(x, y, dim="space") # time coordinates are preserved
610+
<xarray.DataArray (time: 2)> Size: 16B
611+
array([ 50, 110])
612+
Coordinates:
613+
* time (time) int64 16B 0 1
575614
"""
576615
from xarray.core.dataarray import DataArray
577616

xarray/core/dataarray.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5170,6 +5170,11 @@ def dot(
51705170
dot
51715171
numpy.tensordot
51725172
5173+
Notes
5174+
-----
5175+
This method automatically aligns coordinates by their values (not their order).
5176+
See :ref:`math automatic alignment` and :py:func:`xarray.dot` for more details.
5177+
51735178
Examples
51745179
--------
51755180
>>> da_vals = np.arange(6 * 5 * 4).reshape((6, 5, 4))
@@ -5187,6 +5192,14 @@ def dot(
51875192
>>> dot_result.dims
51885193
('x', 'y')
51895194
5195+
Coordinates are aligned by their values:
5196+
5197+
>>> x = xr.DataArray([1, 10], coords=[("foo", ["a", "b"])])
5198+
>>> y = xr.DataArray([2, 20], coords=[("foo", ["b", "a"])])
5199+
>>> x.dot(y)
5200+
<xarray.DataArray ()> Size: 8B
5201+
array(40)
5202+
51905203
"""
51915204
if isinstance(other, Dataset):
51925205
raise NotImplementedError(

0 commit comments

Comments
 (0)