DiffSharp


Numerical Differentiation

In addition to AD, DiffSharp also implements numerical differentiation.

Numerical differentiation is based on finite difference approximations of derivative values, using values of the original function evaluated at some sample points. Unlike AD, numerical differentiation gives only approximate results and is unstable due to truncation and roundoff errors.

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

DiffSharp.Numerical

This is a numerical differentiation module, used with the regular float or float32 numeric types for scalars, float[] or float32[] for vectors, and float[,] or float[,] for matrices.

Currently the library uses the 1st order central difference

\[ \frac{df(x)}{dx} \approx \frac{f(x + h) - f(x - h)}{2h}\]

for the diff and diffdir operations; the 2nd order central difference

\[ \frac{d^2 f(x)}{dx^2} \approx \frac{f(x + h) - 2f(x) + f(x - h)}{h^2}\]

for the diff2 operation; and the 1st order forward difference

\[ \frac{df(x)}{dx} \approx \frac{f(x + h) - f(x)}{h}\]

for the grad, hessian, laplacian, and jacobian operations, where \(0 < h \ll 1\).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
open DiffSharp.Numerical.Float64

// f: float -> float
let f x = sin (3. * sqrt x)

// Derivative of f at 2
let df = diff f 2.

// g: float[] -> float
let g (x:float[]) = sin (x.[0] * x.[1])

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

// h: float[] -> float[]
let h (x:float[]) = [| sin x.[0]; cos x.[1] |]

// Jacobian of h at (2, 3)
let jh = jacobian h [|2.; 3.|]

The default step size is \(h = 10^{-5}\) and it can be changed by using the method DiffSharp.Config.GlobalConfig.SetEpsilon.

Adaptive step size techniques are planned to be implemented in a future release.

1: 
2: 
3: 
4: 
5: 
let v1 = diff sin 0.2

DiffSharp.Config.GlobalConfig.SetEpsilon(0.001)

let v2 = diff sin 0.2
val v1 : float = 0.9800665778
val v2 : float = 0.9800664145
namespace DiffSharp
namespace DiffSharp.Numerical
module Float64

from DiffSharp.Numerical
val f : x:float -> float

Full name: Gettingstarted-numericaldifferentiation.f
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 df : float

Full name: Gettingstarted-numericaldifferentiation.df
val diff : f:(float -> float) -> x:float -> float

Full name: DiffSharp.Numerical.Float64.DiffOps.diff
val g : x:float [] -> float

Full name: Gettingstarted-numericaldifferentiation.g
val x : float []
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val gg : float []

Full name: Gettingstarted-numericaldifferentiation.gg
val grad : f:(float [] -> float) -> x:float [] -> float []

Full name: DiffSharp.Numerical.Float64.DiffOps.grad
val h : x:float [] -> float []

Full name: Gettingstarted-numericaldifferentiation.h
val cos : value:'T -> 'T (requires member Cos)

Full name: Microsoft.FSharp.Core.Operators.cos
val jh : float [,]

Full name: Gettingstarted-numericaldifferentiation.jh
val jacobian : f:(float [] -> float []) -> x:float [] -> float [,]

Full name: DiffSharp.Numerical.Float64.DiffOps.jacobian
val v1 : float

Full name: Gettingstarted-numericaldifferentiation.v1
namespace DiffSharp.Config
Multiple items
type GlobalConfig =
  new : unit -> GlobalConfig
  static member SetBackend : backend:string -> unit
  static member SetEpsilon : e:float -> unit
  static member SetEpsilon : e:float32 -> unit
  static member SetVisualizationContrast : c:float -> unit
  static member SetVisualizationContrast : c:float32 -> unit
  static member SetVisualizationPalette : palette:string -> unit
  static member Float32Backend : Backend<float32>
  static member Float32Epsilon : float32
  static member Float32EpsilonRec : float32
  ...

Full name: DiffSharp.Config.GlobalConfig

--------------------
new : unit -> DiffSharp.Config.GlobalConfig
static member DiffSharp.Config.GlobalConfig.SetEpsilon : e:float -> unit
static member DiffSharp.Config.GlobalConfig.SetEpsilon : e:float32 -> unit
val v2 : float

Full name: Gettingstarted-numericaldifferentiation.v2
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf