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

Skip to content

Commit bba3954

Browse files
authored
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.
1 parent b668e0a commit bba3954

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

numpy/linalg/_linalg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
overrides,
6262
prod,
6363
reciprocal,
64-
sign,
6564
single,
6665
sort,
6766
sqrt,
@@ -1809,7 +1808,8 @@ def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
18091808
# and related arrays to have the correct order
18101809
if compute_uv:
18111810
s, u = eigh(a)
1812-
sgn = sign(s)
1811+
# avoid zero sign
1812+
sgn = np.copysign(1.0, s)
18131813
s = abs(s)
18141814
sidx = argsort(s)[..., ::-1]
18151815
sgn = np.take_along_axis(sgn, sidx, axis=-1)

numpy/linalg/tests/test_linalg.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,12 @@ def hermitian(mat):
765765
class TestSVDHermitian(SVDHermitianCases, SVDBaseTests):
766766
hermitian = True
767767

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

769775
class CondCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):
770776
# cond(x, p) for p in (None, 2, -2)

0 commit comments

Comments
 (0)