# Eigenvector for small non-Hermitian matrices

**URL:** <https://discourse.julialang.org/t/eigenvector-for-small-non-hermitian-matrices/83204>\
**Category:** Performance\
**Tags:** question, performance, linearalgebra\
**Created:** [June 22, 2022, 9:00pm UTC](https://discourse.julialang.org/t/eigenvector-for-small-non-hermitian-matrices/83204 "2022-06-22T21:00:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)\
**Post date:** [June 22, 2022, 9:00pm UTC](https://discourse.julialang.org/t/eigenvector-for-small-non-hermitian-matrices/83204/1 "2022-06-22T21:00:57Z")

</div>

I have to calculate the eigenvector and eigenvalues for many (more than 60.000) non-Hermitian 4x4 matrices. I know that this can be slightly speed up by means of using _LinearAlgebra.LAPACK.geevx!_ instead of using the _eigen()_ function itself. However, if I would do this in C++ and use the eigen library for C++ (using the compiling option _O3_ and _fastmath_) I can even speed up this calculation by a factor 2 … 3. Is there an opportunity to reach the same speed like the C++ eigen library by a Package for instance? Using the option _@fastmath_ in Julia does not change the performance.

---

<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:** [June 22, 2022, 9:06pm UTC](https://discourse.julialang.org/t/eigenvector-for-small-non-hermitian-matrices/83204/2 "2022-06-22T21:06:25Z")

</div>

`StaticArrays` should be better.

---

<div class="post-metadata">

**Author:** ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)\
**Post date:** [June 23, 2022, 7:23am UTC](https://discourse.julialang.org/t/eigenvector-for-small-non-hermitian-matrices/83204/3 "2022-06-23T07:23:36Z")

</div>

Thanks for the answer. But if I haven’t overseen it, `StaticArrays` has no `eigen()` function for non-Hermitian matrices and use than the built-in function of julia, so that result is the same. E.g.,

> using BenchmarkTools  
> using LinearAlgebra  
> using StaticArrays
> 
> m = rand(4,4) + im\*rand(4,4);  
> M = SMatrix{4,4, ComplexF64}(m)  
> @benchmark eigen($m)  
> @benchmark eigen($M)

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [June 23, 2022, 1:11pm UTC](https://discourse.julialang.org/t/eigenvector-for-small-non-hermitian-matrices/83204/4 "2022-06-23T13:11:30Z")

</div>

Related discussion: [Allocations when calling eigen for hermitian 3x3 SMatrix](https://discourse.julialang.org/t/allocations-when-calling-eigen-for-hermitian-3x3-smatrix/80994)

---

<div class="post-metadata">

**Author:** ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)\
**Post date:** [June 24, 2022, 8:37am UTC](https://discourse.julialang.org/t/eigenvector-for-small-non-hermitian-matrices/83204/5 "2022-06-24T08:37:56Z")

</div>

Thanks for the link. But this seems to be quite limited to hermitian matrices and only for size of 3x3

---

<div class="post-metadata">

**Author:** ![alastair-marshall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alastair-marshall/32/21418_2.png) [@alastair-marshall](https://discourse.julialang.org/u/alastair-marshall)\
**Post date:** [June 24, 2022, 9:44am UTC](https://discourse.julialang.org/t/eigenvector-for-small-non-hermitian-matrices/83204/6 "2022-06-24T09:44:31Z")

</div>

Are you perhaps missing a parameter in the SMatrix declaration?

```julia
SMatrix{4,4,ComplexF64,16}

```

Is needed I think?

---

<div class="post-metadata">

**Author:** ![itzui](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@itzui](https://discourse.julialang.org/u/itzui)\
**Post date:** [June 24, 2022, 10:05am UTC](https://discourse.julialang.org/t/eigenvector-for-small-non-hermitian-matrices/83204/7 "2022-06-24T10:05:53Z")

</div>

The above mentioned example still works on my PC (julia 1.7.3) even without the _16_ in the declaration. Anyway, even if I add this in the declaration, the evaluation time is roughly the same.

`@benchmark eigen($m)` leads to

```julia
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 12.680 μs … 258.653 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 14.110 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 16.117 μs ± 7.016 μs ┊ GC (mean ± σ): 0.00% ± 0.00%

 Memory estimate: 10.23 KiB, allocs estimate: 22.

```

while for `StaticArrays`, i.e. `@benchmark eigen($M)` was used I get

```julia
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 12.849 μs … 11.458 ms ┊ GC (min … max): 0.00% … 99.67%
 Time (median): 14.216 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 16.555 μs ± 114.533 μs ┊ GC (mean ± σ): 6.90% ± 1.00%

 Memory estimate: 10.89 KiB, allocs estimate: 24.

```

which is quite similar
