Skip to content

add ALL_DIMS in xr.dot #3423

Closed
Closed
@mathause

Description

@mathause

MCVE Code Sample

import numpy as np
import xarray as xr
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'])

xr.dot(da_a, da_b, dims=None)

This sums over the common dimensions:

<xarray.DataArray (c: 2)>
array([110, 125])
Dimensions without coordinates: c

To sum over all dimensions:

xr.dot(da_a, da_b, dims=['a', 'b', 'c'])
<xarray.DataArray (c: 2)>
array([110, 125])
Dimensions without coordinates: c

Problem Description

xr.dot with dims=None currently sums over all the common dimensions. However, there are cases when a sum over all dimensions is desired. E.g. xr.dot(da_a, da_b, dims=['a', 'b', 'c']) is a a memory efficient way to compute (da_a * da_b).sum() (if a and b don't share the same dimensions). This is currently used in #2922 (example).

Therefore I suggest to allow xr.dot(da_a, da_b, dims=xr.ALL_DIMS) as shortcut to sum over all dimensions.

I assume there is no intent to change the behavior of xr.dot(..., dims=None)? (As it is a bit in contrast to other functions that are mostly applied over all dimensions.)

@fujiisoup @max-sixty

Output of xr.show_versions()

# Paste the output here xr.show_versions() here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions