# Taking gradients of a matrix exponential

**URL:** https://discourse.julialang.org/t/taking-gradients-of-a-matrix-exponential/107865
**Category:** New to Julia
**Tags:** differentiation, gradient
**Created:** [December 20, 2023, 4:28pm UTC](https://discourse.julialang.org/t/taking-gradients-of-a-matrix-exponential/107865 "2023-12-20T16:28:50Z")
**Posts on this page:** 1
**Showing post:** 8

<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: [December 21, 2023, 8:05pm UTC](https://discourse.julialang.org/t/taking-gradients-of-a-matrix-exponential/107865/8 "2023-12-21T20:05:48Z")

</div>

> [@facusapienza](#):
>
> If instead you used a hand-written version of the exponential function (which approximates quite well the actual exponential)

This Taylor-series algorithm is discussed in section 3 (“method 1”) of the classic paper [Nineteen dubious ways to compute the exponential of a matrix](https://www.math.purdue.edu/~yipn/543/matrixExp19-I.pdf) (1978). Basically, the naive series is quite unreliable, even if you sum enough terms, because it is susceptible to [catastrophic cancellation](https://en.wikipedia.org/wiki/Catastrophic_cancellation).

(Contrary to popular misconception from first-year calculus, Taylor series are _not_ typically how special functions are computed.)

It’s really much safer to use the built-in `exp` function here, which means that you need to teach your AD system to use a custom rule (e.g. the one from ChainRules.jl).

---

_[View the full topic](https://discourse.julialang.org/t/taking-gradients-of-a-matrix-exponential/107865)._
