# Fast chain rule algorithm [closed]

Assume I have two functions $f$ and $g$, with derivatives of $g$ at point $x$ and derivatives of $f$ at point $g(x)$ available.

What is the fastest way of computing derivatives of $f \circ g (x)$ ?

• (d(f(g(x))/dx)(x) = (df/dg)(g(x)) * (dg/dx)(x) – sssssssssssss Aug 10 '17 at 15:11
• You don't provide a lot of context here, but using a forward-mode automatic differentiation approach could work well. If you're looking for numerical results and are working in a language that supports user-defined types, generic functions and operator overloading, then I suggest using a "Dual Number" approach. Strictly speaking, it may not be faster than hand-writing the derivatives of your functions (in terms of total operations), but (in my experience) it is less error-prone. en.wikipedia.org/wiki/Automatic_differentiation – Tyler Olsen Aug 10 '17 at 16:17
• I've addressed this topic in more detail on a different StackExchange site before, so you may be interested in checking it out there. quant.stackexchange.com/questions/21885/… – Tyler Olsen Aug 10 '17 at 16:24

$$\frac{\textrm{d} f}{\textrm{d} x}= \frac{\textrm{d} f}{\textrm{d} g}\frac{\textrm{d} g}{\textrm{d} x} \approx \frac{\textrm{d} f}{\textrm{d} g}\frac{\textrm{Im}\left\{g(x+i\epsilon)\right\}}{\epsilon}$$ where $i$ is imaginary number. $\textrm{Im}\{z\}$ gives the imaginary part of the complex number z.
You can implement that in any language which can do operations on complex numbers. You can have very small $\epsilon$, to have small truncation error $O(\epsilon^2)$ and avoid subtractive cancellation error. I don't know how fast it is but is very fast to implement.