Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
BUG: np.linalg.svd(..., hermitian=True) returns non-unitary vh (#…
…31347)

* Explicitly remove 0 from eigenvalue signs to ensure that vh is unitary.
* Add Hermitian SVD test that has 0 as a singular value.
  • Loading branch information
EarlMilktea authored and charris committed May 18, 2026
commit d1bffeb9ec4ec0bf029c94ea35abffa92d5c30f2
4 changes: 2 additions & 2 deletions numpy/linalg/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
overrides,
prod,
reciprocal,
sign,
single,
sort,
sqrt,
Expand Down Expand Up @@ -1810,7 +1809,8 @@ def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
# and related arrays to have the correct order
if compute_uv:
s, u = eigh(a)
sgn = sign(s)
# avoid zero sign
sgn = np.copysign(1.0, s)
s = abs(s)
sidx = argsort(s)[..., ::-1]
sgn = np.take_along_axis(sgn, sidx, axis=-1)
Expand Down
6 changes: 6 additions & 0 deletions numpy/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ def hermitian(mat):
class TestSVDHermitian(SVDHermitianCases, SVDBaseTests):
hermitian = True

def test_singular(self):
x = np.array([[1, 0], [0, 0]])
u, _, vh = linalg.svd(x, hermitian=self.hermitian)
assert_allclose(u @ u.T.conj(), np.eye(2), rtol=1e-14)
assert_allclose(vh @ vh.T.conj(), np.eye(2), rtol=1e-14)


class CondCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
# cond(x, p) for p in (None, 2, -2)
Expand Down
Loading