# Reusing eigendecomposition for matrix exponentials

**URL:** <https://discourse.julialang.org/t/reusing-eigendecomposition-for-matrix-exponentials/50528>\
**Category:** General Usage\
**Created:** [November 21, 2020, 11:22am UTC](https://discourse.julialang.org/t/reusing-eigendecomposition-for-matrix-exponentials/50528 "2020-11-21T11:22:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [November 21, 2020, 11:22am UTC](https://discourse.julialang.org/t/reusing-eigendecomposition-for-matrix-exponentials/50528/1 "2020-11-21T11:22:49Z")

</div>

Looking into the documentation, `exp` is using the `eigen` decomposition to calculate the exponetial `exp(A)` of a matrix `A`. Is there a function in Julia/BLAS to compute `exp(c*A)` for a constant, possible imaginary, `c` for a lot of different `c` but the same `A`, reusing the `eigen` decomposition of `A`?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 21, 2020, 11:28am UTC](https://discourse.julialang.org/t/reusing-eigendecomposition-for-matrix-exponentials/50528/2 "2020-11-21T11:28:35Z")

</div>

> [@jamblejoe](#):
>
> `exp` is using the `eigen` decomposition to calculate the exponetial `exp(A)` of a matrix `A` .

Only for symmetric/Hermitian matrices. For general dense matrices, it is scaling and squaring.

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [November 21, 2020, 11:29am UTC](https://discourse.julialang.org/t/reusing-eigendecomposition-for-matrix-exponentials/50528/3 "2020-11-21T11:29:14Z")

</div>

Ah yes, sure. I meant `A` to be symmetric/hermitian.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [November 21, 2020, 12:58pm UTC](https://discourse.julialang.org/t/reusing-eigendecomposition-for-matrix-exponentials/50528/4 "2020-11-21T12:58:57Z")

</div>

I think it would be quite easy to implement `exp(::LinearAlgebra.Eigen)`, so you could just compute `eigen(A)` once, scale the eigenvalues and compute the exponential of those. That would probably make quite a straightforward PR, if you want to give it a go.

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [November 21, 2020, 1:28pm UTC](https://discourse.julialang.org/t/reusing-eigendecomposition-for-matrix-exponentials/50528/5 "2020-11-21T13:28:40Z")

</div>

Yes, I was just a bit curious if the implementation does some smart tricks, but it is implemented for hermitian matrices as

```julia
F = eigen(A)
Symmetric((F.vectors * Diagonal(($func).(F.values))) * F.vectors')

```

Where `func` are the normal candidates for functions implemented in a standard linear algebra library. The most general use case would be achieved by having a function applying an (arbitrary) function to the matrix `A`. Would something like

```julia
applyfunction(func, eigendecomp) = Symmetric((eigendecomp.vectors * Diagonal((func).(eigendecomp.values))) * eigendecomp.vectors')

```

make sense? I do not know how sensible the compiler is to passing functions as arguments.

Should I open an issue on github and move the discussion there?

---

<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:** [November 21, 2020, 1:29pm UTC](https://discourse.julialang.org/t/reusing-eigendecomposition-for-matrix-exponentials/50528/6 "2020-11-21T13:29:05Z")

</div>

> [@jamblejoe](#):
>
> Is there a function in Julia/BLAS to compute `exp(c*A)` for a constant, possible imaginary, `c` for a lot of different `c` but the same `A` , reusing the `eigen` decomposition of `A` ?

This should do it:

```julia
exp_scaled(c::Number, F::Eigen) = F.vectors * Diagonal(exp.(c .* F.values)) * F.vectors'

```

I’m not sure a library function is really needed for something so simple…

---

<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:** [November 21, 2020, 1:32pm UTC](https://discourse.julialang.org/t/reusing-eigendecomposition-for-matrix-exponentials/50528/7 "2020-11-21T13:32:32Z")

</div>

> [@jamblejoe](#):
>
> I do not know how sensible the compiler is to passing functions as arguments.

The compiler is _very_ good about higher-order functions, and can even inline the function if you [add a type parameter](https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing).
