Skip to content

Fix np.linalg.pinv dimension inference for DataArrays#11415

Closed
C1-BA-B1-F3 wants to merge 1 commit into
pydata:mainfrom
C1-BA-B1-F3:fix/pinv-dim-inference
Closed

Fix np.linalg.pinv dimension inference for DataArrays#11415
C1-BA-B1-F3 wants to merge 1 commit into
pydata:mainfrom
C1-BA-B1-F3:fix/pinv-dim-inference

Conversation

@C1-BA-B1-F3

Copy link
Copy Markdown

Summary

Fixes #11396

When is called on a DataArray or Variable, the result has swapped last two dimensions (e.g., shape → ). Previously, blindly preserved the original dimension names, causing a mismatch between dimension names and their actual sizes.

Problem

Fix

Added helper function that finds the dimension permutation when shape changes. Updated to use this inference when the result shape differs from the original.

The function:

  1. Checks if the new shape is a permutation of the old shape
  2. Maps dimension names based on matching sizes
  3. Raises ValueError if the shape change can't be resolved (e.g., different number of dimensions)

Tests

  • Added regression tests for both Variable and DataArray
  • Verified with batched pinv, square matrices, and regular operations
  • All existing tests pass (988 passed)

When np.linalg.pinv is called on a DataArray or Variable, the result
has swapped last two dimensions. Previously, __array_wrap__ blindly
preserved the original dimension names, causing a mismatch between
dimension names and their actual sizes.

This fix adds dimension inference in __array_wrap__ when the result
shape differs from the original. It finds the permutation that maps
old dimensions to new ones based on their sizes.

Fixes pydata#11396
@C1-BA-B1-F3

Copy link
Copy Markdown
Author

Closing as duplicate of PR #11405 which also fixes #11396 using the existing dimension-aware replacement infrastructure.

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 this pull request may close these issues.

np.linalg.pinv of a DataArray results in mismatched coordinates

1 participant