Liking cljdoc? Tell your friends :D

fastmath.calculus.finite

Finite difference derivatives and Richardson extrapolation.

Univariate derivatives of any order and any order of accuracy with central, forward and backward stencils (derivative), the stencil weights (fd-coeffs, fd-coeffs-for-accuracy), multivariate derivatives (gradient, hessian, hessian-diagonal, jacobian, laplacian) and Richardson extrapolation (extrapolate).

When the step :h is not given (or is 0) it is chosen automatically from the machine epsilon and the magnitude of the point, which balances the truncation and the rounding errors. An explicit :h is always an absolute step.

Options: a nil option is a missing option. Numeric options are coerced to long (an integer option) or double and only their ranges are checked, so a fractional n, :acc or :max-evals is truncated and a non-number gives a ClassCastException. The step given to a function returned by derivative, gradient, jacobian, hessian, hessian-diagonal or laplacian at call time is not checked: a step which is not positive means the automatic step.

Finite difference derivatives and Richardson extrapolation.

Univariate derivatives of any order and any order of accuracy with central, forward and backward stencils ([[derivative]]), the stencil weights ([[fd-coeffs]], [[fd-coeffs-for-accuracy]]), multivariate derivatives ([[gradient]], [[hessian]], [[hessian-diagonal]], [[jacobian]], [[laplacian]]) and Richardson extrapolation ([[extrapolate]]).

When the step `:h` is not given (or is `0`) it is chosen automatically from the machine epsilon and the magnitude of the point, which balances the truncation and the rounding errors. An explicit `:h` is always an absolute step.

Options: a `nil` option is a missing option. Numeric options are coerced to `long` (an integer option) or `double` and only their ranges are checked, so a fractional `n`, `:acc` or `:max-evals` is truncated and a non-number gives a `ClassCastException`. The step given to a function returned by [[derivative]], [[gradient]], [[jacobian]], [[hessian]], [[hessian-diagonal]] or [[laplacian]] at call time is not checked: a step which is not positive means the automatic step.
raw docstring

derivativeclj

(derivative f)
(derivative f n)
(derivative f n options)

Creates the n-th derivative of a univariate function f approximated with finite differences.

Parameters:

  • f (function): double -> double.
  • n (integer >= 0): the order of the derivative (default: 1). For 0 the function f itself is returned.
  • options (map, optional):
    • :acc - order of accuracy, an integer >= 1 (default: 2). The error of the approximation is O(h^acc); an odd acc for the central stencil gives the next even accuracy.
    • :h - step, a number >= 0 (default: 0). 0 means the automatic step eps^(1/(n+acc)) * max(1, |x|/10), where eps is the machine epsilon and acc is the accuracy (for the central method rounded up to even); this balances the truncation and rounding errors and grows with the magnitude of the point above 10. An explicit positive :h is an absolute step.
    • :method - :central (default), :forward or :backward.
    • :extrapolate? - when true or a map with options of extrapolate the derivative is Richardson-extrapolated to h -> 0 (default: false); the extrapolation starts with the step given in its options (:init-h, default 0.5) and :power is 2 for the central and 1 otherwise, unless given.

Returns a function of x returning the derivative as a double. The function has also the arities (d x h) with the absolute step h (a step which is not positive means automatic) and (d x h hn) with the step and its precomputed power h^n. It is the function (d x h) that extrapolate uses.

Throws ex-info with {:option :value :expected} when n is negative, :acc is less than 1, :method is unknown or :h is negative or not finite.

Higher orders and high accuracies need smaller relative steps and are more sensitive to rounding errors: the attainable relative error grows quickly with n.

See also gradient, hessian, extrapolate, fd-coeffs-for-accuracy.

Creates the `n`-th derivative of a univariate function `f` approximated with finite differences.

Parameters:

- `f` (function): double -> double.
- `n` (integer >= 0): the order of the derivative (default: `1`). For `0` the function `f` itself is returned.
- `options` (map, optional):
  - `:acc` - order of accuracy, an integer >= 1 (default: `2`). The error of the approximation is `O(h^acc)`; an odd `acc` for the central stencil gives the next even accuracy.
  - `:h` - step, a number >= 0 (default: `0`). `0` means the automatic step `eps^(1/(n+acc)) * max(1, |x|/10)`, where `eps` is the machine epsilon and `acc` is the accuracy (for the central method rounded up to even); this balances the truncation and rounding errors and grows with the magnitude of the point above `10`. An explicit positive `:h` is an absolute step.
  - `:method` - `:central` (default), `:forward` or `:backward`.
  - `:extrapolate?` - when true or a map with options of [[extrapolate]] the derivative is Richardson-extrapolated to `h -> 0` (default: `false`); the extrapolation starts with the step given in its options (`:init-h`, default `0.5`) and `:power` is `2` for the central and `1` otherwise, unless given.

Returns a function of `x` returning the derivative as a double. The function has also the arities `(d x h)` with the absolute step `h` (a step which is not positive means automatic) and `(d x h hn)` with the step and its precomputed power `h^n`. It is the function `(d x h)` that [[extrapolate]] uses.

Throws `ex-info` with `{:option :value :expected}` when `n` is negative, `:acc` is less than 1, `:method` is unknown or `:h` is negative or not finite.

Higher orders and high accuracies need smaller relative steps and are more sensitive to rounding errors: the attainable relative error grows quickly with `n`.

See also [[gradient]], [[hessian]], [[extrapolate]], [[fd-coeffs-for-accuracy]].
raw docstring

extrapolateclj

(extrapolate g)
(extrapolate g options)

Richardson extrapolation of a function g(x, h) to h -> 0, returns the extrapolated function of x.

The function g is evaluated for a geometric sequence of steps h (started at :init-h and multiplied by :contract each time). The values are combined with Neville's scheme, which eliminates consecutive powers of h from the error expansion. The result is the combination with the smallest error estimate. The method is a port of Richardson.jl.

Parameters:

  • g (function): the function of x and h, both doubles, returning a double. It has to be defined (and finite) for the steps used.
  • options (map, optional):
    • :contract - contraction factor of the step, must be in the open interval (0, 1) (default: 0.5).
    • :power - the power of h in the leading error term; the next terms are assumed to be in powers of contract^power steps, e.g. 2.0 for the central differences (default: 1.0).
    • :init-h - the first step, a nonzero number (default: 0.5).
    • :rel - relative tolerance of the error estimate (default: square root of the ulp of :init-h).
    • :abs - absolute tolerance of the error estimate (default: machine epsilon).
    • :max-evals - maximum number of evaluations of g, an integer >= 1 (default: Integer/MAX_VALUE).
    • :tol - the iteration stops when the best error estimate grows by more than this factor with respect to the previous one (default: 2.0).

The extrapolation stops when the error estimate is not larger than max(rel * |result|, abs), when the error estimate starts to grow, or when :max-evals is reached. In the last case the best value found so far is returned.

For an infinite x the function g(0, 1/h) is evaluated for x = ##Inf and g(0, -1/h) for x = ##-Inf, which allows to extrapolate limits at infinity. NaN gives NaN.

The stopping rule differs from Richardson.jl: the error estimate of the previous iteration is tested against the tolerance (one iteration later than in the original), therefore g can be evaluated once more. The result is not less accurate.

Throws ex-info with {:option :value :expected} when :contract is not in (0, 1), :max-evals is less than 1 or :init-h is zero or not finite.

See also derivative (the :extrapolate? option).

Richardson extrapolation of a function `g(x, h)` to `h -> 0`, returns the extrapolated function of `x`.

The function `g` is evaluated for a geometric sequence of steps `h` (started at `:init-h` and multiplied by `:contract` each time). The values are combined with Neville's scheme, which eliminates consecutive powers of `h` from the error expansion. The result is the combination with the smallest error estimate. The method is a port of Richardson.jl.

Parameters:

- `g` (function): the function of `x` and `h`, both doubles, returning a double. It has to be defined (and finite) for the steps used.
- `options` (map, optional):
  - `:contract` - contraction factor of the step, must be in the open interval `(0, 1)` (default: `0.5`).
  - `:power` - the power of `h` in the leading error term; the next terms are assumed to be in powers of `contract^power` steps, e.g. `2.0` for the central differences (default: `1.0`).
  - `:init-h` - the first step, a nonzero number (default: `0.5`).
  - `:rel` - relative tolerance of the error estimate (default: square root of the ulp of `:init-h`).
  - `:abs` - absolute tolerance of the error estimate (default: machine epsilon).
  - `:max-evals` - maximum number of evaluations of `g`, an integer >= 1 (default: `Integer/MAX_VALUE`).
  - `:tol` - the iteration stops when the best error estimate grows by more than this factor with respect to the previous one (default: `2.0`).

The extrapolation stops when the error estimate is not larger than `max(rel * |result|, abs)`, when the error estimate starts to grow, or when `:max-evals` is reached. In the last case the best value found so far is returned.

For an infinite `x` the function `g(0, 1/h)` is evaluated for `x = ##Inf` and `g(0, -1/h)` for `x = ##-Inf`, which allows to extrapolate limits at infinity. `NaN` gives `NaN`.

The stopping rule differs from Richardson.jl: the error estimate of the previous iteration is tested against the tolerance (one iteration later than in the original), therefore `g` can be evaluated once more. The result is not less accurate.

Throws `ex-info` with `{:option :value :expected}` when `:contract` is not in `(0, 1)`, `:max-evals` is less than 1 or `:init-h` is zero or not finite.

See also [[derivative]] (the `:extrapolate?` option).
raw docstring

fd-coeffsclj

(fd-coeffs n offsets)

Weights of the finite difference approximation of the n-th derivative at a point for given offsets of the stencil.

The weights are computed with Fornberg's algorithm in exact rational arithmetic and converted to doubles, so they are accurate to the last digit for any order and stencil. The derivative is approximated by sum(coeff_i * f(x + offset_i * h)) / h^n.

Parameters:

  • n (integer >= 0): the order of the derivative.
  • offsets (sequence of numbers): distinct offsets of the nodes in units of the step h, e.g. [-1 0 1]. Doubles are converted to exact ratios.

Returns [offsets coeffs], where coeffs is a vector of doubles of the same length as offsets. When there are not more than n offsets, the derivative is not determined and all weights are 0.0.

Throws ex-info with {:option :value :expected} when n is negative or when offsets is empty, contains duplicates or non-finite numbers.

See also fd-coeffs-for-accuracy, derivative.

Weights of the finite difference approximation of the `n`-th derivative at a point for given offsets of the stencil.

The weights are computed with Fornberg's algorithm in exact rational arithmetic and converted to doubles, so they are accurate to the last digit for any order and stencil. The derivative is approximated by `sum(coeff_i * f(x + offset_i * h)) / h^n`.

Parameters:

- `n` (integer >= 0): the order of the derivative.
- `offsets` (sequence of numbers): distinct offsets of the nodes in units of the step `h`, e.g. `[-1 0 1]`. Doubles are converted to exact ratios.

Returns `[offsets coeffs]`, where `coeffs` is a vector of doubles of the same length as `offsets`. When there are not more than `n` offsets, the derivative is not determined and all weights are `0.0`.

Throws `ex-info` with `{:option :value :expected}` when `n` is negative or when `offsets` is empty, contains duplicates or non-finite numbers.

See also [[fd-coeffs-for-accuracy]], [[derivative]].
raw docstring

fd-coeffs-for-accuracyclj

(fd-coeffs-for-accuracy n)
(fd-coeffs-for-accuracy n acc)
(fd-coeffs-for-accuracy n acc kind)

Offsets and weights of the finite difference stencil for the n-th derivative with a given order of accuracy.

Parameters:

  • n (integer >= 0): the order of the derivative (default: 1).
  • acc (integer >= 1): the order of accuracy, the error is O(h^acc) (default: 2). The central stencil gives an even order, an odd acc results in the next even accuracy.
  • kind (keyword): :central (default), :forward or :backward.

Returns [offsets coeffs] as fd-coeffs does: the integer offsets in units of the step h and the weights of the stencil (divide the weighted sum by h^n).

Throws ex-info with {:option :value :expected} when n is negative, acc is less than 1 or kind is unknown.

See also fd-coeffs, derivative.

Offsets and weights of the finite difference stencil for the `n`-th derivative with a given order of accuracy.

Parameters:

- `n` (integer >= 0): the order of the derivative (default: `1`).
- `acc` (integer >= 1): the order of accuracy, the error is `O(h^acc)` (default: `2`). The central stencil gives an even order, an odd `acc` results in the next even accuracy.
- `kind` (keyword): `:central` (default), `:forward` or `:backward`.

Returns `[offsets coeffs]` as [[fd-coeffs]] does: the integer offsets in units of the step `h` and the weights of the stencil (divide the weighted sum by `h^n`).

Throws `ex-info` with `{:option :value :expected}` when `n` is negative, `acc` is less than 1 or `kind` is unknown.

See also [[fd-coeffs]], [[derivative]].
raw docstring

gradientclj

(gradient f)
(gradient f options)

Creates the gradient (vector of the first partial derivatives) of a multivariate scalar function f, approximated with central finite differences.

Parameters:

  • f (function): a vector of doubles -> double.
  • options (map, optional):
    • :acc - order of accuracy, an integer >= 1 (default: 2); an odd value gives the next even accuracy.
    • :h - step, a number >= 0 (default: 0). 0 means the automatic step for each component i: eps^(1/(1+acc)) * max(1, |x_i|/10) (with acc rounded up to even). An explicit positive :h is an absolute step used for every component.

Returns a function of a point v (any sequence of numbers or a double[]) returning the gradient as a vector of doubles. The function has also the arity (g v h) with an explicit absolute step (a step which is not positive means automatic). The function f receives a vector and is called when the gradient is called, the result is not lazy. The gradient of the empty point is [].

Throws ex-info with {:option :value :expected} when :acc is less than 1 or :h is negative or not finite.

See also hessian, jacobian, derivative.

Creates the gradient (vector of the first partial derivatives) of a multivariate scalar function `f`, approximated with central finite differences.

Parameters:

- `f` (function): a vector of doubles -> double.
- `options` (map, optional):
  - `:acc` - order of accuracy, an integer >= 1 (default: `2`); an odd value gives the next even accuracy.
  - `:h` - step, a number >= 0 (default: `0`). `0` means the automatic step for each component `i`: `eps^(1/(1+acc)) * max(1, |x_i|/10)` (with `acc` rounded up to even). An explicit positive `:h` is an absolute step used for every component.

Returns a function of a point `v` (any sequence of numbers or a `double[]`) returning the gradient as a vector of doubles. The function has also the arity `(g v h)` with an explicit absolute step (a step which is not positive means automatic). The function `f` receives a vector and is called when the gradient is called, the result is not lazy. The gradient of the empty point is `[]`.

Throws `ex-info` with `{:option :value :expected}` when `:acc` is less than 1 or `:h` is negative or not finite.

See also [[hessian]], [[jacobian]], [[derivative]].
raw docstring

hessianclj

(hessian f)
(hessian f options)

Creates the Hessian matrix of a multivariate scalar function f, approximated with central finite differences of the second order.

Parameters:

  • f (function): a vector of doubles -> double.
  • options (map, optional):
    • :h - step, a number >= 0 (default: 0). 0 means the automatic step for each component i: eps^(1/4) * max(1, |x_i|/10). An explicit positive :h is an absolute step used for every component.

Returns a function of a point v (any sequence of numbers or a double[]) returning the symmetric matrix as a vector of vectors of doubles. The function has also the arity (hs v h) with an explicit absolute step (a step which is not positive means automatic). The diagonal term is (f(x+h_i) - 2 f(x) + f(x-h_i)) / h_i^2 and the mixed term is (f(++) - f(+-) - f(-+) + f(--)) / (4 h_i h_j). The function f is called when the Hessian is called. The Hessian of the empty point is [].

Throws ex-info with {:option :value :expected} when :h is negative or not finite.

See also hessian-diagonal, laplacian, gradient.

Creates the Hessian matrix of a multivariate scalar function `f`, approximated with central finite differences of the second order.

Parameters:

- `f` (function): a vector of doubles -> double.
- `options` (map, optional):
  - `:h` - step, a number >= 0 (default: `0`). `0` means the automatic step for each component `i`: `eps^(1/4) * max(1, |x_i|/10)`. An explicit positive `:h` is an absolute step used for every component.

Returns a function of a point `v` (any sequence of numbers or a `double[]`) returning the symmetric matrix as a vector of vectors of doubles. The function has also the arity `(hs v h)` with an explicit absolute step (a step which is not positive means automatic). The diagonal term is `(f(x+h_i) - 2 f(x) + f(x-h_i)) / h_i^2` and the mixed term is `(f(++) - f(+-) - f(-+) + f(--)) / (4 h_i h_j)`. The function `f` is called when the Hessian is called. The Hessian of the empty point is `[]`.

Throws `ex-info` with `{:option :value :expected}` when `:h` is negative or not finite.

See also [[hessian-diagonal]], [[laplacian]], [[gradient]].
raw docstring

hessian-diagonalclj

(hessian-diagonal f)
(hessian-diagonal f options)

Creates the function returning the diagonal of the Hessian matrix of a multivariate scalar function f, approximated with central finite differences of the second order.

Parameters:

  • f (function): a vector of doubles -> double.
  • options (map, optional):
    • :h - step, a number >= 0 (default: 0). 0 means the automatic step for each component i: eps^(1/4) * max(1, |x_i|/10). An explicit positive :h is an absolute step used for every component.

Returns a function of a point v (any sequence of numbers or a double[]) returning the diagonal (f(x+h_i) - 2 f(x) + f(x-h_i)) / h_i^2 as a double[]. The function has also the arity (hd v h) with an explicit absolute step (a step which is not positive means automatic). The function f is called when the diagonal is computed.

Throws ex-info with {:option :value :expected} when :h is negative or not finite.

See also hessian, laplacian.

Creates the function returning the diagonal of the Hessian matrix of a multivariate scalar function `f`, approximated with central finite differences of the second order.

Parameters:

- `f` (function): a vector of doubles -> double.
- `options` (map, optional):
  - `:h` - step, a number >= 0 (default: `0`). `0` means the automatic step for each component `i`: `eps^(1/4) * max(1, |x_i|/10)`. An explicit positive `:h` is an absolute step used for every component.

Returns a function of a point `v` (any sequence of numbers or a `double[]`) returning the diagonal `(f(x+h_i) - 2 f(x) + f(x-h_i)) / h_i^2` as a `double[]`. The function has also the arity `(hd v h)` with an explicit absolute step (a step which is not positive means automatic). The function `f` is called when the diagonal is computed.

Throws `ex-info` with `{:option :value :expected}` when `:h` is negative or not finite.

See also [[hessian]], [[laplacian]].
raw docstring

jacobianclj

(jacobian f)
(jacobian f options)

Creates the Jacobian matrix of a vector valued function f, approximated with central finite differences.

Parameters:

  • f (function): a vector of n doubles -> a sequence (or array) of m numbers.
  • options (map, optional): as in gradient
    • :acc - order of accuracy, an integer >= 1 (default: 2).
    • :h - step, a number >= 0 (default: 0 = automatic step for each component, eps^(1/(1+acc)) * max(1, |x_i|/10)); an explicit positive value is an absolute step.

Returns a function of a point v (any sequence of numbers or a double[]) returning the matrix as a vector of m rows, each a vector of n partial derivatives d f_j / d x_i. The function has also the arity (j v h) with an explicit absolute step (a step which is not positive means automatic). The function f is called when the Jacobian is called. For the empty point the result is a vector of m empty rows.

Throws ex-info with {:option :value :expected} when :acc is less than 1, :h is negative or not finite, f returns a number instead of a sequence or the number of returned values differs between the calls.

See also gradient, hessian.

Creates the Jacobian matrix of a vector valued function `f`, approximated with central finite differences.

Parameters:

- `f` (function): a vector of `n` doubles -> a sequence (or array) of `m` numbers.
- `options` (map, optional): as in [[gradient]]
  - `:acc` - order of accuracy, an integer >= 1 (default: `2`).
  - `:h` - step, a number >= 0 (default: `0` = automatic step for each component, `eps^(1/(1+acc)) * max(1, |x_i|/10)`); an explicit positive value is an absolute step.

Returns a function of a point `v` (any sequence of numbers or a `double[]`) returning the matrix as a vector of `m` rows, each a vector of `n` partial derivatives `d f_j / d x_i`. The function has also the arity `(j v h)` with an explicit absolute step (a step which is not positive means automatic). The function `f` is called when the Jacobian is called. For the empty point the result is a vector of `m` empty rows.

Throws `ex-info` with `{:option :value :expected}` when `:acc` is less than 1, `:h` is negative or not finite, `f` returns a number instead of a sequence or the number of returned values differs between the calls.

See also [[gradient]], [[hessian]].
raw docstring

laplacianclj

(laplacian f)
(laplacian f options)

Creates the Laplacian (the sum of the second partial derivatives) of a multivariate scalar function f, approximated with central finite differences.

Parameters:

  • f (function): a vector of doubles -> double.
  • options (map, optional): as in hessian-diagonal
    • :h - step, a number >= 0 (default: 0 = automatic step for each component, eps^(1/4) * max(1, |x_i|/10)); an explicit positive value is an absolute step.

Returns a function of a point v (any sequence of numbers or a double[]) returning the Laplacian as a double. The function has also the arity (l v h) with an explicit absolute step (a step which is not positive means automatic). The Laplacian of the empty point is 0.0.

Throws ex-info with {:option :value :expected} when :h is negative or not finite.

See also hessian-diagonal, hessian.

Creates the Laplacian (the sum of the second partial derivatives) of a multivariate scalar function `f`, approximated with central finite differences.

Parameters:

- `f` (function): a vector of doubles -> double.
- `options` (map, optional): as in [[hessian-diagonal]]
  - `:h` - step, a number >= 0 (default: `0` = automatic step for each component, `eps^(1/4) * max(1, |x_i|/10)`); an explicit positive value is an absolute step.

Returns a function of a point `v` (any sequence of numbers or a `double[]`) returning the Laplacian as a double. The function has also the arity `(l v h)` with an explicit absolute step (a step which is not positive means automatic). The Laplacian of the empty point is `0.0`.

Throws `ex-info` with `{:option :value :expected}` when `:h` is negative or not finite.

See also [[hessian-diagonal]], [[hessian]].
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close