API

Finite Differences

FiniteDifferenceMethod

Abstract type for all finite differencing method types. Subtypes of FiniteDifferenceMethod are callable with the signature

method(f, x; kwargs...)

where the keyword arguments can be any of

  • adapt: The number of adaptive steps to use improve the estimate of bound.
  • bound: Bound on the value of the function and its derivatives at x.
  • condition: The condition number. See DEFAULT_CONDITION.
  • eps: The assumed roundoff error. Defaults to eps() plus TINY.
source
FiniteDifferences.fdmFunction.
fdm(m::FiniteDifferenceMethod, f, x[, Val(false)]; kwargs...) -> Real
fdm(m::FiniteDifferenceMethod, f, x, Val(true); kwargs...) -> Tuple{FiniteDifferenceMethod, Real}

Compute the derivative of f at x using the finite differencing method m. The optional Val argument dictates whether the method should be returned alongside the derivative value, which can be useful for examining the step size used and other such parameters.

The recognized keywords are:

  • adapt: The number of adaptive steps to use improve the estimate of bound.
  • bound: Bound on the value of the function and its derivatives at x.
  • condition: The condition number. See DEFAULT_CONDITION.
  • eps: The assumed roundoff error. Defaults to eps() plus TINY.
Warning

Bounds can't be adaptively computed over nonstandard grids; passing a value for adapt greater than 0 when m::Nonstandard results in an error.

Note

Calling FiniteDifferenceMethod objects is equivalent to passing them to fdm.

Examples

julia> fdm(central_fdm(5, 1), sin, 1; adapt=2)
0.5403023058681039

julia> fdm(central_fdm(2, 1), exp, 0, Val(true))
(FiniteDifferenceMethod:
  order of method:       2
  order of derivative:   1
  grid:                  [-1, 1]
  coefficients:          [-0.5, 0.5]
  roundoff error:        1.42e-14
  bounds on derivatives: 1.00e+02
  step size:             1.69e-08
  accuracy:              1.69e-06
, 1.0000000031817473)
source
FiniteDifferences.Backward(p, q; kwargs...)
backward_fdm(p, q; kwargs...)

Construct a backward finite difference method of order p to compute the qth derivative. See FiniteDifferenceMethod for more details.

source
FiniteDifferences.Central(p, q; kwargs...)
central_fdm(p, q; kwargs...)

Construct a central finite difference method of order p to compute the qth derivative. See FiniteDifferenceMethod for more details.

source
FiniteDifferences.Forward(p, q; kwargs...)
forward_fdm(p, q; kwargs...)

Construct a forward finite difference method of order p to compute the qth derivative. See FiniteDifferenceMethod for more details.

source
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
FiniteDifferences.DEFAULT_CONDITION

The default condition number used when computing bounds. It provides amplification of the ∞-norm when passed to the function's derivatives.

source
FiniteDifferences.TINY

A tiny number added to some quantities to ensure that division by 0 does not occur.

source

Gradients

grad(fdm, f, xs...)

Approximate the gradient of f at xs... using fdm. Assumes that f(xs...) is scalar.

source
jacobian(fdm, f, xs::Union{Real, AbstractArray{<:Real}}; len::Int=length(f(x)))

Approximate the Jacobian of f at x using fdm. f(x) must be a length D vector. If D is not provided, then f(x) is computed once to determine the output size.

source
FiniteDifferences.jvpFunction.
jvp(fdm, f, x, ẋ)

Compute a Jacobian-vector product with any types of arguments for which to_vec is defined.

source
j′vp(fdm, f, ȳ, x...)

Compute an adjoint with any types of arguments for which to_vec is defined.

source
to_vec(x) -> Tuple{<:AbstractVector, <:Function}

Transform x into a Vector, and return a closure which inverts the transformation.

source