xarray.dot (original) (raw)
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:
- *arrays (DataArray or Variable) – Arrays to compute.
- dim (str, iterable of hashable,
"..."
or 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) – Additional keyword arguments passed to
numpy.einsum
ordask.array.einsum
Returns:
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.
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)