# Symbolic matrix exponential

**URL:** <https://discourse.julialang.org/t/symbolic-matrix-exponential/85435>\
**Category:** General Usage\
**Tags:** linearalgebra, symbolic\
**Created:** [August 7, 2022, 11:11am UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435 "2022-08-07T11:11:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaguro](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Kaguro](https://discourse.julialang.org/u/Kaguro)\
**Post date:** [August 7, 2022, 11:11am UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/1 "2022-08-07T11:11:26Z")

</div>

Hello All! I am fairly new to Julia.

I want to find the matrix exponential of a symbolic matrix. How to do this?  
When I use the Symbolics and LinearAlgebra packages together, it doesn’t work:

```julia
using Symbolics
using LinearAlgebra

function run()
@variables a b c t
M = [ -a b 0
     1 c -a
     0 1 0]
Mt = M*t
expMt = exp(Mt)
print(expMt[1,1])
end

@time run()

```

Any help will be very appreciated!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 7, 2022, 11:52am UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/2 "2022-08-07T11:52:32Z")

</div>

Open an issue on Symbolics.jl. This isn’t implemented, and I’m not convinced there is a general symbolic solution that can be given on this operation. It’s worth a dev discussion.

---

<div class="post-metadata">

**Author:** ![Kaguro](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Kaguro](https://discourse.julialang.org/u/Kaguro)\
**Post date:** [August 7, 2022, 12:07pm UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/3 "2022-08-07T12:07:25Z")

</div>

Okay. I did that. I hope this gets implemented. Thanks.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 7, 2022, 12:15pm UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/4 "2022-08-07T12:15:18Z")

</div>

I’m not convinced it’s possible in general 😅 .

I just took a look and saw SymPy can only do it if the Jordan normal form exists for `A`. That’s probably a fine enough restriction.

> <https://github.com/sympy/sympy/blob/77f1d79c705da5e9b3dee456a14b1b4e92dd620c/sympy/matrices/matrices.py#L1649>

---

<div class="post-metadata">

**Author:** ![Ismam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ismam/32/26140_2.png) [@Ismam](https://discourse.julialang.org/u/Ismam)\
**Post date:** [February 10, 2023, 11:18am UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/5 "2023-02-10T11:18:58Z")

</div>

Wolfram Alpha is able ot spit out a result.  
[https://www.wolframalpha.com/input?i2d=true&i=matrix+exponential+%7B%7B-a%2Cb%2C0%7D%2C%7B1%2Cc%2C-a%7D%2C%7B0%2C1%2C0%7D%7D](https://www.wolframalpha.com/input?i2d=true&i=matrix+exponential+%7B%7B-a%2Cb%2C0%7D%2C%7B1%2Cc%2C-a%7D%2C%7B0%2C1%2C0%7D%7D)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/9/69576fe2cba82643950b9a38799bcef3edf07b59.png)

I can’t say if it’s correct. It looks hideous - but I think that’s to be expected.

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [February 10, 2023, 11:25am UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/6 "2023-02-10T11:25:25Z")

</div>

> [@ChrisRackauckas](#):
>
> I just took a look and saw SymPy can only do it if the Jordan normal form exists for `A`. That’s probably a fine enough restriction.

The Jordan form exists for all matrices. The only question is if it can be computed symbolically; this might require giving a name to solutions of polynomials of degree 5+ and work with them. Can Symbolics.jl do it?

---

<div class="post-metadata">

**Author:** ![Ismam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ismam/32/26140_2.png) [@Ismam](https://discourse.julialang.org/u/Ismam)\
**Post date:** [February 10, 2023, 11:41am UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/7 "2023-02-10T11:41:59Z")

</div>

That makes sense.

Do you think Wolfram Alpha is spitting out the right results then? I was confused as to what “omega” meant in my screenshot of Wolfram Alpha - and in light of your comment, I suspect it’s a solution to some \>5 degree polynomial.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 10, 2023, 12:30pm UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/8 "2023-02-10T12:30:00Z")

</div>

From the image it look like ω are the solutions (plural) to a 3rd degree polynomial. The expressions in ω are being summed over all solutions to this polynomial. Didn’t check it, but this is probably the characteristic polynomial of the matrix `M`. For 3x3 matrix there is a closed form of the solution. For \>=5 matrices this would be more problematic and will need exactly the sort of notation Wolfram Alpha uses.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 10, 2023, 12:34pm UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/9 "2023-02-10T12:34:38Z")

</div>

> [@Ismam](#):
>
> Do you think Wolfram Alpha is spitting out the right results then? I was confused as to what “omega” meant in my screenshot of Wolfram Alpha - and in light of your comment, I suspect it’s a solution to some \>5 degree polynomial.

This case would be a degree 3 polynomial. But Symbolics hasn’t specialized the \<5 cases just because this breaks down at 5x5 so no one has had the motivation yet to write a scheme that works for and 4x4 and smaller matrix only.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 10, 2023, 1:45pm UTC](https://discourse.julialang.org/t/symbolic-matrix-exponential/85435/10 "2023-02-10T13:45:23Z")

</div>

Approximating the symbolic expression for `exp(Mt)[1,1]` might be enough for the application. In that case:

```julia
using TaylorSeries, LinearAlgebra, Symbolics

@variables a b c t m i
M = [ -a b 0
      1 c -a
      0 1 0 ]
Mt = M*t

k = Taylor1(Rational{Int}, 4)
ek = convert(Taylor1{Rational{Int}},exp(k))

```

With these:

```julia
julia> Mt11 = simplify(substitute(substitute(expand(i*ek(m)), Dict(i => I)), Dict(m => Mt))[1,1])
1 + (1//24)*((t^4)*((b + a^2)^2) + (c*(t^2) - a*(t^2))*(b*c*(t^2) - a*b*(t^2)) - a*b*(t^4)) + (1//6)*(b*t*(c*(t^2) - a*(t^2)) - a*(b + a^2)*(t^3)) + (1//2)*(b + a^2)*(t^2) - a*t

julia> simplify(substitute(Mt11, Dict(a => 1, b => 1//2, c => 1//3, t => 1//5)))
446951//540000

```

The number of terms in the Taylor expansion is controlled by definition of the `k` Taylor1 variable.

A formal bound on the approximation quality can be calculated using spectral norm of `M` (in the usual Taylor series method).
