numbers-3000.1.0.1: Various number types

Safe HaskellSafe-Inferred



The Dif module contains a data type, Dif, that allows for automatic forward differentiation.

All the ideas are from Jerzy Karczmarczuk's work, see

A simple example, if we define

 foo x = x*x

then the function

 foo' = deriv foo

will behave as if its body was 2*x.



data Dif a Source

The Dif type is the type of differentiable numbers. It's an instance of all the usual numeric classes. The computed derivative of a function is is correct except where the function is discontinuous, at these points the derivative should be a Dirac pulse, but it isn't.

The Dif numbers are printed with a trailing ~~ to indicate that there is a "tail" of derivatives.


Eq a => Eq (Dif a) 
(Fractional (Dif a), Floating a, Eq a) => Floating (Dif a) 
(Num (Dif a), Fractional a, Eq a) => Fractional (Dif a) 
(Num a, Eq a) => Num (Dif a) 
(Eq (Dif a), Ord a) => Ord (Dif a) 
Read a => Read (Dif a) 
(Num (Dif a), Ord (Dif a), Real a) => Real (Dif a) 
(RealFrac (Dif a), Floating (Dif a), RealFloat a) => RealFloat (Dif a) 
(Real (Dif a), Fractional (Dif a), RealFrac a) => RealFrac (Dif a) 
Show a => Show (Dif a) 

val :: Dif a -> aSource

The val function takes a Dif number back to a normal number, thus forgetting about all the derivatives.

df :: (Num a, Eq a) => Dif a -> Dif aSource

The df takes a Dif number and returns its first derivative. The function can be iterated to to get higher derivaties.

mkDif :: a -> Dif a -> Dif aSource

The mkDif takes a value and Dif value and makes a Dif number that has the given value as its normal value, and the Dif number as its derivatives.

dCon :: Num a => a -> Dif aSource

The dCon function turns a normal number into a Dif number with the same value. Not that numeric literals do not need an explicit conversion due to the normal Haskell overloading of literals.

dVar :: (Num a, Eq a) => a -> Dif aSource

The dVar function turns a number into a variable number. This is the number with with respect to which the derivaticve is computed.

deriv :: (Num a, Num b, Eq a, Eq b) => (Dif a -> Dif b) -> a -> bSource

The deriv function is a simple utility to take the derivative of a (single argument) function. It is simply defined as

  deriv f = val . df . f . dVar

unDif :: (Num a, Eq a) => (Dif a -> Dif b) -> a -> bSource

Convert a Dif function to an ordinary function.