# Dual Numbers and Differentiation. Part 1.

### Overview

Just recently I came across the interesting and, at first viewing, the rather abstract idea of dual numbers. I suppose they are no more or less abstract than other numbers… anyway the idea is similar to that of complex numbers where we have

Dual numbers are quite similar, we have the dual number d as

So now lets take this idea of a dual number and explore how adding, multiplying and dividing them might be defined.
Addition and subtraction are simple – we just add the corresponding components – in much the same way that complex numbers are added. i.e.

Similarly for subtraction.

Multiplication requires some simple algebraic manipulation.

and, as this becomes

Finally to determine

we take the dual conjugate, , of the denominator and multiply like this.

So, where is this going you ask? Well Dual numbers can be used to compute the exact value of the derivatives of a function at a given point. Not just approximately – but exactly, at least with regard to the accuracy of the machine. The so called ‘Automatic Differentiation’.

When calculating, from first principles, the derivative of a function it can be shown that

for suitably small value of h. However too big or too small a value of h gives wild inaccuracies. But if we use dual numbers then the values are calculated to machine accuracy! For example

And, by direct calculation,

or, in dual form we have

so both the function and its first derivate are calculated in ‘one go’!

### Simple Implementation

Now we need to create a type for dual in such a way that wherever we can write

we can also write

OK! What’s my favourite language? Which language could express these notions in a clear and compact way?

This is parameterised over a general type, a, which will allow more flexibility if needed later. (e.g. complex numbers rather than reals.)
The next step will be to make Dual into a Number, typing :i Num into ghci shows what’s needed.

Here’s one I made earlier… and using x, dx notation to perhaps reinforce the link to derivatives.

Most of the above we’ve covered already. For abs (Dual x dx) = Dual (abs x) (dx * (signum x)), well the signnum function can be defined as the derivative of the absolute value function.

To try this out we need a couple of helper functions:

The first one, derivDual, simply pattern matches and returns a tuple of the value and the value of the derivative. The helper function derivfx takes an ‘x’, makes it into a dual, applies f and then extracts the result. Let’s try it with and ‘manually’ we have

and in ghci:

Isn’t that satisfying? It’s a kind of Voodoo magic – and expressing it in Haskell really complements the mathematics.

Now, what about a function that includes division? For example , to handle division we need the Fractional typeclass.

From this we see that we need fromRational, that’s easy enough and division – well, we’ve already done that in the first section about dual numbers.

So with this now added we can try some fractional functions.

To check these results you can of course work out the derivate of

to be

and then plugin the values of x. However I found this excellent ‘Online Derivative Calculator‘ that will save you a lot of paper and ink!

OK. So far we can readily calculate the derivatives of polynomial functions. What about trigonometric and log functions etc. Well, Haskell has the typeclass Floating and a quick query in ghci shows

and again we can see the minimal requirements. The implementation is below and is really just a matter of working out or looking up the derivatives of the required functions.

And trying out some of these functions:

A slightly more convoluted one:

And, just for fun,

and the differential by ‘hand’ is

The ‘Online Derivative Calculator‘ really is useful… 😉

This is just an introduction to the topic but I hope it gives a flavour of the potential it has. And, of course, the elegance of the Haskell type system comes to the fore with things like this. The next post in this series will look at extending these ideas to calculate 2nd and higher derivatives. In fact an infinite stream of derivatives based on using
data Dual a = Dual a (Dual a) as a recursive definition of Dual. As usual all the code is in github and…