Skip to content

Add opnorm implementation #375

Open
farhadrclass wants to merge 41 commits into
JuliaSmoothOptimizers:mainfrom
Farhad-phd:opNorm_branch
Open

Add opnorm implementation #375
farhadrclass wants to merge 41 commits into
JuliaSmoothOptimizers:mainfrom
Farhad-phd:opNorm_branch

Conversation

@farhadrclass
Copy link
Copy Markdown
Contributor

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

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
@farhadrclass farhadrclass requested a review from dpo August 21, 2025 21:15
@farhadrclass
Copy link
Copy Markdown
Contributor Author

@tmigot and @MaxenceGollier

@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 21, 2025

Codecov Report

❌ Patch coverage is 96.87500% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.93%. Comparing base (32dbc5e) to head (274e336).
⚠️ Report is 77 commits behind head on main.

Files with missing lines Patch % Lines
ext/LinearOperatorsOpNormExt.jl 96.87% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

tmigot
tmigot previously requested changes Aug 22, 2025
Copy link
Copy Markdown
Member

@tmigot tmigot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @farhadrclass that will be a great add. I made a first batch of comments

Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread test/test_opnorm.jl Outdated
Comment thread Project.toml Outdated
Added Arpack to Project.toml dependencies and compat section. Updated utilities.jl to import Arpack, preparing for usage of its functionality.
Copilot AI review requested due to automatic review settings January 20, 2026 16:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 opnorm function 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.

Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread test/test_opnorm.jl Outdated
Comment thread test/test_opnorm.jl Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/utilities.jl
Comment thread src/utilities.jl
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread test/test_estimate_opnorm.jl Outdated
Comment thread src/utilities.jl Outdated
Copy link
Copy Markdown
Contributor Author

@farhadrclass farhadrclass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@farhadrclass
Copy link
Copy Markdown
Contributor Author

@tmigot and @dpo I updated the code, and also used AI to code review (to test if AI can help us automate the review)
I am now pushing the changes,

Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
@dpo
Copy link
Copy Markdown
Member

dpo commented Jan 23, 2026

@farhadrclass All tests fail and the branch must be rebased.

We also need to hear from @MohamedLaghdafHABIBOULLAH and @MaxenceGollier.

farhadrclass and others added 9 commits January 23, 2026 16:11
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>
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.
farhadrclass and others added 2 commits April 7, 2026 14:12
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
@farhadrclass farhadrclass requested a review from tmigot April 7, 2026 18:21
@farhadrclass
Copy link
Copy Markdown
Contributor Author

@tmigot Thanks for your feedbacks

Here is the most updated code,
Let me know so we can push this branch in so I can use it in my JSOSolvers

@farhadrclass
Copy link
Copy Markdown
Contributor Author

hey
@tmigot ,

I was wondering if you had a chance to look at this ?

Copy link
Copy Markdown
Member

@tmigot tmigot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a few comments, it looks good overall. Thanks @farhadrclass

Comment thread src/utilities.jl
Comment thread src/utilities.jl
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
Comment thread src/utilities.jl Outdated
farhadrclass and others added 2 commits April 23, 2026 11:44
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
@farhadrclass
Copy link
Copy Markdown
Contributor Author

@tmigot
Thanks for your suggestions,
I updated them
@dpo I think it is ready for merging, I am going to re-base and send the final changes

@farhadrclass farhadrclass dismissed MaxenceGollier’s stale review May 11, 2026 18:20

I think I changed too much since then and I can not see your review

@farhadrclass
Copy link
Copy Markdown
Contributor Author

@dpo I added more tests to make sure the codecov passes

@farhadrclass farhadrclass requested a review from tmigot May 12, 2026 13:22
Copy link
Copy Markdown
Member

@tmigot tmigot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/utilities.jl Outdated
Comment thread ext/LinearOperatorsOpNormExt.jl Outdated
Comment thread ext/LinearOperatorsOpNormExt.jl Outdated
Comment thread ext/LinearOperatorsOpNormExt.jl Outdated
Comment thread ext/LinearOperatorsOpNormExt.jl Outdated
Comment thread ext/LinearOperatorsOpNormExt.jl Outdated
Comment thread ext/LinearOperatorsOpNormExt.jl Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_svd returns NaN as a Float64 literal, which makes the return type inconsistent for Float32/ComplexF32 operators. Return a NaN value of the appropriate real element type so callers (and inference) don’t see type instability when success == false.
  end

  return NaN, false
end

src/utilities.jl:320

  • The PR title/description mention adding an opnorm implementation, but the public API added here is estimate_opnorm (exported) with no visible LinearAlgebra.opnorm(::AbstractLinearOperator, ...) method. If the intent is to let users call opnorm(op) directly, consider adding the appropriate LinearAlgebra.opnorm method (possibly delegating to estimate_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

Comment on lines +4 to +9
using Arpack
using TSVD
using LinearAlgebra
using GenericLinearAlgebra
import Arpack: eigs, svds

Comment on lines +83 to +85

return NaN, false
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@farhadrclass this is a good comment to adress

farhadrclass and others added 8 commits May 21, 2026 08:05
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.
@farhadrclass
Copy link
Copy Markdown
Contributor Author

@tmigot I updated the code and make sure the formatter run
also I fix the NaN type

Thanks for catching them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants