Previously we looked at using Dual numbers get the value of the first derivative of a function. As useful as this is there is more potential if we can also obtain the second derivative. My initial, naive, approach to this was to extend the idea of a Dual to that of a Triple like this. data Triple a = T a a a deriving (Show). Creating Triple somehow seemed ‘wrong’, or if not wrong then certainly clumsy as can be seen in some of the code below.

1 2 3 4 5 6 |
data Triple a = T a a a deriving (Show) instance Fractional a => Fractional (Triple a ) where fromRational n = T (fromRational n) 0 0 (T g g' g'') / (T h h' h'') = T (g / h) ((g * h' - h * g')/ h * h) secDiff where secDiff = ( 2*h'*(g*h' - h*g') - h*(g*h'' - h*g'')) / (h * h * h) |

Note how messy the code is! It’s the result of apply the quotient rule to the result of applying the quotient Read More