Skip to content

Commit

Permalink
Fixes bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Quesada committed Jan 3, 2024
1 parent d62b0d1 commit 647e464
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion thewalrus/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def takagi(A, svd_order=True):
# If the matrix A is real one can be more clever and use its eigendecomposition
ls, U = np.linalg.eigh(A)
vals = np.abs(ls) # These are the Takagi eigenvalues
signs = (-1) ** (1 + np.heaviside(vals, 1))
signs = (-1) ** (1 + np.heaviside(ls, 1))
phases = np.sqrt(np.complex128(signs))
Uc = U * phases # One needs to readjust the phases
# Find the permutation to sort in decreasing order
Expand Down
7 changes: 5 additions & 2 deletions thewalrus/tests/test_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,14 @@ def test_takagi(n, datatype, svd_order):
@pytest.mark.parametrize("half_rank", [0, 1])
@pytest.mark.parametrize("phase", [0, 1])
@pytest.mark.parametrize("null_space", [0, 5, 10])
def test_degenerate(n, datatype, svd_order, half_rank, phase, null_space):
@pytest.mark.parametrize("offset", [0, 0.5])
def test_degenerate(n, datatype, svd_order, half_rank, phase, null_space, offset):
"""Tests Takagi produces the correct result for very degenerate cases"""
nhalf = n // 2
diags = (
[half_rank * np.random.rand()] * nhalf + [np.random.rand()] * (n - nhalf) + [0] * null_space
[half_rank * np.random.rand()] * nhalf
+ [np.random.rand() - offset] * (n - nhalf)
+ [0] * null_space
)
if datatype is np.complex128:
U = haar_measure(n + null_space)
Expand Down

0 comments on commit 647e464

Please sign in to comment.