# Pre-allocated eigen value decomposition

**URL:** <https://discourse.julialang.org/t/pre-allocated-eigen-value-decomposition/79358>\
**Category:** Numerics\
**Created:** [April 11, 2022, 8:46pm UTC](https://discourse.julialang.org/t/pre-allocated-eigen-value-decomposition/79358 "2022-04-11T20:46:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)\
**Post date:** [April 11, 2022, 8:46pm UTC](https://discourse.julialang.org/t/pre-allocated-eigen-value-decomposition/79358/1 "2022-04-11T20:46:15Z")

</div>

Is there a way to pre-allocate an array to hold the returned eigenvalues and vectors from `eigen` or `eigen!` ? Currently both functions return a factorization which does not seem to allow a pre-allocation. The following code shows what I would like to do but does not work:

```julia
julia> A=rand(100,100);

julia> evals=zeros(CF64,100);

julia> evecs=zeros(CF64,100,100);

julia> julia> esys=Eigen(evals,evecs);

julia> esys.values[1]
0.0 + 0.0im

julia> evals[1]=1;

julia> esys.values[1]
1.0 + 0.0im

julia> esys = eigen(A)

julia> esys.values[1]
-3.0686209886339606 - 1.1725958631992774im   

julia> evals[1]
1.0 + 0.0im

```

Here `CF64=ComplexF64`. I would hope that `eigen` would write to `evals` and `evecs` but apparently it does not. I can also do something like this:

```julia
pre-allocated-eigen!(A,evals,evecs);

```

I haven’t found such a function though. Any suggestions? Or is there a reason why this is not making sense?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 11, 2022, 10:05pm UTC](https://discourse.julialang.org/t/pre-allocated-eigen-value-decomposition/79358/2 "2022-04-11T22:05:14Z")

</div>

If there is no support for preallocated results I’d assume the cost of allocating is negligible in comparison to the actual computation?

---

<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:** [April 11, 2022, 10:07pm UTC](https://discourse.julialang.org/t/pre-allocated-eigen-value-decomposition/79358/3 "2022-04-11T22:07:07Z")

</div>

> [@jinml](#):
>
> Is there a way to pre-allocate an array to hold the returned eigenvalues and vectors from `eigen` or `eigen!`

You might have to call the lower-level LAPACK routines directly. (Preallocation shouldn’t make much difference unless your matrices are fairly small.)
