Finite Differences

FiniteDifferences.FiniteDifferenceMethodType
FiniteDifferenceMethod{G<:AbstractVector, C<:AbstractVector, E<:Function}

A finite difference method.

Fields

  • grid::G: Multiples of the step size that the function will be evaluated at.
  • q::Int: Order of derivative to estimate.
  • coefs::C: Coefficients corresponding to the grid functions that the function evaluations will be weighted by.
  • bound_estimator::Function: A function that takes in the function and the evaluation point and returns a bound on the magnitude of the length(grid)th derivative.
source
FiniteDifferences.estimate_stepFunction
function estimate_step(
    m::FiniteDifferenceMethod,
    f::Function,
    x::T;
    factor::Real=1,
    max_step::Real=0.1 * max(abs(x), one(x))
) where T<:AbstractFloat

Estimate the step size for a finite difference method m. Also estimates the error of the estimate of the derivative.

Arguments

  • m::FiniteDifferenceMethod: Finite difference method to estimate the step size for.
  • f::Function: Function to evaluate the derivative of.
  • x::T: Point to estimate the derivative at.

Keywords

  • factor::Real=1. Factor to amplify the estimated round-off error by. This can be used to force a more conservative step size.
  • max_step::Real=0.1 * max(abs(x), one(x)): Maximum step size.

Returns

  • Tuple{T, <:AbstractFloat}: Estimated step size and an estimate of the error of the finite difference estimate.
source
FiniteDifferences.forward_fdmFunction
forward_fdm(
    p::Int,
    q::Int;
    adapt::Int=1,
    condition::Real=DEFAULT_CONDITION,
    geom::Bool=false
)

Contruct a finite difference method at a forward grid of p linearly spaced points.

Arguments

  • p::Int: Number of grid points.
  • q::Int: Order of the derivative to estimate.

Keywords

  • adapt::Int=1: Use another finite difference method to estimate the magnitude of the pth order derivative, which is important for the step size computation. Recurse this procedure adapt times.
  • condition::Real: Condition number. See DEFAULT_CONDITION.
  • geom::Bool: Use geometrically spaced points instead of linearly spaced points.

Returns

  • FiniteDifferenceMethod: The specified finite difference method.
source
FiniteDifferences.central_fdmFunction
central_fdm(
    p::Int,
    q::Int;
    adapt::Int=1,
    condition::Real=DEFAULT_CONDITION,
    geom::Bool=false
)

Contruct a finite difference method at a central grid of p linearly spaced points.

Arguments

  • p::Int: Number of grid points.
  • q::Int: Order of the derivative to estimate.

Keywords

  • adapt::Int=1: Use another finite difference method to estimate the magnitude of the pth order derivative, which is important for the step size computation. Recurse this procedure adapt times.
  • condition::Real: Condition number. See DEFAULT_CONDITION.
  • geom::Bool: Use geometrically spaced points instead of linearly spaced points.

Returns

  • FiniteDifferenceMethod: The specified finite difference method.
source
FiniteDifferences.backward_fdmFunction
backward_fdm(
    p::Int,
    q::Int;
    adapt::Int=1,
    condition::Real=DEFAULT_CONDITION,
    geom::Bool=false
)

Contruct a finite difference method at a backward grid of p linearly spaced points.

Arguments

  • p::Int: Number of grid points.
  • q::Int: Order of the derivative to estimate.

Keywords

  • adapt::Int=1: Use another finite difference method to estimate the magnitude of the pth order derivative, which is important for the step size computation. Recurse this procedure adapt times.
  • condition::Real: Condition number. See DEFAULT_CONDITION.
  • geom::Bool: Use geometrically spaced points instead of linearly spaced points.

Returns

  • FiniteDifferenceMethod: The specified finite difference method.
source
FiniteDifferences.extrapolate_fdmFunction
extrapolate_fdm(
    m::FiniteDifferenceMethod,
    f::Function,
    x::T,
    h::Real=0.1 * max(abs(x), one(x));
    power=nothing,
    breaktol=Inf,
    kw_args...
) where T<:AbstractFloat

Use Richardson extrapolation to refine a finite difference method.

Takes further in keyword arguments for Richardson.extrapolate. This method automatically sets power = 2 if m is symmetric and power = 1. Moreover, it defaults breaktol = Inf.

Arguments

  • m::FiniteDifferenceMethod: Finite difference method to estimate the step size for.
  • f::Function: Function to evaluate the derivative of.
  • x::T: Point to estimate the derivative at.
  • h::Real=0.1 * max(abs(x), one(x)): Initial step size.

Returns

  • Tuple{<:AbstractFloat, <:AbstractFloat}: Estimate of the derivative and error.
source
FiniteDifferences.assert_approx_equalFunction
assert_approx_equal(x, y, ε_abs, ε_rel[, desc])

Assert that x is approximately equal to y.

Let eps_z = eps_abs / eps_rel. Call x and y small if abs(x) < eps_z and abs(y) < eps_z, and call x and y large otherwise. If this function returns True, then it is guaranteed that abs(x - y) < 2 eps_rel max(abs(x), abs(y)) if x and y are large, and abs(x - y) < 2 eps_abs if x and y are small.

Arguments

  • x: First object to compare.
  • y: Second object to compare.
  • ε_abs: Absolute tolerance.
  • ε_rel: Relative tolerance.
  • desc: Description of the comparison. Omit or set to false to have no description.
source

Gradients

FiniteDifferences.jacobianFunction
jacobian(fdm, f, x...)

Approximate the Jacobian of f at x using fdm. Results will be returned as a Matrix{<:Real} of size(length(y_vec), length(x_vec)) where x_vec is the flattened version of x, and y_vec the flattened version of f(x...). Flattening performed by to_vec.

source
FiniteDifferences.jvpFunction
jvp(fdm, f, xẋs::Tuple{Any, Any}...)

Compute a Jacobian-vector product with any types of arguments for which to_vec is defined. Each 2-Tuple in xẋs contains the value x and its tangent .

source