Overview
We use dispatch to determine the mode of finite difference we use to perform numerical differentiation. The modes are specified using the following types:
ForwardMode
BackwardMode
CentralMode
ComplexMode
HessianMode
Given a mode and a value of x
at which to differentiate f(x)
, the following functions are used to determine the step-size to use for finite difference approximations of derivatives:
step_size(::ForwardMode, x::AbstractFloat)
step_size(::BackwardMode, x::AbstractFloat)
step_size(::CentralMode, x::AbstractFloat)
step_size(::ComplexMode, x::AbstractFloat)
step_size(::HessianMode, x::AbstractFloat)
Unless there are bugs in the definitions of the core functions, the quality of an approximate derivative is almost entirely determined by these step-sizes. These step sizes are chosen to reach a reasonable compromise between errors introduced by floating-point truncation (which could be minimized by using larger step-sizes) and errors introduced by the use of an truncated Taylor series (which could be minimized by using smaller step-sizes). The exact compromise depends on the mode of finite differences we use.
Detailed Method-Level Documentation
#
FiniteDiff.step_size
— Method.
step_size(::ForwardMode, x::AbstractFloat)
Description
Determine the step-size to use for forward-mode finite differences.
Arguments
::ForwardMode
: An instance of theForwardMode
type.x::AbstractFloat
: A point at which the derivative of a function will be approximated. Its type must implementeps
.
Returns
ϵ::Real
: The step-size to use for finite differences atx
.
References
See Section 5.7 of Numerical Recipes in C for the mathematical justification for working with sqrt(eps(typeof(x)))
.
Examples
import FiniteDiff: step_size, ForwardMode ϵ = step_size(ForwardMode(), 0.0)
#
FiniteDiff.step_size
— Method.
step_size(::BackwardMode, x::AbstractFloat)
Description
Determine the step-size to use for backward-mode finite differences.
Arguments
::BackwardMode
: An instance of theBackwardMode
type.x::AbstractFloat
: A point at which the derivative of a function will be approximated. Its type must implementeps
.
Returns
ϵ::Real
: The step-size to use for finite differences atx
.
References
See Section 5.7 of Numerical Recipes in C for the mathematical justification for working with sqrt(eps(typeof(x)))
.
Examples
import FiniteDiff: step_size, BackwardMode ϵ = step_size(BackwardMode(), 0.0)
#
FiniteDiff.step_size
— Method.
step_size(::CentralMode, x::AbstractFloat)
Description
Determine the step-size to use for central-mode finite differences.
Arguments
::CentralMode
: An instance of theCentralMode
type.x::AbstractFloat
: A point at which the derivative of a function will be approximated. Its type must implementeps
.
Returns
ϵ::Real
: The step-size to use for finite differences atx
.
References
See Section 5.7 of Numerical Recipes in C for the mathematical justification for working with cbrt(eps(typeof(x)))
.
Examples
import FiniteDiff: step_size, CentralMode ϵ = step_size(CentralMode(), 0.0)
#
FiniteDiff.step_size
— Method.
step_size(::ComplexMode, x::AbstractFloat)
Description
Determine the step-size to use for complex-mode finite differences.
Arguments
::ComplexMode
: An instance of theComplexMode
type.x::AbstractFloat
: A point at which the derivative of a function will be approximated. Its type must implementeps
.
Returns
ϵ::Real
: The step-size to use for finite differences atx
.
References
See "The Complex-Step Derivative Approximation" by Martins, Sturdza and Alonso (2003) for the mathematical justification for working with eps(x)
.
Examples
import FiniteDiff: step_size, ComplexMode ϵ = step_size(ComplexMode(), 0.0)
#
FiniteDiff.step_size
— Method.
step_size(::HessianMode, x::AbstractFloat)
Description
Determine the step-size to use for finite differences of hessians.
Arguments
::HessianMode
: An instance of theHessianMode
type.x::AbstractFloat
: A point at which the derivative of a function will be approximated. Its type must implementeps
.
Returns
ϵ::Real
: The step-size to use for finite differences atx
.
References
See Section 5.7 of Numerical Recipes in C for the mathematical justification for working with eps(x)^(1 // 4)
.
Examples
import FiniteDiff: step_size, HessianMode ϵ = step_size(HessianMode(), 0.0)