Skip to content

ENH: Return rank 0 for empty matrices in matrix_rank (#30422)#31197

Merged
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-30422
Apr 10, 2026
Merged

ENH: Return rank 0 for empty matrices in matrix_rank (#30422)#31197
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-30422

Conversation

@charris
Copy link
Copy Markdown
Member

@charris charris commented Apr 10, 2026

Backport of #30422.

This PR fixes a bug in np.linalg.matrix_rank where empty matrices
(previously with 0 rows or 0 columns) would raise a ValueError due to
attempting a reduction operation on a zero-size array.

Changes include:

  • Return 0 for all empty matrices in matrix_rank.
  • Added tests covering all empty matrix shapes to ensure the correct behavior.

Files modified:

  • numpy/linalg/_linalg.py
  • numpy/linalg/tests/test_linalg.py

Closes #30421

This PR fixes a bug in np.linalg.matrix_rank where empty matrices
(previously with 0 rows or 0 columns) would raise a ValueError due to
attempting a reduction operation on a zero-size array.
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Apr 10, 2026
@charris charris added this to the 2.4.5 release milestone Apr 10, 2026
@charris charris merged commit 1681476 into numpy:maintenance/2.4.x Apr 10, 2026
74 checks passed
@charris charris deleted the backport-30422 branch April 10, 2026 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants