-
Notifications
You must be signed in to change notification settings - Fork 10
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
Reuse Arpack in order to compute norm of Bk #159
base: master
Are you sure you want to change the base?
Changes from all commits
f2c9bbe
47311cb
441521f
9a1122e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,79 @@ | ||||||
# use Arpack to obtain largest eigenvalue in magnitude with a minimum of robustness | ||||||
function LinearAlgebra.opnorm(B; kwargs...) | ||||||
_, s, _ = tsvd(B) | ||||||
return s[1] | ||||||
m, n = size(B) | ||||||
opnorm_fcn = m == n ? opnorm_eig : opnorm_svd | ||||||
return opnorm_fcn(B; kwargs...) | ||||||
end | ||||||
function opnorm_eig(B; max_attempts::Int = 3) | ||||||
have_eig = false | ||||||
attempt = 0 | ||||||
λ = zero(eltype(B)) | ||||||
n = size(B, 1) | ||||||
nev = 1 | ||||||
ncv = max(20, 2 * nev + 1) | ||||||
|
||||||
while !(have_eig || attempt >= max_attempts) | ||||||
attempt += 1 | ||||||
try | ||||||
# Perform eigendecomposition | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
d, nconv, niter, nmult, resid = eigs(B; nev = nev, ncv = ncv, which = :LM, ritzvec = false, check = 1) | ||||||
|
||||||
# Check if eigenvalue has converged | ||||||
have_eig = nconv == 1 | ||||||
if have_eig | ||||||
λ = abs(d[1]) # Take absolute value of the largest eigenvalue | ||||||
break # Exit loop if successful | ||||||
else | ||||||
# Increase NCV for the next attempt if convergence wasn't achieved | ||||||
ncv = min(2 * ncv, n) | ||||||
end | ||||||
catch e | ||||||
if occursin("XYAUPD_Exception", string(e)) | ||||||
@warn "Arpack error: $e. Increasing NCV to $ncv and retrying." | ||||||
ncv = min(2 * ncv, n) # Increase NCV but don't exceed matrix size | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we still get an error when |
||||||
else | ||||||
rethrow(e) # Re-raise if it's a different error | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
return λ, have_eig | ||||||
end | ||||||
|
||||||
function opnorm_svd(J; max_attempts::Int = 3) | ||||||
MohamedLaghdafHABIBOULLAH marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
have_svd = false | ||||||
attempt = 0 | ||||||
σ = zero(eltype(J)) | ||||||
n = min(size(J)...) # Minimum dimension of the matrix | ||||||
nsv = 1 | ||||||
ncv = 10 | ||||||
|
||||||
while !(have_svd || attempt >= max_attempts) | ||||||
attempt += 1 | ||||||
try | ||||||
# Perform singular value decomposition | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
s, nconv, niter, nmult, resid = svds(J; nsv = nsv, ncv = ncv, ritzvec = false, check = 1) | ||||||
|
||||||
# Check if singular value has converged | ||||||
have_svd = nconv >= 1 | ||||||
if have_svd | ||||||
σ = maximum(s.S) # Take the largest singular value | ||||||
break # Exit loop if successful | ||||||
else | ||||||
# Increase NCV for the next attempt if convergence wasn't achieved | ||||||
ncv = min(2 * ncv, n) | ||||||
end | ||||||
catch e | ||||||
if occursin("XYAUPD_Exception", string(e)) | ||||||
@warn "Arpack error: $e. Increasing NCV to $ncv and retrying." | ||||||
ncv = min(2 * ncv, n) # Increase NCV but don't exceed matrix size | ||||||
else | ||||||
rethrow(e) # Re-raise if it's a different error | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
return σ, have_svd | ||||||
end | ||||||
|
||||||
ShiftedProximalOperators.iprox!( | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have a backup plan if the computation fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working on understanding why it may not work ^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done