xarray.dot#
- xarray.dot(*arrays, dim=None, **kwargs)[source]#
Generalized dot product for xarray objects. Like
np.einsum, but provides a simpler interface based on array dimension names.- Parameters:
- Returns:
See also
numpy.einsum,dask.array.einsum,opt_einsum.contractNotes
We recommend installing the optional
opt_einsumpackage, or alternatively passingoptimize=True, which is passed through tonp.einsum, and works for most array backends.Coordinate Handling
Like all xarray operations,
dotautomatically 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 defaultarithmetic_join="inner",dot(a, b)is mathematically equivalent to(a * b).sum()over the specified dimensions. See Automatic alignment for more details.Examples
>>> 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"])
>>> da_a <xarray.DataArray (a: 3, b: 2)> Size: 48B array([[0, 1], [2, 3], [4, 5]]) Dimensions without coordinates: a, b
>>> 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
>>> da_c <xarray.DataArray (c: 2, d: 3)> Size: 48B array([[0, 1, 2], [3, 4, 5]]) Dimensions without coordinates: c, d
>>> xr.dot(da_a, da_b, dim=["a", "b"]) <xarray.DataArray (c: 2)> Size: 16B array([110, 125]) Dimensions without coordinates: c
>>> 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
>>> 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
>>> xr.dot(da_a, da_b) <xarray.DataArray (c: 2)> Size: 16B array([110, 125]) Dimensions without coordinates: c
>>> 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:
>>> 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:
>>> 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:
>>> 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