Hey all,
I am having some problems with trying to implement Automatic Differentiation in F#. I think the problem is down to the evaluation not being 'lazy'.
Here is my code:
type Diff =
{d : double; df : Diff}
static member (+) (x : Diff, y : Diff) =
{d = x.d + y.d; df = x.df + y.df}
static member (-) (x : Diff, y : Diff) =
{d = x.d - y.d; df = x.df - y.df}
static member (*) (x : Diff, a : double) =
{d = x.d * a; df = x.df * a}
static member (*) (x : Diff, y : Diff) =
{d = x.d * y.d; df = (x.df * y) + (y.df * x)}
let rec dZero = {d = 0.0; df = dZero}
let dConst x = {d = x; df = dZero}
let dId x = {d = x; df = dConst 1.0}
let test = dId 5.0
let add (x:Diff) = (x+x).d
If I try to use 'add test' I get a stack overflow error, which I think is down to the definition of (+) inside my type itself relying on '+'.
Is there any way I can fix this? Any help would be greatly appreciated.
Many thanks, Ash