# Eigenvalue ordering unexpectedly different in two cases

**URL:** <https://discourse.julialang.org/t/eigenvalue-ordering-unexpectedly-different-in-two-cases/99189>\
**Category:** Numerics\
**Created:** [May 21, 2023, 8:45pm UTC](https://discourse.julialang.org/t/eigenvalue-ordering-unexpectedly-different-in-two-cases/99189 "2023-05-21T20:45:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikos\_Gianniotis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikos_gianniotis/32/11487_2.png) [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)\
**Post date:** [May 21, 2023, 8:45pm UTC](https://discourse.julialang.org/t/eigenvalue-ordering-unexpectedly-different-in-two-cases/99189/1 "2023-05-21T20:45:25Z")

</div>

Hello everyone, I am probably making a subtle mistake or assumption somewhere as I cannot understand why the following two eigendecompositions do not return the eigenvalues in the same order.  
Could somebody please explain to me why this is the case?

Here is a MWE:

```julia
using Distributions, LinearAlgebra

D = Diagonal([2.0; 1.0])

Q = MvNormal([0.0;0.0], D)

eigen(cov(Q)).values # 1st eigenvalue decomposition. I get eigenvalues ordered as 1.0, 2.0

eigen(D).values # 2nd eigenvalue decomposition. I get eigenvalues ordered as 2.0, 1.0

```

Many thanks.

---

<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:** [May 21, 2023, 8:51pm UTC](https://discourse.julialang.org/t/eigenvalue-ordering-unexpectedly-different-in-two-cases/99189/2 "2023-05-21T20:51:25Z")

</div>

> [@Nikos\_Gianniotis](#):
>
> Could somebody please explain to me why this is the case?

For `Diagonal` it always returns the eigenvalues in the order they appear in the diagonal, so that the eigenvectors are the identity matrix. For most other matrix types it sorts the eigenvalues in ascending order (by real part), though you can change this using the `eigsortby` argument.

This is explained in the [docs for `eigen`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.eigen), which say:

> By default, the eigenvalues and vectors are sorted lexicographically by `(real(λ),imag(λ))` . A different comparison function `by(λ)` can be passed to `sortby` , or you can pass `sortby=nothing` to leave the eigenvalues in an arbitrary order. Some special matrix types (e.g. [`Diagonal`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.Diagonal) or [`SymTridiagonal`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.SymTridiagonal)) may implement their own sorting convention and not accept a `sortby` keyword.

---

<div class="post-metadata">

**Author:** ![Nikos\_Gianniotis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikos_gianniotis/32/11487_2.png) [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)\
**Post date:** [May 21, 2023, 8:52pm UTC](https://discourse.julialang.org/t/eigenvalue-ordering-unexpectedly-different-in-two-cases/99189/3 "2023-05-21T20:52:23Z")

</div>

Thank you. I read the help multiple times, but somehow overlooked the mention of Diagonal.
