# Issue computing eigenvalues/vectors and orthonormalization

**URL:** <https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 20, 2024, 8:11am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835 "2024-11-20T08:11:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sparsetacus](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@Sparsetacus](https://discourse.julialang.org/u/Sparsetacus)\
**Post date:** [November 20, 2024, 8:11am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/1 "2024-11-20T08:11:30Z")

</div>

Hi there,

I am having trouble to compute eigenvectors and orthonormalize them.

I try to solve the generalized eigenvalues problem associated to matrices `A` and `B`.

`A` is not symmetric; `B` is symmetric but not necessary definit positive.

As `A` is not symmetric, one has to compute right and left eigenvectors, respectively `X` and `Y`.

After some tries, I managed to compute them (`eigs` from `Arpack.jl` was a fail; I ended up using `ArnoldiMethod.jl` with the [shift and invert method](https://julialinearalgebra.github.io/ArnoldiMethod.jl/dev/#Shift-and-invert-for-generalized-eigenvalue-problems); `KrylovKit.jl` does not apply as `A` is not symmetric nore hermitian).

So, I have now `X` and `Y` ans I want to _bi-orthonormalize_ them, i.e., `Y^T * B * X = Id` (where `^T` is the hermitian transpose) and `Y^T * A * X = L` with `L` matrix with the eigenvalues on diagonal.

I do this by computing `Y^T * B * X = D` and by doing for example `X <= X * inv(D)` I achieve to have `Y^T * B * X = Id`.

But `Y^T * A * X` works badly. The eigenvalues are on the diagonal. But extra diagonal terms are very _large_ (`1e-2` in norm) when it comes to the last eigenvalues (I compute 10 of them).

So here are my questions :

- Is there another way to compute eigenvalues/vectors efficiently ?
- Why is the orthonormalization working so badly ? It works well on `matlab`.

---

<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 20, 2024, 8:33am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/2 "2024-11-20T08:33:52Z")

</div>

> [@Sparsetacus](#):
>
> `KrylovKit.jl` does not apply as `A` is not symmetric nore hermitian).

KK does not require this!!

---

<div class="post-metadata">

**Author:** ![Sparsetacus](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@Sparsetacus](https://discourse.julialang.org/u/Sparsetacus)\
**Post date:** [November 20, 2024, 9:00am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/3 "2024-11-20T09:00:34Z")

</div>

I have the following error :

```julia
ERROR: LoadError: ArgumentError: Only symmetric or hermitian generalized eigenvalue problems with positive definite `B` matrix are currently supported.

```

---

<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 20, 2024, 9:27am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/4 "2024-11-20T09:27:42Z")

</div>

for GEV, you need hermitian B.

---

<div class="post-metadata">

**Author:** ![Sparsetacus](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@Sparsetacus](https://discourse.julialang.org/u/Sparsetacus)\
**Post date:** [November 20, 2024, 9:31am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/5 "2024-11-20T09:31:16Z")

</div>

The B matrix I use is hermitian.

`issymmetric(B) = true`

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [November 21, 2024, 2:47am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/6 "2024-11-21T02:47:33Z")

</div>

Your scheme works for me with results from ArnoldiMethod.jl on benign matrices, but your remarks suggest that your case is not so easy. Have you used a small tolerance and checked that the residuals satisfy it? Experimenting with different shifts may also help.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [November 21, 2024, 4:04am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/7 "2024-11-21T04:04:01Z")

</div>

Maybe the eigenvalues at the edges of the window are badly converged? (I have no experience with ArnoldiMethod.jl but KrylovKit.jl does ensure convergence of the requested number of eigenvectors IIRC).

A quick check is to just request some more vectors and compare the resulting matrices.

Out of curiosity: How large are your matrices?

---

<div class="post-metadata">

**Author:** ![Sparsetacus](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@Sparsetacus](https://discourse.julialang.org/u/Sparsetacus)\
**Post date:** [November 21, 2024, 7:27am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/8 "2024-11-21T07:27:16Z")

</div>

You mean with symmetric `B` and non symmetric `A` ?

I did not try on other matrices. I should give it a try.

---

<div class="post-metadata">

**Author:** ![Sparsetacus](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@Sparsetacus](https://discourse.julialang.org/u/Sparsetacus)\
**Post date:** [November 21, 2024, 7:31am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/9 "2024-11-21T07:31:00Z")

</div>

Yes, I could compute more eigenvalues so that the ones I want are more converged.

My matrices are not so large, 30,000 x 30,000, and very sparse.

---

<div class="post-metadata">

**Author:** ![Sparsetacus](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@Sparsetacus](https://discourse.julialang.org/u/Sparsetacus)\
**Post date:** [November 21, 2024, 7:50am UTC](https://discourse.julialang.org/t/issue-computing-eigenvalues-vectors-and-orthonormalization/122835/10 "2024-11-21T07:50:46Z")

</div>

Ok it seems to work. Thanks !

I also tighten a bit the tolerance and it also improves the results.
