Add opnorm implementation #375
Conversation
Introduces a new opnorm function that efficiently computes the operator 2-norm for matrices and linear operators, dispatching to LAPACK, ARPACK, or TSVD as appropriate. Adds comprehensive tests for opnorm covering both Float64 and BigFloat types, and integrates the new test file into the test suite. Update Project.toml
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #375 +/- ##
==========================================
- Coverage 95.00% 94.93% -0.07%
==========================================
Files 17 22 +5
Lines 1100 1243 +143
==========================================
+ Hits 1045 1180 +135
- Misses 55 63 +8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
tmigot
left a comment
There was a problem hiding this comment.
Thanks @farhadrclass that will be a great add. I made a first batch of comments
Added Arpack to Project.toml dependencies and compat section. Updated utilities.jl to import Arpack, preparing for usage of its functionality.
There was a problem hiding this comment.
Pull request overview
This pull request introduces a new opnorm function to efficiently compute the operator 2-norm (largest singular value) for matrices and linear operators. The implementation uses a dispatching strategy that selects LAPACK for small dense matrices, ARPACK for larger matrices with Float32/Float64/ComplexF32/ComplexF64 element types, and TSVD for other element types like BigFloat.
Changes:
- Adds
opnormfunction with type-based dispatch for efficient norm computation - Adds comprehensive tests for Float64 and BigFloat matrix types
- Adds new dependencies: Arpack, GenericLinearAlgebra, and TSVD
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 18 comments.
| File | Description |
|---|---|
| src/utilities.jl | Implements opnorm function with dispatch to opnorm_eig for square matrices and opnorm_svd for rectangular matrices, with fallback to TSVD |
| test/test_opnorm.jl | Adds tests for square and rectangular matrices with Float64 and BigFloat types |
| test/runtests.jl | Integrates new test_opnorm.jl into the test suite |
| Project.toml | Adds Arpack, GenericLinearAlgebra, and TSVD dependencies with version constraints |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 4 changed files in this pull request and generated 15 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@farhadrclass All tests fail and the branch must be rebased. We also need to hear from @MohamedLaghdafHABIBOULLAH and @MaxenceGollier. |
Introduces a new opnorm function that efficiently computes the operator 2-norm for matrices and linear operators, dispatching to LAPACK, ARPACK, or TSVD as appropriate. Adds comprehensive tests for opnorm covering both Float64 and BigFloat types, and integrates the new test file into the test suite. Update Project.toml
Added Arpack to Project.toml dependencies and compat section. Updated utilities.jl to import Arpack, preparing for usage of its functionality.
Replaces the opnorm function with estimate_opnorm throughout the codebase for clarity. Updates all relevant documentation, exports, and test files to use the new function name, and renames the test file accordingly. Update utilities.jl Update test_estimate_opnorm.jl
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Dominique <dominique.orban@gmail.com>
05ba478 to
8b0da8c
Compare
Added Arpack, GenericLinearAlgebra, and TSVD as dependencies in Project.toml with corresponding compat entries. Updated test_estimate_opnorm.jl to use consistent spacing and formatting in test assertions.
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
|
@tmigot Thanks for your feedbacks Here is the most updated code, |
|
hey I was wondering if you had a chance to look at this ? |
tmigot
left a comment
There was a problem hiding this comment.
I made a few comments, it looks good overall. Thanks @farhadrclass
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
I think I changed too much since then and I can not see your review
|
@dpo I added more tests to make sure the codecov passes |
tmigot
left a comment
There was a problem hiding this comment.
LGTM @farhadrclass !
Minor comments, don't add comments line if they don't bring extra information. I tried to details a little bit to illustrate.
Also, I think you didn't use the formatter.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (2)
ext/LinearOperatorsOpNormExt.jl:134
- On failure to converge,
opnorm_svdreturnsNaNas aFloat64literal, which makes the return type inconsistent forFloat32/ComplexF32operators. Return a NaN value of the appropriate real element type so callers (and inference) don’t see type instability whensuccess == false.
end
return NaN, false
end
src/utilities.jl:320
- The PR title/description mention adding an
opnormimplementation, but the public API added here isestimate_opnorm(exported) with no visibleLinearAlgebra.opnorm(::AbstractLinearOperator, ...)method. If the intent is to let users callopnorm(op)directly, consider adding the appropriateLinearAlgebra.opnormmethod (possibly delegating toestimate_opnorm) or updating the PR description/docs to match the actual API being introduced.
"""
estimate_opnorm(B::AbstractLinearOperator; kwargs...)
Compute an estimate of the operator 2-norm (largest singular value) of a linear operator `B`.
This method dispatches to efficient algorithms depending on the properties and size of `B`:
- **Non-standard element types** (e.g., `BigFloat`, `Float16`): Always uses `TSVD.tsvd` as the fallback, since ARPACK only supports standard 32-bit and 64-bit floating-point types.
- **Small operators** (dimension ≤ `tiny_dense_threshold`): Attempts to convert `B` to a dense `Matrix` and uses direct LAPACK routines (`LinearAlgebra.eigen` if Hermitian, `LinearAlgebra.svd` otherwise).
- **Large, standard-type operators**:
- If `ishermitian(B)`: Uses `Arpack.eigs` to find the eigenvalue with the largest magnitude.
- Otherwise: Uses `Arpack.svds` to find the largest singular value.
**Note:** This function allocates memory. It requires `Arpack.jl` and `TSVD.jl`
(and `GenericLinearAlgebra.jl` for generic types) to be loaded.
# Arguments
- `B::AbstractLinearOperator`: The linear operator to analyze.
# Keyword Arguments
- `max_attempts::Int=3`: Maximum number of retries for iterative solvers if convergence fails, doubling the Krylov subspace dimension (`ncv`) each time.
- `tiny_dense_threshold::Int=5`: If the minimum dimension of `B` is less than or equal to this threshold, it attempts to use direct dense LAPACK routines.
- `kwargs...`: Additional optional keyword arguments passed to the underlying iterative routines (`Arpack.eigs`, `Arpack.svds`, or `TSVD.tsvd`).
# Returns
- A tuple `(norm, success)` where:
- `norm` is the estimated operator 2-norm of `B` (largest singular value or eigenvalue in absolute value).
- `success` is a boolean indicating whether the iterative method (if used) reported successful convergence.
"""
function estimate_opnorm end
| using Arpack | ||
| using TSVD | ||
| using LinearAlgebra | ||
| using GenericLinearAlgebra | ||
| import Arpack: eigs, svds | ||
|
|
|
|
||
| return NaN, false | ||
| end |
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Make opnorm_eig and opnorm_svd return a NaN value matching the real floating-point type of the input operator. The code now computes T = real(eltype(B)) and returns T(NaN), false to keep return types consistent and avoid type-instability or conversion issues when B has a specific element type.
|
@tmigot I updated the code and make sure the formatter run Thanks for catching them |
Introduces a new opnorm function that efficiently computes the operator 2-norm for matrices and linear operators, dispatching to LAPACK, ARPACK, or TSVD as appropriate. Adds comprehensive tests for opnorm covering both Float64 and BigFloat types, and integrates the new test file into the test suite.
Update Project.toml