Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Scipy's eigh instead of Numpy's to avoid convergence error #311

Merged
merged 4 commits into from
Apr 12, 2022
Merged
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
5 changes: 4 additions & 1 deletion deerlab/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,13 @@ def nearest_psd(A):
How can I calculate the nearest positive semi-definite matrix?
StackOverflow, https://stackoverflow.com/a/63131250/16396391
"""
# If matrix is empty, return it empty (scipy.linalg.eigh cannot deal with empty matrix)
if A.size==0:
return A
# Symmetrize the matrix
Asym = (A + A.T)/2
# Construct positive semi-definite matrix via eigenvalue decomposition
eigval, eigvec = np.linalg.eigh(Asym)
eigval, eigvec = scp.linalg.eigh(Asym)
eigval[eigval < 0] = 0
Cpsd = np.real(eigvec.dot(np.diag(eigval)).dot(eigvec.T))
# Avoid round-off errors
Expand Down