-
Notifications
You must be signed in to change notification settings - Fork 40
Add opnorm implementation #375
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
Open
farhadrclass
wants to merge
30
commits into
JuliaSmoothOptimizers:main
Choose a base branch
from
Farhad-phd:opNorm_branch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
eeed18a
Add robust opnorm implementation and tests
farhadrclass a67ac20
Update test_opnorm.jl
farhadrclass 13c04c7
Add Arpack as dependency and import in utilities.jl
farhadrclass f2c3d06
Add robust opnorm implementation and tests
farhadrclass cf1031a
Update test_opnorm.jl
farhadrclass 88b5df5
Add Arpack as dependency and import in utilities.jl
farhadrclass e29a76e
Rename opnorm to estimate_opnorm and update tests
farhadrclass 4fce741
Apply suggestions from code review
farhadrclass 3b17e79
Apply suggestions from code review
farhadrclass 67aeac9
Update utilities.jl
farhadrclass 829a552
Update src/utilities.jl
farhadrclass 8b0da8c
Update utilities.jl
farhadrclass 2d7ba5d
Update Project.toml
farhadrclass 5af8752
Update Project.toml
farhadrclass a3d545f
Add new dependencies and update opnorm tests
farhadrclass a672263
Refactor opnorm estimation dispatch and improve tests
farhadrclass 779431f
Add Arpack/TSVD weakdeps extension
farhadrclass b4dd14d
Update utilities.jl
farhadrclass ad73692
Merge branch 'JuliaSmoothOptimizers:main' into opNorm_branch
farhadrclass 50b27b6
Merge branch 'JuliaSmoothOptimizers:main' into opNorm_branch
farhadrclass 7111af1
Merge branch 'opNorm_branch' of https://github.com/Farhad-phd/LinearO…
farhadrclass 15b6dbd
Support estimate_opnorm for AbstractLinearOperator
farhadrclass accfea2
Update test/Project.toml
farhadrclass 15ae552
Merge branch 'JuliaSmoothOptimizers:main' into opNorm_branch
farhadrclass a1d8bcc
Update test/test_estimate_opnorm.jl
farhadrclass 755f82f
Update src/utilities.jl
farhadrclass 12deb75
Edits based on Tangi's review
farhadrclass 1565241
extra end
farhadrclass 9d68e4a
Update src/utilities.jl
farhadrclass d97f699
Update utilities.jl
farhadrclass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| module LinearOperatorsOpNormExt | ||
|
|
||
| using LinearOperators | ||
| using Arpack | ||
| using TSVD | ||
| using LinearAlgebra | ||
| using GenericLinearAlgebra | ||
| import Arpack: eigs, svds | ||
|
|
||
| import LinearOperators: estimate_opnorm | ||
|
|
||
| function estimate_opnorm(B::AbstractLinearOperator; kwargs...) | ||
| _estimate_opnorm(B, eltype(B); kwargs...) | ||
| end | ||
|
|
||
| # Fallback for non-standard number types (uses TSVD) | ||
| function _estimate_opnorm(B, ::Type{T}; kwargs...) where {T} | ||
| _, s, _ = tsvd(B, 1; kwargs...) | ||
| return s[1], true | ||
| end | ||
|
|
||
| # Optimized path for standard FloatingPoint/Complex types | ||
| # Note: Arpack strictly only supports Float32, Float64, ComplexF32, and ComplexF64. | ||
| # Any other type (like BigFloat or Float16) must use the TSVD fallback above. | ||
| function _estimate_opnorm( | ||
| B, | ||
| ::Type{T}; | ||
| kwargs... | ||
| ) where {T <: Union{Float32, Float64, ComplexF32, ComplexF64}} | ||
| if ishermitian(B) | ||
| return opnorm_eig(B; kwargs...) | ||
| else | ||
| return opnorm_svd(B; kwargs...) | ||
| end | ||
| end | ||
|
|
||
| function opnorm_eig(B; max_attempts::Int = 3, tiny_dense_threshold = 5, kwargs...) | ||
| n = size(B, 1) | ||
|
|
||
| # Check if we can use direct dense methods (only if B supports conversion to Matrix) | ||
| if n ≤ tiny_dense_threshold | ||
| try | ||
| return maximum(abs, eigen(Matrix(B)).values), true | ||
| catch | ||
| # If Matrix(B) fails or isn't efficient, fall through to iterative | ||
| end | ||
| end | ||
|
|
||
| nev = 1 | ||
| ncv = max(20, 2*nev + 1) | ||
|
|
||
| for attempt = 1:max_attempts | ||
| try | ||
| d, nconv, _, _, _ = eigs(B; nev = nev, ncv = ncv, which = :LM, ritzvec = false, check = 1, kwargs...) | ||
|
|
||
| if nconv == 1 | ||
| return abs(d[1]), true | ||
| end | ||
|
|
||
| catch e | ||
| if e isa Arpack.ARPACKException || | ||
| occursin("ARPACK", string(e)) || | ||
| occursin("AUPD", string(e)) | ||
| if ncv >= n | ||
| @warn "ARPACK failed and NCV cannot be increased further." exception=e | ||
| rethrow(e) | ||
| end | ||
| else | ||
| rethrow(e) | ||
| end | ||
| end | ||
|
|
||
| if attempt < max_attempts | ||
| old_ncv = ncv | ||
| ncv = min(2 * ncv, n) | ||
| if ncv > old_ncv | ||
| @warn "opnorm_eig: increasing NCV from $old_ncv to $ncv and retrying." | ||
| else | ||
| break | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return NaN, false | ||
| end | ||
|
|
||
| function opnorm_svd(B; max_attempts::Int = 3, tiny_dense_threshold = 5, kwargs...) | ||
| m, n = size(B) | ||
|
|
||
| if min(m, n) ≤ tiny_dense_threshold | ||
| try | ||
| return maximum(svd(Matrix(B)).S), true | ||
| catch | ||
| # Fallback | ||
| end | ||
| end | ||
|
|
||
| min_dim = min(m, n) | ||
|
|
||
| nsv = 1 | ||
| ncv = 10 | ||
|
|
||
| for attempt = 1:max_attempts | ||
| try | ||
| s, nconv, _, _, _ = svds(B; nsv = nsv, ncv = ncv, ritzvec = false, check = 1, kwargs...) | ||
|
|
||
| if nconv >= 1 | ||
| return maximum(s.S), true | ||
| end | ||
|
|
||
| catch e | ||
| if e isa Arpack.ARPACKException || | ||
| occursin("ARPACK", string(e)) || | ||
| occursin("AUPD", string(e)) | ||
| if ncv >= min_dim | ||
| @warn "ARPACK failed and NCV cannot be increased further." exception=e | ||
| rethrow(e) | ||
| end | ||
| else | ||
| rethrow(e) | ||
| end | ||
| end | ||
|
|
||
| if attempt < max_attempts | ||
| old_ncv = ncv | ||
| ncv = min(2 * ncv, min_dim) | ||
| if ncv > old_ncv | ||
| @warn "opnorm_svd: increasing NCV from $old_ncv to $ncv and retrying." | ||
| else | ||
| break | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return NaN, false | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using TSVD | ||
| using Arpack | ||
| using GenericLinearAlgebra | ||
|
|
||
| @testset "estimate_opnorm type stability and dispatch" begin | ||
| @testset "Type $T" for T in [Float32, Float64, ComplexF32, ComplexF64] | ||
| # A) Rectangular Matrix (Forces SVD path) | ||
| J_mat = zeros(T, 3, 2) | ||
| J_mat[1, 1] = 3.0 | ||
| J_mat[2, 2] = 1.0 | ||
| J_op = LinearOperator(J_mat) | ||
|
|
||
| σ, ok = estimate_opnorm(J_op) | ||
| @test ok | ||
| @test isapprox(σ, 3.0; rtol = 1e-5) | ||
|
|
||
| # B) Hermitian Wrapper (Forces Eig path via dispatch) | ||
| H_data = rand(T, 10, 10) | ||
| H_data = H_data + H_data' | ||
| H = Hermitian(H_data) | ||
|
|
||
| H_op = LinearOperator(H) | ||
|
|
||
| exact_norm = opnorm(H) # Uses standard LinearAlgebra | ||
| est_norm, ok = estimate_opnorm(H_op) # Uses our extension | ||
|
|
||
| @test ok | ||
| @test isapprox(est_norm, exact_norm; rtol = 1e-5) | ||
|
|
||
| # C) Symmetric Wrapper (Dispatch varies by Real vs Complex) | ||
| S_data = rand(T, 10, 10) | ||
| S_data = S_data + transpose(S_data) | ||
| S = Symmetric(S_data) | ||
|
|
||
| S_op = LinearOperator(S) | ||
|
|
||
| exact_norm_S = opnorm(Matrix(S)) | ||
| est_norm_S, ok_S = estimate_opnorm(S_op) | ||
|
|
||
| @test ok_S | ||
| @test isapprox(est_norm_S, exact_norm_S; rtol = 1e-5) | ||
|
|
||
| # C.1) Specific check: Ensure Symmetric{Complex} works | ||
| # (This is NOT Hermitian, so it must successfully fall back to the SVD path) | ||
| if T <: Complex | ||
| # Ensure the operator didn't falsely flag itself as Hermitian | ||
| @test !ishermitian(S_op) | ||
| end | ||
| end | ||
|
|
||
| @testset "BigFloat (Generic)" begin | ||
| # ------------------------- | ||
| B_mat = Matrix{BigFloat}([2.0 0.0; 0.0 -1.0]) | ||
| B_op = LinearOperator(B_mat) | ||
|
|
||
| λ_bf, ok_bf = estimate_opnorm(B_op) | ||
|
|
||
| @test ok_bf | ||
| @test isapprox(λ_bf, BigFloat(2); rtol = 1e-12) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.