Skip to content
Open
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
2 changes: 2 additions & 0 deletions numpy/linalg/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,8 @@ def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
if compute_uv:
s, u = eigh(a)
sgn = sign(s)
# avoid zero sign
sgn = np.where(sgn == 0, 1, sgn)
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, can we assume that -0 isn't a thing and just use signbit instead of sign?

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.

I personally prefer my approach to make the result predictable...

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.

well, there's a performance penalty with this approach

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.

Yeah, but if we aren't sure (I dunno yet), there is already sorting, etc. going on, so it shouldn't actually matter (where just always feels slightly awkward to me, I guess).

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.

Anyway, I think it's probably good. But @WarrenWeckesser might have a quick thought and know this a bit better.

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.

Thank you all for comments. Let me know if there are any updates.

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.

@WarrenWeckesser Could you kindly review this PR for me? Thank you in advance!

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_almost_equal(u @ u.T.conj(), np.eye(2))
assert_almost_equal(vh @ vh.T.conj(), np.eye(2))


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