# \`eigen\` with \`sortby\` does not work for \`StaticArrays\`

**URL:** https://discourse.julialang.org/t/eigen-with-sortby-does-not-work-for-staticarrays/125682
**Category:** General Usage
**Tags:** staticarrays, eigenvalue-problem
**Created:** [February 8, 2025, 8:43am UTC](https://discourse.julialang.org/t/eigen-with-sortby-does-not-work-for-staticarrays/125682 "2025-02-08T08:43:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [February 8, 2025, 8:43am UTC](https://discourse.julialang.org/t/eigen-with-sortby-does-not-work-for-staticarrays/125682/1 "2025-02-08T08:43:04Z")

</div>

As reported in the following post, `eigen` can be called with a `sortby` argument to provide the output in a different order, e.g. in descending order of the eigenvalues’ real part instead of the default.

> [@Eigen values and vectors sorting](https://discourse.julialang.org/t/eigen-values-and-vectors-sorting/98637/8):
>
> Note that the eigen function takes a parameter sortby that is a function specifying how you want them to be sorted. If you want them sorted in descending order by the real part, you can just pass sortby=-: julia\> A = [i^2 + i\*j + j^2 for i=1:4, j=1:4] 4×4 Matrix{Int64}: 3 7 13 21 7 12 19 28 13 19 27 37 21 28 37 48 julia\> eigen(A, sortby=-) Eigen{Float64, Float64, Matrix{Float64}, Vector{Float64}} values: 4-element Vector{Float64}: 97.34671140716996 0.11020703271515231 …

However, this does not work if the input is a matrix of `StaticArrays`, e.g.:

```julia
julia> eigen(SMatrix{3,3}(randn(9)), sortby=-)
ERROR: MethodError: no method matching eigen(::SMatrix{3, 3, Float64, 9}; sortby::typeof(-))

```

According to the error message, this seems to be a limitation of the type `SMatrix`, but perhaps also of the `LinearAlgebra` module in the way it generalizes the behavior of `AbstractMatrix`.

So, should this be reported in StaticArrays.jl or rather in julialang? Or perhaps in none of them, because this is an unavoidable limitation (e.g. due to the unmutability of `SMatrix`)?

---

<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: [February 8, 2025, 12:29pm UTC](https://discourse.julialang.org/t/eigen-with-sortby-does-not-work-for-staticarrays/125682/2 "2025-02-08T12:29:59Z")

</div>

The [`eigen` documentation](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.eigen) says:

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

So, it’s not a bug, but more a missing feature — you could request it at StaticArrays.jl.

> [@heliosdrm](#):
>
> Or perhaps in none of them, because this is an unavoidable limitation (e.g. due to the unmutability of `SMatrix`)?

It should definitely be possible to implement. immutability is not an issue — the whole point of StaticArrays is that it is cheap to work “out-of-place” (which doesn’t actually allocate on the heap), so it can just call `sort`.

Note also that for non-Hermitian arrays, `eigen` on `SMatrix` just case the generic `Matrix` function and converts back to `SMatrix`, so it could pass through the `sortby` keyword. By the same token, however, there is no performance advantage to using `SMatrix` in this case. (That being said, if you are calling `eigen` on arbitrary non-[normal](https://en.wikipedia.org/wiki/Normal_matrix) matrices you might want to re-think your overall approach.)
