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

Skip to content

Commit 0c72b0b

Browse files
authored
Merge pull request #31459 from charris/backport-31347
BUG: `np.linalg.svd(..., hermitian=True)` returns non-unitary `vh` (#31347)
2 parents 9778d26 + d1bffeb commit 0c72b0b

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,
@@ -1810,7 +1809,8 @@ def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
18101809
# and related arrays to have the correct order
18111810
if compute_uv:
18121811
s, u = eigh(a)
1813-
sgn = sign(s)
1812+
# avoid zero sign
1813+
sgn = np.copysign(1.0, s)
18141814
s = abs(s)
18151815
sidx = argsort(s)[..., ::-1]
18161816
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
@@ -745,6 +745,12 @@ def hermitian(mat):
745745
class TestSVDHermitian(SVDHermitianCases, SVDBaseTests):
746746
hermitian = True
747747

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

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

0 commit comments

Comments
 (0)