Finite Differences
FiniteDifferences.FiniteDifferenceMethod — TypeFiniteDifferenceMethod{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 thelength(grid)th derivative.
FiniteDifferences.FiniteDifferenceMethod — MethodFiniteDifferenceMethod(
grid::AbstractVector,
q::Int;
condition::Real=DEFAULT_CONDITION
)Construct a finite difference method.
Arguments
grid::Abstract: The grid. SeeFiniteDifferenceMethod.q::Int: Order of the derivative to estimate.condition::Real: Condition number. SeeDEFAULT_CONDITION.
Returns
FiniteDifferenceMethod: Specified finite difference method.
FiniteDifferences.estimate_step — Functionfunction estimate_step(
m::FiniteDifferenceMethod,
f::Function,
x::T;
factor::Real=1,
max_step::Real=0.1 * max(abs(x), one(x))
) where T<:AbstractFloatEstimate 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.
FiniteDifferences.forward_fdm — Functionforward_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 thepth order derivative, which is important for the step size computation. Recurse this procedureadapttimes.condition::Real: Condition number. SeeDEFAULT_CONDITION.geom::Bool: Use geometrically spaced points instead of linearly spaced points.
Returns
FiniteDifferenceMethod: The specified finite difference method.
FiniteDifferences.central_fdm — Functioncentral_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 thepth order derivative, which is important for the step size computation. Recurse this procedureadapttimes.condition::Real: Condition number. SeeDEFAULT_CONDITION.geom::Bool: Use geometrically spaced points instead of linearly spaced points.
Returns
FiniteDifferenceMethod: The specified finite difference method.
FiniteDifferences.backward_fdm — Functionbackward_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 thepth order derivative, which is important for the step size computation. Recurse this procedureadapttimes.condition::Real: Condition number. SeeDEFAULT_CONDITION.geom::Bool: Use geometrically spaced points instead of linearly spaced points.
Returns
FiniteDifferenceMethod: The specified finite difference method.
FiniteDifferences.extrapolate_fdm — Functionextrapolate_fdm(
m::FiniteDifferenceMethod,
f::Function,
x::T,
h::Real=0.1 * max(abs(x), one(x));
power=nothing,
breaktol=Inf,
kw_args...
) where T<:AbstractFloatUse 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.
FiniteDifferences.assert_approx_equal — Functionassert_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 tofalseto have no description.
FiniteDifferences.DEFAULT_CONDITION — ConstantFiniteDifferences.DEFAULT_CONDITIONThe default condition number used when computing bounds. It provides amplification of the ∞-norm when passed to the function's derivatives.
Gradients
FiniteDifferences.grad — Functiongrad(fdm, f, xs...)Compute the gradient of f for any xs for which to_vec is defined.
FiniteDifferences.jacobian — Functionjacobian(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.
FiniteDifferences.jvp — Functionjvp(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 ẋ.
FiniteDifferences.j′vp — Functionj′vp(fdm, f, ȳ, x...)Compute an adjoint with any types of arguments x for which to_vec is defined.
FiniteDifferences.to_vec — Functionto_vec(x)Transform x into a Vector, and return the vector, and a closure which inverts the transformation.