# Efficient computation of largest eigenvalue

**URL:** <https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933>\
**Category:** General Usage\
**Tags:** performance, eigenvalues\
**Created:** [November 8, 2023, 10:10am UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933 "2023-11-08T10:10:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![VictorSeven](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victorseven/32/50437_2.png) [@VictorSeven](https://discourse.julialang.org/u/VictorSeven)\
**Post date:** [November 8, 2023, 10:10am UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/1 "2023-11-08T10:10:18Z")

</div>

For some numerical simulations I run, I need to construct a matrix and obtain its leading eigenvalue. I will make use of both of them.

I was using the `LinearAlgebra` package, and computing the leading eigenvalue as `maximum(real(eigvals(matrix)))`. When profiling the code, I noticed that this computation takes a noticeable amount of time, especially for larger matrices, so I am trying to speed it up.

I cannot use `eigvals!` because I need the matrix later and do not want to override it. Furthermore, I only need one eigenvalue. I found that `Arpack.jl` can compute the leading eigenvalue directly. It is way faster, but it fails to diagonalize some matrices, with the error

```julia
┌ Error: XYAUPD_Exception: Maximum number of iterations taken. All possible eigenvalues of OP has been found.
│ IPARAM(5) returns the number of wanted converged Ritz values.

```

Looking at the package issues, it seems that version 0.5.4 of the package is broken and leads to this problem. Other users have reported this problem, see [1](https://github.com/JuliaLinearAlgebra/Arpack.jl/issues/147), [2](https://github.com/JuliaLinearAlgebra/Arpack.jl/issues/149). Might be that some matrices actually cannot be solved by this method (the ones I am using are dense and large), but the error is not clear about it and the package does not seem to be maintained anymore. Other alternatives such as `KrylovKit.jl` or `Arnoldi.jl` also do not have any recent commit, nor answer to open issues, so they all seem to be unmaintained.

So I wonder what the supported, most efficient way of computing just a single eigenvalue is, and if there’s any plan to re-take `Arpack.jl` package again. The speed difference is noticeable, especially for large matrices.

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 8, 2023, 10:17am UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/2 "2023-11-08T10:17:47Z")

</div>

> [@VictorSeven](#):
>
> `KrylovKit.jl`

[https://jutho.github.io/KrylovKit.jl/latest/man/eig/#KrylovKit.eigsolve](https://jutho.github.io/KrylovKit.jl/latest/man/eig/#KrylovKit.eigsolve)

what didn’t work for you?

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [November 8, 2023, 10:21am UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/3 "2023-11-08T10:21:18Z")

</div>

Power method ?  
`[https://services.math.duke.edu/~jtwong/math361-2019/lectures/Lec10eigenvalues.pdf](https://services.math.duke.edu/~jtwong/math361-2019/lectures/Lec10eigenvalues.pdf)

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 8, 2023, 10:45am UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/4 "2023-11-08T10:45:23Z")

</div>

You can try to pass a sigma to Arpack and that should work

---

<div class="post-metadata">

**Author:** ![VictorSeven](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victorseven/32/50437_2.png) [@VictorSeven](https://discourse.julialang.org/u/VictorSeven)\
**Post date:** [November 8, 2023, 10:53am UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/5 "2023-11-08T10:53:52Z")

</div>

Regarding `KrylovKit.jl`, it seems to work for my case (a bit more of testing is needed but seems stable). The problem is that the package does not seem to be maintained, but I might be mistaken (is it?). I would like to rely on packages that will have some continuity.

About the use of sigma in `Arpack.jl`, @rveltz , as far as I understand I need an initial guess of the eigenvalues beforehand, which I might not have. Also, does this mean that the XYAUPD error is the expected behaviour of the package?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 8, 2023, 10:54am UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/6 "2023-11-08T10:54:45Z")

</div>

> [@VictorSeven](#):
>
> The problem is that the package does not seem to be maintained,

not updated doesn’t mean not maintained, maybe it’s just mature? The author is still active

---

<div class="post-metadata">

**Author:** ![VictorSeven](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victorseven/32/50437_2.png) [@VictorSeven](https://discourse.julialang.org/u/VictorSeven)\
**Post date:** [November 8, 2023, 10:57am UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/7 "2023-11-08T10:57:52Z")

</div>

Definitely, I was lurking around the issues and gave me some abandonware impression, but it seems I was wrong. Then KrylovKit seems to get the job done efficiently (speed is comparable to Arpack). Thanks!!

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [November 8, 2023, 12:36pm UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/8 "2023-11-08T12:36:36Z")

</div>

FWIW : I don’t know how efficiently `KrylovKit.jl` and `Arpack.jl` deal with the specialized largest eigenvalue case, but I used to work on a large project where the (accelerated inverse) power method was the fastest approach.

It may be interesting to check what gives non accelerated PM using:

[https://docs.juliahub.com/IterativeSolvers/ef2NV/0.8.5/eigenproblems/power\_method/](https://docs.juliahub.com/IterativeSolvers/ef2NV/0.8.5/eigenproblems/power_method/)

---

<div class="post-metadata">

**Author:** ![VictorSeven](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victorseven/32/50437_2.png) [@VictorSeven](https://discourse.julialang.org/u/VictorSeven)\
**Post date:** [November 8, 2023, 3:11pm UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/9 "2023-11-08T15:11:13Z")

</div>

I have tried, but the amount of iterations I need for the power method to converge to the correct result makes it way slower than the other available options. Even the function from `LinearAlgebra` package gives me better results when I run it with `@benchmark`.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 8, 2023, 3:48pm UTC](https://discourse.julialang.org/t/efficient-computation-of-largest-eigenvalue/105933/10 "2023-11-08T15:48:20Z")

</div>

> [@VictorSeven](#):
>
> About the use of sigma in `Arpack.jl`, @rveltz , as far as I understand I need an initial guess of the eigenvalues beforehand, which I might not have. Also, does this mean that the XYAUPD error is the expected behaviour of the package?

You might have an idea by now. You can then update it when you change the problem a little based on a previous computation
