Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
seberg merged 11 commits intonumpy:mainnumpy/numpy:mainfrom
false200:fix-matrix-rank-emptyfalse200/numpy:fix-matrix-rank-emptyCopy head branch name to clipboard
Dec 19, 2025
Merged

ENH: Return rank 0 for empty matrices in matrix_rank#30422
seberg merged 11 commits intonumpy:mainnumpy/numpy:mainfrom
false200:fix-matrix-rank-emptyfalse200/numpy:fix-matrix-rank-emptyCopy head branch name to clipboard

Conversation

@false200
Copy link
Copy Markdown
Contributor

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

Fix trailing whitespace causing lint failure and simplify the empty-matrix
rank assertion.
Comment thread numpy/linalg/_linalg.py Outdated

# Handle empty matrices - rank is 0 for matrices with 0 rows or 0 columns
if A.size == 0:
return 0
Copy link
Copy Markdown
Member

@seberg seberg Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should just add initial=0 which I assume also works. The tests you added are missing the case of np.zeros((0, 5, 5)) where this return is clearly incorrect.

EDIT: Actually, while we are here, can you check if we don't return np.intp(0) on the last return? We may want to adjust the return type for the other early return as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @seberg, thanks for the feedback!
Added initial=0 to the S.max(... ) call as suggested, and I also added the missing np.zeros((0, 5, 5)) test.

I ran the linalg test suite locally and everything looks good.
All relevant tests passed, only the usual SKIP/XFAIL cases showed up.

WhatsApp Image 2025-12-12 at 22 58 30_538d4365
Let me know if you'd like any additional tweaks!

Copy link
Copy Markdown
Contributor Author

@false200 false200 Dec 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @seberg , just a quick ping , all checks have passed now.

Would you mind taking a look for merge? Thanks!

@false200 false200 requested a review from seberg December 12, 2025 11:12
@seberg seberg removed their request for review December 12, 2025 11:21
@false200 false200 requested a review from seberg December 12, 2025 17:32
@seberg seberg changed the title BUG: Return rank 0 for empty matrices in matrix_rank ENH: Return rank 0 for empty matrices in matrix_rank Dec 19, 2025
Copy link
Copy Markdown
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good now (I might punt on backporting, but should be OK too).
The tests could be nicer with parametrization, but it is OK.

I am not quite 100% on whether zero is always expected, so wondering if you can argue that or know some prior art?

@false200
Copy link
Copy Markdown
Contributor Author

@seberg Thanks for the careful review and the questions
On the semantics, returning rank = 0 for empty matrices is consistent with standard linear algebra definitions and with prior art.
From a theoretical standpoint, rank is the dimension of the column (or row) space. For matrices with zero rows or zero columns, there are no columns (or no vectors at all), so the dimension of the span is 0 by definition. This is the convention used in linear algebra texts when discussing degenerate or empty linear maps.
From an SVD perspective, which matrix_rank is based on, an empty matrix has no singular values, so the count of singular values greater than the tolerance is naturally zero. Using initial=0 simply makes this behavior explicit and avoids the reduction error.
There is also prior art

  • NumPy itself already returns 0 for degenerate inputs such as np.linalg.matrix_rank(np.zeros((n,))), which aligns with this interpretation.
  • MATLAB’s rank returns 0 for empty matrices (it treats the empty singular-value set as having no values above tolerance).
  • This is consistent with the rank–nullity theorem - if the number of columns is zero, rank must be zero.

So this change doesn’t introduce a new definition so much as make the existing mathematical convention explicit and handle it robustly in code.
Happy to adjust wording or tests further if you’d like.

@false200 false200 requested a review from seberg December 19, 2025 17:48
@seberg
Copy link
Copy Markdown
Member

seberg commented Dec 19, 2025

Cool, thanks for confirming, agreed seems rather clear convention, so let's put this in, thanks.

Should be safe for backporting, but I would probably just consider it an enhancement.

@seberg seberg merged commit c59095f into numpy:main Dec 19, 2025
71 checks passed
@false200
Copy link
Copy Markdown
Contributor Author

@seberg Thanks for clarifying!

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Apr 3, 2026
charris pushed a commit to charris/numpy that referenced this pull request Apr 10, 2026
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 removed the 09 - Backport-Candidate PRs tagged should be backported label Apr 10, 2026
charris added a commit that referenced this pull request Apr 10, 2026
ENH: Return rank 0 for empty matrices in matrix_rank (#30422)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: np.linalg.matrix_rank fails for empty matrices

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.