DiffSharp


Symbolic Differentiation

In addition to AD, the DiffSharp library also implements symbolic differentiation, which works by the symbolic manipulation of closed-form expressions using rules of differential calculus.

For a complete list of the available differentiation operations, please refer to API Overview and API Reference.

DiffSharp.Symbolic

This is a symbolic differentiation module, used with the Expr type representing F# code expressions. A common way of generating F# code expressions is to use code quotations, with the <@ and @> symbols delimiting an expression.

Symbolic differentiation operators construct the wanted derivative as a new expression and return this as a compiled function that can be used subsequently for evaluating the derivative. Once the derivative expression is compiled and returned, it is significantly faster to run it with specific numerical arguments, compared to the initial time it takes to compile the function. You can see example compilation and running times on the Benchmarks page.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
open DiffSharp.Symbolic.Float64

// Derivative of Sin(3 * Sqrt(x))
// This returns a compiled function that gives the derivative
let d = diff <@ fun x -> sin (3. * sqrt x) @>

// Compute the derivative at x = 2
let d2 = d 2.

Function definitions should be marked with the ReflectedDefinition attribute for allowing access to quotation expressions at runtime.

1: 
2: 
3: 
4: 
5: 
6: 
// f: float -> float
[<ReflectedDefinition>]
let f x = sin (3. * sqrt x)

// Derivative of f at 2
let df = diff <@ f @> 2.

Different from the DiffSharp.AD and DiffSharp.Numerical parts of the library, multivariate functions are expected to be in curried form.

1: 
2: 
3: 
4: 
5: 
6: 
// g: float -> float -> float
[<ReflectedDefinition>]
let g x y = sin (x * y)

// Gradient of g at (2, 3)
let gg = grad <@ g @> [|2.; 3.|]

Functions can be marked with the ReflectedDefinition attribute one by one, or they can be put into a module marked with this attribute to make it apply to all.

Differentiation operations will delve into the definition of any other function referenced from a given function (the referenced function will be inlined into the body of the calling function), as long as they have the ReflectedDefinition attribute.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
[<ReflectedDefinition>]
module m =
    // f: float -> float
    let f x = sqrt x
    // g: float -> float -> float
    let g x y = sin (x * (f y))

// Hessian of g at (2, 3)
let hg = hessian <@ m.g @> [|2.; 3.|]
namespace DiffSharp
namespace DiffSharp.Symbolic
module Float64

from DiffSharp.Symbolic
val d : (float -> float)

Full name: Gettingstarted-symbolicdifferentiation.d
val diff : f:Quotations.Expr<(float -> float)> -> (float -> float)

Full name: DiffSharp.Symbolic.Float64.DiffOps.diff
val x : float
val sin : value:'T -> 'T (requires member Sin)

Full name: Microsoft.FSharp.Core.Operators.sin
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
val d2 : float

Full name: Gettingstarted-symbolicdifferentiation.d2
Multiple items
type ReflectedDefinitionAttribute =
  inherit Attribute
  new : unit -> ReflectedDefinitionAttribute
  new : includeValue:bool -> ReflectedDefinitionAttribute
  member IncludeValue : bool

Full name: Microsoft.FSharp.Core.ReflectedDefinitionAttribute

--------------------
new : unit -> ReflectedDefinitionAttribute
new : includeValue:bool -> ReflectedDefinitionAttribute
val f : x:float -> float

Full name: Gettingstarted-symbolicdifferentiation.f
val df : float

Full name: Gettingstarted-symbolicdifferentiation.df
val g : x:float -> y:float -> float

Full name: Gettingstarted-symbolicdifferentiation.g
val y : float
val gg : float []

Full name: Gettingstarted-symbolicdifferentiation.gg
val grad : f:Quotations.Expr -> (float [] -> float [])

Full name: DiffSharp.Symbolic.Float64.DiffOps.grad
module m

from Gettingstarted-symbolicdifferentiation
val f : x:float -> float

Full name: Gettingstarted-symbolicdifferentiation.m.f
val g : x:float -> y:float -> float

Full name: Gettingstarted-symbolicdifferentiation.m.g
val hg : float [,]

Full name: Gettingstarted-symbolicdifferentiation.hg
val hessian : f:Quotations.Expr -> (float [] -> float [,])

Full name: DiffSharp.Symbolic.Float64.DiffOps.hessian