Liking cljdoc? Tell your friends :D

fastmath.solver.common

Internal helpers shared by the root finders fastmath.solver.*.

The namespace is not part of the public API and may change without notice. It collects what every solver needs, so that each solver namespace contains only its iteration.

Options: parse-options validates the options of fastmath.solver/find-root and returns the setup of a solve; throw-invalid-option reports a rejected value.

Evaluation: evaluator wraps the user function, counts the evaluations, enforces the limit :max-iters and rejects NaN and non numbers; tolerance is the rule abs + rel * |x| which decides convergence.

Driver: solve-bracketed checks the bracket, builds the [[Problem]] and calls the iteration of a solver; certified? confirms that a point is a root of a sign change within the tolerance.

Failures: a solve which does not converge throws ex-info; the exception data holds :status (one of statuses), :evaluations and a few scalars, never the iterates. With the option :info? the status is returned as a map instead.

Internal helpers shared by the root finders `fastmath.solver.*`.

The namespace is not part of the public API and may change without notice. It collects what every solver needs, so that each solver namespace contains only its iteration.

Options: [[parse-options]] validates the options of [[fastmath.solver/find-root]] and returns the setup of a solve; [[throw-invalid-option]] reports a rejected value.

Evaluation: [[evaluator]] wraps the user function, counts the evaluations, enforces the limit `:max-iters` and rejects `NaN` and non numbers; [[tolerance]] is the rule `abs + rel * |x|` which decides convergence.

Driver: [[solve-bracketed]] checks the bracket, builds the [[Problem]] and calls the iteration of a solver; [[certified?]] confirms that a point is a root of a sign change within the tolerance.

Failures: a solve which does not converge throws `ex-info`; the exception data holds `:status` (one of [[statuses]]), `:evaluations` and a few scalars, never the iterates. With the option `:info?` the status is returned as a map instead.
raw docstring

call-guardedclj

(call-guarded info? ev thunk)

Calls the function of no arguments thunk of a solve with the evaluator ev.

A solver failure (see throw-status) is returned as the info map {:root ##NaN :evaluations :status} when info? is truthy and rethrown otherwise. Any other exception, also an ex-info thrown by the user function, is rethrown.

Calls the function of no arguments `thunk` of a solve with the evaluator `ev`.

A solver failure (see [[throw-status]]) is returned as the info map `{:root ##NaN :evaluations :status}` when `info?` is truthy and rethrown otherwise. Any other exception, also an `ex-info` thrown by the user function, is rethrown.
raw docstring

certified?clj

(certified? ev x fx p)

Checks that x is a root of a sign change within the tolerance.

Returns true when fx is 0 or when the function has the opposite sign at x - 8 tol or at x + 8 tol (a probe outside the bracket [a, b] of the problem is skipped); costs up to two evaluations. A point which converged by the step size but is not a root (an even multiplicity, a jump) fails.

Parameters: ev the evaluator, x and fx the point and the value, p the [[Problem]] with the bracket and the tolerance.

Checks that `x` is a root of a sign change within the tolerance.

Returns `true` when `fx` is `0` or when the function has the opposite sign at `x - 8 tol` or at `x + 8 tol` (a probe outside the bracket `[a, b]` of the problem is skipped); costs up to two evaluations. A point which converged by the step size but is not a root (an even multiplicity, a jump) fails.

Parameters: `ev` the evaluator, `x` and `fx` the point and the value, `p` the [[Problem]] with the bracket and the tolerance.
raw docstring

default-absolute-accuracyclj

Default absolute tolerance, close to the smallest normal double so that it is relevant only for a root at zero.

Default absolute tolerance, close to the smallest normal double so that it is relevant only for a root at zero.
raw docstring

default-max-itersclj

Default limit of function evaluations: enough for the bisection to reach the floating point resolution on any interval.

Default limit of function evaluations: enough for the bisection to reach the floating point resolution on any interval.
raw docstring

default-relative-accuracyclj

Default relative tolerance, four times the machine epsilon.

Default relative tolerance, four times the machine epsilon.
raw docstring

derivative-evaluatorclj

(derivative-evaluator df)

Wraps the derivative df (a function of one number) for a solver.

Returns a function of x returning the derivative as a double; NaN and infinite derivatives are returned as they are (the caller replaces the step by a bisection step). Throws ex-info with the status :invalid-result when df does not return a number.

Wraps the derivative `df` (a function of one number) for a solver.

Returns a function of `x` returning the derivative as a double; `NaN` and infinite derivatives are returned as they are (the caller replaces the step by a bisection step). Throws `ex-info` with the status `:invalid-result` when `df` does not return a number.
raw docstring

evaluatorclj

(evaluator f max-evals)

Wraps the function f for a solver.

Parameters:

  • f (function): the function of one number.
  • max-evals (long): the limit of evaluations.

Returns an IEvaluator with (.eval ev x) and (.evaluations ev). The evaluation throws ex-info with the status :max-evals when the limit is reached, :nan when f returns NaN and :invalid-result when f does not return a number. Infinite values are returned as they are.

Contract: f is called only with finite doubles.

Wraps the function `f` for a solver.

Parameters:

- `f` (function): the function of one number.
- `max-evals` (long): the limit of evaluations.

Returns an `IEvaluator` with `(.eval ev x)` and `(.evaluations ev)`. The evaluation throws `ex-info` with the status `:max-evals` when the limit is reached, `:nan` when `f` returns `NaN` and `:invalid-result` when `f` does not return a number. Infinite values are returned as they are.

Contract: `f` is called only with finite doubles.
raw docstring

false-positionclj

(false-position variant p)

Iteration of the false position family on the bracket of the [[Problem]] p; returns the root.

variant selects what happens to the retained end of the bracket when the new point has the same sign as the newest one: :regula-falsi keeps its value, :illinois halves it and :pegasus scales it by f1 / (f1 + f).

The search ends when the bracket is narrower than twice the tolerance, or when the step is within the tolerance and the point is certified (see certified?). The new point is always strictly inside the bracket, a step which could not move is replaced by the middle.

Iteration of the false position family on the bracket of the [[Problem]] `p`; returns the root.

`variant` selects what happens to the retained end of the bracket when the new point has the same sign as the newest one: `:regula-falsi` keeps its value, `:illinois` halves it and `:pegasus` scales it by `f1 / (f1 + f)`.

The search ends when the bracket is narrower than twice the tolerance, or when the step is within the tolerance and the point is certified (see [[certified?]]). The new point is always strictly inside the bracket, a step which could not move is replaced by the middle.
raw docstring

finite-numberclj

(finite-number option v)

Returns v as a double when it is a finite number, throws ex-info (see throw-invalid-option) for the option option otherwise.

Returns `v` as a double when it is a finite number, throws `ex-info` (see [[throw-invalid-option]]) for the option `option` otherwise.
raw docstring

long-optionclj

(long-option options option default)

Reads the option option of the map options: an integer not less than 1, or default when the option is missing. Throws ex-info (see throw-invalid-option) for any other value.

Reads the option `option` of the map `options`: an integer not less than `1`, or `default` when the option is missing. Throws `ex-info` (see [[throw-invalid-option]]) for any other value.
raw docstring

muller-stepclj

(muller-step xs ys)

Muller's quadratic through three points, centered at the middle one.

Parameters: xs (Vec3) the abscissae [xa xc xb], the center xc is the middle one, and ys (Vec3) the values [ya yc yb]. The abscissae have to be distinct.

Returns a Vec4 [x y z w]: x and y are the two real roots of the parabola (NaN when the discriminant is negative, x is the root with the larger denominator, nearer to xc), z the slope of the parabola at xc and w its curvature.

Muller's quadratic through three points, centered at the middle one.

Parameters: `xs` (`Vec3`) the abscissae `[xa xc xb]`, the center `xc` is the middle one, and `ys` (`Vec3`) the values `[ya yc yb]`. The abscissae have to be distinct.

Returns a `Vec4` `[x y z w]`: `x` and `y` are the two real roots of the parabola (`NaN` when the discriminant is negative, `x` is the root with the larger denominator, nearer to `xc`), `z` the slope of the parabola at `xc` and `w` its curvature.
raw docstring

newton-iterationclj

(newton-iteration variant df d2f p)

Iteration of the Newton (:newton) or Halley (:halley) method on the bracket of the [[Problem]] p; returns the root.

df and d2f are functions of x returning the first and the second derivative as doubles (d2f is ignored by Newton).

The iteration is safeguarded by the bracket: a step which would leave the bracket, is not finite or does not converge at least as fast as halving (the safeguard of the bisection) is replaced by the bisection step, so the iteration cannot fail on a function with a sign change. The start is the start of the problem or the middle of the bracket. The search ends when the bracket is narrower than twice the tolerance, or when the step is within the tolerance and the point is certified (see certified?).

Iteration of the Newton (`:newton`) or Halley (`:halley`) method on the bracket of the [[Problem]] `p`; returns the root.

`df` and `d2f` are functions of `x` returning the first and the second derivative as doubles (`d2f` is ignored by Newton).

The iteration is safeguarded by the bracket: a step which would leave the bracket, is not finite or does not converge at least as fast as halving (the safeguard of the bisection) is replaced by the bisection step, so the iteration cannot fail on a function with a sign change. The start is the `start` of the problem or the middle of the bracket. The search ends when the bracket is narrower than twice the tolerance, or when the step is within the tolerance and the point is certified (see [[certified?]]).
raw docstring

parse-optionsclj

(parse-options options)

Validates the options of a solve.

Parameters:

  • options (map or nil): :absolute-accuracy, :relative-accuracy (finite numbers not less than 0), :max-iters (integer not less than 1, the limit of function evaluations), :initial-value (a finite number), :info? (truthy to return the info map). Unknown keys are ignored.

Returns a map with :atol, :rtol (doubles), :max-evals (long), :start (a double, NaN when :initial-value is not given) and :info? (boolean). Throws ex-info (see throw-invalid-option) for an invalid value.

See also tolerance.

Validates the options of a solve.

Parameters:

- `options` (map or `nil`): `:absolute-accuracy`, `:relative-accuracy` (finite numbers not less than `0`), `:max-iters` (integer not less than `1`, the limit of function evaluations), `:initial-value` (a finite number), `:info?` (truthy to return the info map). Unknown keys are ignored.

Returns a map with `:atol`, `:rtol` (doubles), `:max-evals` (long), `:start` (a double, `NaN` when `:initial-value` is not given) and `:info?` (boolean). Throws `ex-info` (see [[throw-invalid-option]]) for an invalid value.

See also [[tolerance]].
raw docstring

solve-bracketedclj

(solve-bracketed core uses-start? f lower upper options)

Runs the iteration core of a solver on a sign change of f.

Parameters:

  • core (function): (core ^Problem p) returns the root as a double. It reads the evaluator, the bracket [a, b] with the values fa, fb (opposite signs, a < b), the tolerance atol, rtol and start (NaN when not given). It throws with throw-status when it does not converge.
  • uses-start? (boolean): when true, :initial-value has to lie strictly inside the bracket; when false it is ignored.
  • f, lower, upper, options: the arguments of the solver.

Bounds are swapped when reversed. A root at an end of the interval is returned after at most two evaluations. Equal bounds return the point when f is zero there. Returns the root, or with :info? the map {:root :evaluations :status}; the status of a failed solve is returned instead of thrown.

Throws ex-info for an invalid argument (see throw-invalid-option), and for a failed solve (see throw-status) when :info? is not set.

Runs the iteration `core` of a solver on a sign change of `f`.

Parameters:

- `core` (function): `(core ^Problem p)` returns the root as a double. It reads the evaluator, the bracket `[a, b]` with the values `fa`, `fb` (opposite signs, `a < b`), the tolerance `atol`, `rtol` and `start` (`NaN` when not given). It throws with [[throw-status]] when it does not converge.
- `uses-start?` (boolean): when `true`, `:initial-value` has to lie strictly inside the bracket; when `false` it is ignored.
- `f`, `lower`, `upper`, `options`: the arguments of the solver.

Bounds are swapped when reversed. A root at an end of the interval is returned after at most two evaluations. Equal bounds return the point when `f` is zero there. Returns the root, or with `:info?` the map `{:root :evaluations :status}`; the status of a failed solve is returned instead of thrown.

Throws `ex-info` for an invalid argument (see [[throw-invalid-option]]), and for a failed solve (see [[throw-status]]) when `:info?` is not set.
raw docstring

solve-with-derivativesclj

(solve-with-derivatives variant f lower upper options)

Solves with the safeguarded Newton (:newton) or Halley (:halley) iteration, see newton-iteration.

Parameters: variant, the function f, the bounds and the options of the solver; the options :df and :d2f are the first and the second derivative of f (functions of one number) and default to central finite differences (order 4 and 2) of f, whose evaluations count to :max-iters.

Throws ex-info (see throw-invalid-option) when :df or :d2f is given and is not a function, before any evaluation of f.

Solves with the safeguarded Newton (`:newton`) or Halley (`:halley`) iteration, see [[newton-iteration]].

Parameters: `variant`, the function `f`, the bounds and the `options` of the solver; the options `:df` and `:d2f` are the first and the second derivative of `f` (functions of one number) and default to central finite differences (order 4 and 2) of `f`, whose evaluations count to `:max-iters`.

Throws `ex-info` (see [[throw-invalid-option]]) when `:df` or `:d2f` is given and is not a function, before any evaluation of `f`.
raw docstring

statusesclj

Statuses of a solve which did not end with a root: :max-evals (the limit of evaluations is reached), :no-bracket (no sign change), :nan (the function is NaN), :diverged (the iteration left the interval, stalled or the result is not a root), :invalid-result (the function returned a non number). A solve which found the root has the status :converged.

Statuses of a solve which did not end with a root: `:max-evals` (the limit of evaluations is reached), `:no-bracket` (no sign change), `:nan` (the function is `NaN`), `:diverged` (the iteration left the interval, stalled or the result is not a root), `:invalid-result` (the function returned a non number). A solve which found the root has the status `:converged`.
raw docstring

throw-invalid-optionclj

(throw-invalid-option option value reason)

Throws ex-info reporting an invalid value of an option or an argument.

Parameters:

  • option (keyword): name of the option, for example :max-iters.
  • value: the rejected value.
  • reason (string): what the value has to satisfy.

The exception data is {:option option :value value :reason reason}. Never returns.

Throws `ex-info` reporting an invalid value of an option or an argument.

Parameters:

- `option` (keyword): name of the option, for example `:max-iters`.
- `value`: the rejected value.
- `reason` (string): what the value has to satisfy.

The exception data is `{:option option :value value :reason reason}`. Never returns.
raw docstring

throw-statusclj

(throw-status status message data)

Throws ex-info for a solve which ended without a root.

Parameters:

  • status (keyword): one of statuses.
  • message (string): the description.
  • data (map): scalars which help to locate the problem (an evaluation count, a point), never a sequence of iterates.

The exception data is data with :status added; the exception is an ex-info marked as a solver failure, which call-guarded turns into an info map. Never returns.

Throws `ex-info` for a solve which ended without a root.

Parameters:

- `status` (keyword): one of [[statuses]].
- `message` (string): the description.
- `data` (map): scalars which help to locate the problem (an evaluation count, a point), never a sequence of iterates.

The exception data is `data` with `:status` added; the exception is an `ex-info` marked as a solver failure, which [[call-guarded]] turns into an info map. Never returns.
raw docstring

toleranceclj

(tolerance atol rtol x)

Tolerance of the root at x: atol + rtol * |x|, never less than two units in the last place of x, so that the iteration can always end.

Tolerance of the root at `x`: `atol + rtol * |x|`, never less than two units in the last place of `x`, so that the iteration can always end.
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