In one implementation of forward-mode automatic differentiation (autodiff), we use "dual numbers" to carry forward partial derivatives along with our calculation. A dual number is similar to a complex number in that it has a real valued component, and an extra component. In the case of dual numbers the extra component is an "infinitesimal" component $\epsilon$. Whereas in complex numbers, $i$ is defined by $i^2 = -1$, for dual numbers, $\epsilon$ is defined by $\epsilon^2 = 0$.
In [1]:
# create a dual number type
immutable Dual
value::Float64
eps::Float64
end
We'll attempt to differentiate the function
$$f(x) = x^2 + x \sin(x)$$which seems moderately interesting.
In [2]:
# define a test function:
f(x) = x^2 + x * sin(x)
Out[2]:
In [3]:
f(1.)
Out[3]:
So we'll need to know how to multiply, add and take the sine of our Dual
type.
In [4]:
import Base: +, *, sin
In [5]:
*(x::Dual, y::Dual) = Dual(x.value * y.value, x.value * y.eps + y.value * x.eps)
Out[5]:
In [6]:
+(x::Dual, y::Dual) = Dual(x.value + y.value, x.eps + y.eps)
Out[6]:
In [7]:
sin(x::Dual) = Dual(sin(x.value), cos(x.value) * x.eps)
Out[7]:
Sweet! Now we'll try to run it!
In [8]:
f(3.)
Out[8]:
In [9]:
x = Dual(3., 1.)
f(x)
Out[9]:
Check it:
In [10]:
fprime(x) = 2 * x + sin(x) + x * cos(x)
Out[10]:
In [11]:
fprime(3.)
Out[11]: