Skip to content

Instantly share code, notes, and snippets.

@eulerfx
Last active May 14, 2016 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eulerfx/934326d6205f1cd810dfbc8741853575 to your computer and use it in GitHub Desktop.
Save eulerfx/934326d6205f1cd810dfbc8741853575 to your computer and use it in GitHub Desktop.
F# automatic differentiation
/// A value together with its derivative.
[<NoComparison>]
type D<'a> = D of 'a * 'a
module D =
[<GeneralizableValue>]
let inline GenericTwo () =
LanguagePrimitives.GenericOne + LanguagePrimitives.GenericOne
[<GeneralizableValue>]
let inline GenericThree () =
LanguagePrimitives.GenericOne + LanguagePrimitives.GenericOne + LanguagePrimitives.GenericOne
let inline konst (x:'a) : D<'a> =
D (x, LanguagePrimitives.GenericZero)
let inline id (x:'a) : D<'a> =
D (x, LanguagePrimitives.GenericOne)
let inline ofInt (x:int) =
konst x
let inline add (D(a,a') : D<'a>) (D(b,b') : D<'a>) : D<'a> =
D (a + b, a' + b')
let inline mul (D(a,a') : D<'a>) (D(b,b') : D<'a>) : D<'a> =
D (a * b, a' * b')
let inline neg (D(a,a') : D<'a>) : D<'a> =
D (-a, -a')
let inline abs (D(a,a') : D<'a>) : D<'a> =
D (abs a, abs a')
let inline sqr (d:D<'a>) : D<'a> =
mul d d
let inline exp (D(a,a') : D<'a>) : D<'a> =
D (exp a, a' * exp a)
let inline log (D(a,a') : D<'a>) : D<'a> =
D (log a, a' / a)
let inline sqrt (D(a,a') : D<'a>) : D<'a> =
D (sqrt a, a' / (GenericTwo * sqrt a))
let inline sin (D(a,a') : D<'a>) : D<'a> =
D (sin a, a' * cos a)
let inline cos (D(a,a') : D<'a>) : D<'a> =
D (cos a, a' * (-(Operators.sin a)))
let inline asin (D(a,a') : D<'a>) : D<'a> =
D (asin a, a' / Operators.sqrt (LanguagePrimitives.GenericOne - Operators.pown a 2))
let inline acos (D(a,a') : D<'a>) : D<'a> =
D (acos a, a' / (-Operators.sqrt (LanguagePrimitives.GenericOne - (Operators.pown a 2))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment