Skip to content

BUG: np.linalg.svd(..., hermitian=True) returns non-unitary vh#31347

Merged
WarrenWeckesser merged 7 commits into
numpy:mainfrom
EarlMilktea:svdh-singular
May 18, 2026
Merged

BUG: np.linalg.svd(..., hermitian=True) returns non-unitary vh#31347
WarrenWeckesser merged 7 commits into
numpy:mainfrom
EarlMilktea:svdh-singular

Conversation

@EarlMilktea

@EarlMilktea EarlMilktea commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

PR summary

Currently np.linalg.svd can return non-unitary vh due to sign 0 appearing when input array is singular.
This PR fixes the issue.

>>> import numpy as np
>>> res = np.linalg.svd(np.array([[1, 0], [0, 0]]), hermitian=True)
>>> res.Vh
array([[1., 0.],
       [0., 0.]])

AI Disclosure

I've used ChatGPT to ensure that my PR is following the guideline. No codes are generated by AI.

Explicitly remove 0 from eigenvalue signs to ensure that vh is unitary
sign contains zero here as x @ x = [[1, 0], [0, 0]].
@EarlMilktea EarlMilktea marked this pull request as ready for review April 28, 2026 05:36
@EarlMilktea

Copy link
Copy Markdown
Contributor Author

MEMO: I suspect two CI failures are not related to this PR.

Comment thread numpy/linalg/_linalg.py Outdated

@WarrenWeckesser WarrenWeckesser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @EarlMilktea. The fix looks correct, but I have some suggested changes in the implementation.

Comment thread numpy/linalg/tests/test_linalg.py Outdated
Comment thread numpy/linalg/_linalg.py Outdated

@WarrenWeckesser WarrenWeckesser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me.

@WarrenWeckesser WarrenWeckesser merged commit bba3954 into numpy:main May 18, 2026
86 checks passed
@charris charris added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels May 18, 2026
@charris charris added this to the 2.4.6 release milestone May 18, 2026
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label May 18, 2026
charris pushed a commit that referenced this pull request May 18, 2026
…31347)

* Explicitly remove 0 from eigenvalue signs to ensure that vh is unitary.
* Add Hermitian SVD test that has 0 as a singular value.
charris added a commit that referenced this pull request May 18, 2026
BUG: `np.linalg.svd(..., hermitian=True)` returns non-unitary `vh` (#31347)
@EarlMilktea EarlMilktea deleted the svdh-singular branch May 19, 2026 08:14
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.

5 participants