# How to calculate opnorm for a sparse matrix in julia?

**URL:** <https://discourse.julialang.org/t/how-to-calculate-opnorm-for-a-sparse-matrix-in-julia/120820>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 2, 2024, 4:25pm UTC](https://discourse.julialang.org/t/how-to-calculate-opnorm-for-a-sparse-matrix-in-julia/120820 "2024-10-02T16:25:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![usaer](https://avatars.discourse-cdn.com/v4/letter/u/b9bd4f/32.png) [@usaer](https://discourse.julialang.org/u/usaer)\
**Post date:** [October 2, 2024, 4:25pm UTC](https://discourse.julialang.org/t/how-to-calculate-opnorm-for-a-sparse-matrix-in-julia/120820/1 "2024-10-02T16:25:02Z")

</div>

Please guide on how to calculate the opnorm for p=2 in julia for sparse matrices. When I try I get the error  
ArgumentError: 2-norm not yet implemented for sparse matrices. Try opnorm(Array(A)) or opnorm(A, p) where p=1 or Inf.

My code:

```julia
using SparseArrays
using LinearAlgebra

# Define a sparse matrix
A = sparse([1, 2], [1, 2], [1.0, 5.0], 2, 2)

# Calculate the 2-norm operator norm (default)
opnorm_value = opnorm(A)

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 2, 2024, 4:27pm UTC](https://discourse.julialang.org/t/how-to-calculate-opnorm-for-a-sparse-matrix-in-julia/120820/2 "2024-10-02T16:27:01Z")

</div>

Do you want the actual `opnorm` or are you OK with an approximation? The reason it isn’t implemented is that the 2 norm is usually computed with an SVD which would be really slow for sparse matrices.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 2, 2024, 4:59pm UTC](https://discourse.julialang.org/t/how-to-calculate-opnorm-for-a-sparse-matrix-in-julia/120820/3 "2024-10-02T16:59:03Z")

</div>

> [@Oscar\_Smith](#):
>
> Do you want the actual `opnorm` or are you OK with an approximation? The reason it isn’t implemented is that the 2 norm is usually computed with an SVD which would be really slow for sparse matrices.

In particular, the operator 2-norm \Vert A \Vert\_2 is exactly equal to the _largest_ singular value of A. For a large sparse matrix (or a matrix implicitly defined as a linear operator), you **need to compute this by iterative methods** , which are _not_ included in SparseArrays but are provided by many packages.

For example, you can use `KrylovKit.svdsolve(A)[1][1]` from KrylovKit.jl to get the largest singular value of a sparse matrix `A`, equivalent to the operator norm.

PS. I edited @usaer’s original post to quote the code properly. Please see [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

<div class="post-metadata">

**Author:** ![usaer](https://avatars.discourse-cdn.com/v4/letter/u/b9bd4f/32.png) [@usaer](https://discourse.julialang.org/u/usaer)\
**Post date:** [October 2, 2024, 5:04pm UTC](https://discourse.julialang.org/t/how-to-calculate-opnorm-for-a-sparse-matrix-in-julia/120820/4 "2024-10-02T17:04:31Z")

</div>

Thanks Alot!
