Skip to content

add ALL_DIMS in xr.dot #3423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mathause opened this issue Oct 21, 2019 · 0 comments · Fixed by #3424
Closed

add ALL_DIMS in xr.dot #3423

mathause opened this issue Oct 21, 2019 · 0 comments · Fixed by #3424

Comments

@mathause
Copy link
Collaborator

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant