Derivatives

Functions for computing derivatives of scalar-valued functions.

Overview

Derivatives are computed for scalar→scalar maps f(x) where x can be a single point or a collection of points. The derivative functions support:

  • Forward differences: O(1) function evaluation per point, O(h) accuracy
  • Central differences: O(2) function evaluations per point, O(h²) accuracy
  • Complex step: O(1) function evaluation per point, machine precision accuracy

For optimal performance with repeated computations, use the cached versions with DerivativeCache.

Functions

FiniteDiff.finite_difference_derivativeFunction
FiniteDiff.finite_difference_derivative(
    f,
    x          :: T,
    fdtype     :: Type{T1}         = Val{:central},
    returntype :: Type{T2}         = eltype(x),
    f_x        :: Union{Nothing,T} = nothing)

Single-point derivative of scalar->scalar maps.

source
FiniteDiff.finite_difference_derivative(
    f,
    x          :: AbstractArray{<:Number},
    fdtype     :: Type{T1} = Val{:central},
    returntype :: Type{T2} = eltype(x),      # return type of f
    fx         :: Union{Nothing,AbstractArray{<:Number}} = nothing,
    epsilon    :: Union{Nothing,AbstractArray{<:Real}} = nothing;
    [epsilon_factor])

Compute the derivative df of a scalar-valued map f at a collection of points x.

Cache-less.

source
FiniteDiff.finite_difference_derivative!Function
FiniteDiff.finite_difference_derivative!(
    df         :: AbstractArray{<:Number},
    f,
    x          :: AbstractArray{<:Number},
    fdtype     :: Type{T1} = Val{:central},
    returntype :: Type{T2} = eltype(x),
    fx         :: Union{Nothing,AbstractArray{<:Number}} = nothing,
    epsilon    :: Union{Nothing,AbstractArray{<:Real}}   = nothing;
    [epsilon_factor])

Compute the derivative df of a scalar-valued map f at a collection of points x.

Cache-less but non-allocating if fx and epsilon are supplied (fx must be f(x)).

source
FiniteDiff.finite_difference_derivative!(
    df    :: AbstractArray{<:Number},
    f,
    x     :: AbstractArray{<:Number},
    cache :: DerivativeCache{T1,T2,fdtype,returntype};
    relstep = default_relstep(fdtype, eltype(x)),
    absstep = relstep,
    dir     = true)

Compute the derivative df of a scalar-valued map f at a collection of points x.

Cached.

source

Cache

FiniteDiff.DerivativeCacheType
FiniteDiff.DerivativeCache(
    x          :: AbstractArray{<:Number},
    fx         :: Union{Nothing,AbstractArray{<:Number}} = nothing,
    epsilon    :: Union{Nothing,AbstractArray{<:Real}}   = nothing,
    fdtype     :: Type{T1} = Val{:central},
    returntype :: Type{T2} = eltype(x))

This allocates either fx or epsilon if these are nothing and they are needed. fx is the current call of f(x) and is required for forward-differencing (otherwise is not necessary).

source