Dual Numbers and Differentiation. Part 2.

At the end of the previous post I had intended this posting to be an exploration of a recursive definition of Dual that will give an infinite (lazy) list of derivatives. However, there’s still a lot to play with using our simple
data Dual a = Dual a a Let’s try a simple function of two variables…

    \begin{align*} f(x,y) &= x^2 + xy \\ f(1,2) &= 3 \\ \frac{\partial f}{\partial x} &= 2x + y \\ \frac{\partial f}{\partial y} &= x \\ \end{align*}

and at (1,2) we have

    \begin{align*} \frac{\partial f}{\partial x} &= 2x + y = 2 \times 1 + 2 = 4 \\ \frac{\partial f}{\partial y} &= 1 \\ \end{align*}

Now we can evaluate f(x,y) &= x^2 + xy at (1,2) using dual numbers with a subscript of x or y to ‘remember’ where it came from…i.e we want f(1 + \epsilon_x, 2 + \epsilon_y) but really \epsilon_x, \epsilon_y are the ‘same thing’.

    \begin{align*} f(1 + \epsilon_x, 2 + \epsilon_y) &= (1 + \epsilon_x)^2 + (1 + \epsilon_x)(2 + \epsilon_y) \\ &= 1 + \epsilon_x^2 + 2\epsilon_x + 2 + \epsilon_y + 2\epsilon_x + \epsilon_x \epsilon_y \\ &= 3 + 4 \epsilon_x + \epsilon_y \\ \end{align*}

Notice that the coefficients of \epsilon_x, \epsilon_y are the same as the partial derivatives! This sort of suggests that we can get \frac{\partial f}{\partial x} in isolation by setting \epsilon_y = 0. In effect setting \epsilon_y = 0 is the equivalent of treating y as constant and similarly for \epsilon_x = 0, which is how partial derivatives are calculated.

So let’s try this in Haskell…

as can be seen the partial derivatives simply drop out!

Extending to three variables and hiding the use of Dual we have

Which leads to a neat Haskell function for the gradient operator

    \begin{flalign*} \nabla f(x, y, z) = \frac{\partial f}{\partial x}\hat{\imath} + \frac{\partial f}{\partial y}\hat{\jmath} + \frac{\partial f}{\partial z}\hat{k} \ \ \\ \end{flalign*}

Actually if we import the Vector package, used in the series ‘Haskell, Vectors and Simple Mechanics‘ then we can use ‘Vector‘ and write a ‘proper’ grad function:

where we simply create a Vector from the result of applying the grad function – gradV. Using a Vector will then open it up to further vector-based manipulation…

For example, this is taken from ‘The Gradient and Directional Derivatives’

Example

What is the directional derivative in the direction <1,2> of the function z=f(x,y)=4x^2+y^2 at the point x=1 and y=1. The gradient is <8x,2y>, which is <8,2> at the point x=1 and y=1. The direction u is <2,1>. Converting this to a unit vector, we have <2,1>/sqrt(5). Hence,

displaymath70

 

And working the above out in ghci using our gradV and Vector package…

So really this post was a bit of an aside to show how easily the use of Dual extends to functions of more than one variable and how the gradient operator can be easily defined. We then tied this into a simple example using Vectors. Next time we will look at extending the Dual idea to produce second and higher order derivatives and see where that takes us! As always the code is on Github.

Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *

ˆ Back To Top