Liking cljdoc? Tell your friends :D

fastmath.solver

Root finding of univariate functions and polynomials.

Bracketing methods find a root of a function that changes the sign on an interval and cannot fail on it: brent (default), bisection, illinois, pegasus, regula-falsi, ridders, muller and toms748. secant and muller2 have no bracket: they are faster on smooth functions but fail with the status :diverged when the iteration runs away. newton and halley use derivatives (given or approximated by finite differences) and are safeguarded by the bracket. fixed-point solves g(x) = x.

find-root is the common entry point of all of them, find-roots finds all the roots in an interval and find-bracket finds an interval with a sign change around a point. Every solver lives in its own namespace fastmath.solver.<name> and has the same signature (solve f lower upper options); the options are described in find-root.

quadratic and cubic return the real roots of the polynomials of degree two and three, polyroot all the complex roots of a polynomial of any degree.

Failures are reported with ex-info whose data holds the :status (:no-bracket, :max-evals, :nan, :diverged, :invalid-result) and a few scalars; the option :info? returns the status as a map instead.

Root finding of univariate functions and polynomials.

Bracketing methods find a root of a function that changes the sign on an interval and cannot fail on it: [[brent]] (default), [[bisection]], [[illinois]], [[pegasus]], [[regula-falsi]], [[ridders]], [[muller]] and [[toms748]]. [[secant]] and [[muller2]] have no bracket: they are faster on smooth functions but fail with the status `:diverged` when the iteration runs away. [[newton]] and [[halley]] use derivatives (given or approximated by finite differences) and are safeguarded by the bracket. [[fixed-point]] solves `g(x) = x`.

[[find-root]] is the common entry point of all of them, [[find-roots]] finds all the roots in an interval and [[find-bracket]] finds an interval with a sign change around a point. Every solver lives in its own namespace `fastmath.solver.<name>` and has the same signature `(solve f lower upper options)`; the options are described in [[find-root]].

[[quadratic]] and [[cubic]] return the real roots of the polynomials of degree two and three, [[polyroot]] all the complex roots of a polynomial of any degree.

Failures are reported with `ex-info` whose data holds the `:status` (`:no-bracket`, `:max-evals`, `:nan`, `:diverged`, `:invalid-result`) and a few scalars; the option `:info?` returns the status as a map instead.
raw docstring

bisectionclj

(bisection f lower upper)
(bisection f lower upper options)

Finds a root of f on [lower, upper] by halving the interval.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200, enough to reach the floating point resolution on any interval. Exceeding it throws.
    • :initial-value - ignored by this method.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double, the middle of the last interval. A root at an end of the interval is returned after at most two evaluations. At the default tolerance the result is within a few units in the last place; on a jump of the function it is the point of the jump.

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.brent/brent.

Finds a root of `f` on `[lower, upper]` by halving the interval.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`, enough to reach the floating point resolution on any interval. Exceeding it throws.
    - `:initial-value` - ignored by this method.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double, the middle of the last interval. A root at an end of the interval is returned after at most two evaluations. At the default tolerance the result is within a few units in the last place; on a jump of the function it is the point of the jump.

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.brent/brent]].
raw docstring

brentclj

(brent f lower upper)
(brent f lower upper options)

Finds a root of f on [lower, upper] with Brent's method.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - the first point of the search, strictly inside the interval, default the plain bracket. The point replaces the end of the interval which has the same sign.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. Function values are never used to stop the search: only the bracket decides, so a function with tiny values is solved as accurately as any other.

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.bisection/bisection, fastmath.solver.toms748/toms748.

Finds a root of `f` on `[lower, upper]` with Brent's method.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - the first point of the search, strictly inside the interval, default the plain bracket. The point replaces the end of the interval which has the same sign.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. Function values are never used to stop the search: only the bracket decides, so a function with tiny values is solved as accurately as any other.

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.bisection/bisection]], [[fastmath.solver.toms748/toms748]].
raw docstring

cubicclj

(cubic a b c d)

Real roots of the cubic equation a x^3 + b x^2 + c x + d = 0.

Parameters: a, b, c, d (numbers): the coefficients.

Returns a Vec3 [r0 r1 r2] with the real roots in ascending order, NaN after the last real root: one real root gives [r NaN NaN], three real roots (a multiple root is returned as many times as its multiplicity) give all three places. [NaN NaN NaN] is returned when any coefficient is not finite. For a = 0 the equation is quadratic and the result is the result of quadratic followed by NaN. For d = 0 one root is exactly 0.0.

A polynomial which differs from one with a multiple real root by no more than the rounding error of its coefficients is reported with the multiple root (a double root is returned twice, a triple root three times), with a relative accuracy of the root of the order of the square root, respectively the cube root, of the machine epsilon.

The magnitudes of the roots are estimated from the exponents of the coefficients (the Newton polygon) and every group of roots is solved in its own scale: the critical points of the scaled cubic split the line into monotone parts, and the root of every part with a sign change is found by Brent's method. No root is deflated from the polynomial and the coefficients are scaled by powers of two, so the roots keep a small componentwise backward error also when they differ by many orders of magnitude, and the result is correct over the whole range of doubles whenever the roots are representable. A root beyond the range is Infinity, a root below the range is 0.0.

See also quadratic, fastmath.solver/polyroot.

Real roots of the cubic equation `a x^3 + b x^2 + c x + d = 0`.

Parameters: `a`, `b`, `c`, `d` (numbers): the coefficients.

Returns a `Vec3` `[r0 r1 r2]` with the real roots in ascending order, `NaN` after the last real root: one real root gives `[r NaN NaN]`, three real roots (a multiple root is returned as many times as its multiplicity) give all three places. `[NaN NaN NaN]` is returned when any coefficient is not finite. For `a = 0` the equation is quadratic and the result is the result of [[quadratic]] followed by `NaN`. For `d = 0` one root is exactly `0.0`.

A polynomial which differs from one with a multiple real root by no more than the rounding error of its coefficients is reported with the multiple root (a double root is returned twice, a triple root three times), with a relative accuracy of the root of the order of the square root, respectively the cube root, of the machine epsilon.

The magnitudes of the roots are estimated from the exponents of the coefficients (the Newton polygon) and every group of roots is solved in its own scale: the critical points of the scaled cubic split the line into monotone parts, and the root of every part with a sign change is found by Brent's method. No root is deflated from the polynomial and the coefficients are scaled by powers of two, so the roots keep a small componentwise backward error also when they differ by many orders of magnitude, and the result is correct over the whole range of doubles whenever the roots are representable. A root beyond the range is `Infinity`, a root below the range is `0.0`.

See also [[quadratic]], [[fastmath.solver/polyroot]].
raw docstring

find-bracketclj

(find-bracket f x0)
(find-bracket f x0 options)

Finds an interval with a sign change of f around the point x0 by expanding it geometrically.

Parameters:

  • f (function): a function of one number returning a number.
  • x0 (finite number): the point.
  • options (map, optional):
    • :step - half-width of the first interval, a positive number, default 0.1 * max(1, |x0|).
    • :factor - the growth of the interval in every step, greater than 1, default 1.6. In every step the end with the smaller absolute value of f is moved outwards.
    • :lower, :upper - limits of the search, default none (##-Inf, ##Inf). An end which reaches a limit stays there.
    • :max-iters - limit of function evaluations, default 200.

Returns the vector [a b] with a < b and a sign change (or a zero) between f(a) and f(b), ready for find-root.

Throws ex-info for an invalid argument, with the status :no-bracket when both ends reached the limits :lower and :upper without a sign change, :max-evals when the number of evaluations is exhausted, :nan when f is NaN and :invalid-result when it is not a number. The data holds the :status, the :evaluations and the last interval :a, :b.

See also find-root, find-roots.

Finds an interval with a sign change of `f` around the point `x0` by expanding it geometrically.

Parameters:

- `f` (function): a function of one number returning a number.
- `x0` (finite number): the point.
- `options` (map, optional):
    - `:step` - half-width of the first interval, a positive number, default `0.1 * max(1, |x0|)`.
    - `:factor` - the growth of the interval in every step, greater than `1`, default `1.6`. In every step the end with the smaller absolute value of `f` is moved outwards.
    - `:lower`, `:upper` - limits of the search, default none (`##-Inf`, `##Inf`). An end which reaches a limit stays there.
    - `:max-iters` - limit of function evaluations, default `200`.

Returns the vector `[a b]` with `a < b` and a sign change (or a zero) between `f(a)` and `f(b)`, ready for [[find-root]].

Throws `ex-info` for an invalid argument, with the status `:no-bracket` when both ends reached the limits `:lower` and `:upper` without a sign change, `:max-evals` when the number of evaluations is exhausted, `:nan` when `f` is `NaN` and `:invalid-result` when it is not a number. The data holds the `:status`, the `:evaluations` and the last interval `:a`, `:b`.

See also [[find-root]], [[find-roots]].
raw docstring

find-rootclj

(find-root f lower upper)
(find-root f lower upper options)

Finds a root of the function f on the interval [lower, upper].

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :solver - the method, one of :brent (default), :bisection, :illinois, :pegasus, :regula-falsi, :ridders, :muller, :muller2, :secant, :toms748, :newton, :halley; see the keys of solvers. An unknown method throws.
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|, so the default is the resolution of doubles at any scale of the root.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - the first point of the search, strictly inside the interval; used by :brent, :muller, :newton and :halley, ignored by the other methods.
    • :df, :d2f - the first and the second derivative of f, used by :newton and :halley; the default is a central finite difference.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The function value is never used to stop the search, so a function with tiny values is solved as accurately as any other.

Throws ex-info for an invalid argument or option, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result), the evaluation limit (:max-evals) or a runaway iteration of :secant and :muller2 (:diverged). The data of the exception holds the :status, the number of :evaluations and a few scalars.

See also find-roots, find-bracket, solvers, fixed-point.

Finds a root of the function `f` on the interval `[lower, upper]`.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:solver` - the method, one of `:brent` (default), `:bisection`, `:illinois`, `:pegasus`, `:regula-falsi`, `:ridders`, `:muller`, `:muller2`, `:secant`, `:toms748`, `:newton`, `:halley`; see the keys of [[solvers]]. An unknown method throws.
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`, so the default is the resolution of doubles at any scale of the root.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - the first point of the search, strictly inside the interval; used by `:brent`, `:muller`, `:newton` and `:halley`, ignored by the other methods.
    - `:df`, `:d2f` - the first and the second derivative of `f`, used by `:newton` and `:halley`; the default is a central finite difference.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The function value is never used to stop the search, so a function with tiny values is solved as accurately as any other.

Throws `ex-info` for an invalid argument or option, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`), the evaluation limit (`:max-evals`) or a runaway iteration of `:secant` and `:muller2` (`:diverged`). The data of the exception holds the `:status`, the number of `:evaluations` and a few scalars.

See also [[find-roots]], [[find-bracket]], [[solvers]], [[fixed-point]].
raw docstring

find-rootsclj

(find-roots f lower upper)
(find-roots f lower upper options)

Finds all the roots of f on the interval [lower, upper] that are sign changes at the resolution of a grid.

The interval is divided into :samples equal parts; every part with a sign change is solved by find-root, and a grid point with f = 0 is a root. A pole is a sign change too, but it is not a root: a solution where the absolute value of f is larger than at both ends of its part is dropped.

Parameters:

  • f (function): a function of one number returning a number.
  • lower, upper (finite numbers): ends of the interval, lower < upper.
  • options (map, optional):
    • :samples - the number of parts, an integer not less than 1, default 1000.
    • all the options of find-root; :max-iters applies to every root separately, :info? and :initial-value are not supported and ignored. The default :absolute-accuracy is 1e-15 * max(|lower|, |upper|), so that the search of a pole, which attracts the solver, ends quickly.

Returns a vector of the roots in ascending order, empty when there is none. A root inside a part where the function changes the sign twice (two close roots), and a root of an even multiplicity (a touch of zero without a sign change) are missed: raise :samples to resolve close roots. Parts where f is NaN at an end are skipped.

Throws ex-info for an invalid argument or option, and the failures of find-root for a part (for example :max-evals).

See also find-root, find-bracket.

Finds all the roots of `f` on the interval `[lower, upper]` that are sign changes at the resolution of a grid.

The interval is divided into `:samples` equal parts; every part with a sign change is solved by [[find-root]], and a grid point with `f = 0` is a root. A pole is a sign change too, but it is not a root: a solution where the absolute value of `f` is larger than at both ends of its part is dropped.

Parameters:

- `f` (function): a function of one number returning a number.
- `lower`, `upper` (finite numbers): ends of the interval, `lower < upper`.
- `options` (map, optional):
    - `:samples` - the number of parts, an integer not less than `1`, default `1000`.
    - all the options of [[find-root]]; `:max-iters` applies to every root separately, `:info?` and `:initial-value` are not supported and ignored. The default `:absolute-accuracy` is `1e-15 * max(|lower|, |upper|)`, so that the search of a pole, which attracts the solver, ends quickly.

Returns a vector of the roots in ascending order, empty when there is none. A root inside a part where the function changes the sign twice (two close roots), and a root of an even multiplicity (a touch of zero without a sign change) are missed: raise `:samples` to resolve close roots. Parts where `f` is `NaN` at an end are skipped.

Throws `ex-info` for an invalid argument or option, and the failures of [[find-root]] for a part (for example `:max-evals`).

See also [[find-root]], [[find-bracket]].
raw docstring

fixed-pointclj

(fixed-point g x0)
(fixed-point g x0 options)

Finds a fixed point of g, a solution of g(x) = x, by iteration from x0.

Parameters:

  • g (function): a function of one number returning a number.
  • x0 (finite number): the start.
  • options (map, optional):
    • :steffensen? - when truthy, uses the Steffensen acceleration; each step costs two evaluations. Default false.
    • :absolute-accuracy - absolute tolerance, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the result, default four times the machine epsilon. The iteration stops when the step is within absolute + relative * |x|.
    • :max-iters - limit of evaluations of g (an integer), default 1200. Exceeding it throws.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the fixed point, and returns the failure status instead of throwing. Default false.

Returns the fixed point as a double. The step size is the only stopping rule: a small step of a slowly converging iteration (a rate close to 1) does not mean a small error. The error is about step * rate / (1 - rate).

Throws ex-info for an invalid argument, and for a failed iteration: iterates which are not finite (:status :diverged), NaN values of g (:nan), a non number result (:invalid-result) or the evaluation limit (:max-evals, which is also the result of a diverging or oscillating iteration). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.newton/newton.

Finds a fixed point of `g`, a solution of `g(x) = x`, by iteration from `x0`.

Parameters:

- `g` (function): a function of one number returning a number.
- `x0` (finite number): the start.
- `options` (map, optional):
    - `:steffensen?` - when truthy, uses the Steffensen acceleration; each step costs two evaluations. Default `false`.
    - `:absolute-accuracy` - absolute tolerance, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the result, default four times the machine epsilon. The iteration stops when the step is within `absolute + relative * |x|`.
    - `:max-iters` - limit of evaluations of `g` (an integer), default `1200`. Exceeding it throws.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the fixed point, and returns the failure status instead of throwing. Default `false`.

Returns the fixed point as a double. The step size is the only stopping rule: a small step of a slowly converging iteration (a rate close to 1) does not mean a small error. The error is about `step * rate / (1 - rate)`.

Throws `ex-info` for an invalid argument, and for a failed iteration: iterates which are not finite (`:status :diverged`), `NaN` values of `g` (`:nan`), a non number result (`:invalid-result`) or the evaluation limit (`:max-evals`, which is also the result of a diverging or oscillating iteration). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.newton/newton]].
raw docstring

halleyclj

(halley f lower upper)
(halley f lower upper options)

Finds a root of f on [lower, upper] with Halley's method safeguarded by the bracket.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :df, :d2f - the first and the second derivative of f, functions of one number returning a number. Default central finite differences of order 4 and 2 (the evaluations of f made by them count to :max-iters).
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of evaluations of f (an integer), default 1200; a call of a derivative is made at most once per evaluation of f. Exceeding the limit throws.
    • :initial-value - the first point, strictly inside the interval, default the middle of the interval.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. A derivative which is zero, NaN or infinite never stops the search: the step is replaced by a bisection step.

Throws ex-info for an invalid argument (an option which is not a function included), and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result of f or of a derivative (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.newton/newton, fastmath.solver.brent/brent.

Finds a root of `f` on `[lower, upper]` with Halley's method safeguarded by the bracket.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:df`, `:d2f` - the first and the second derivative of `f`, functions of one number returning a number. Default central finite differences of order 4 and 2 (the evaluations of `f` made by them count to `:max-iters`).
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of evaluations of `f` (an integer), default `1200`; a call of a derivative is made at most once per evaluation of `f`. Exceeding the limit throws.
    - `:initial-value` - the first point, strictly inside the interval, default the middle of the interval.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. A derivative which is zero, `NaN` or infinite never stops the search: the step is replaced by a bisection step.

Throws `ex-info` for an invalid argument (an option which is not a function included), and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result of `f` or of a derivative (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.newton/newton]], [[fastmath.solver.brent/brent]].
raw docstring

illinoisclj

(illinois f lower upper)
(illinois f lower upper options)

Finds a root of f on [lower, upper] with the Illinois method.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - ignored by this method.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The new point always stays inside the bracket, so the method does not leave the interval; it stops when the bracket is narrower than the tolerance, or when the step is within the tolerance and the point is confirmed by a sign change at a distance of two tolerances.

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.regula-falsi/regula-falsi, fastmath.solver.pegasus/pegasus, fastmath.solver.brent/brent.

Finds a root of `f` on `[lower, upper]` with the Illinois method.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - ignored by this method.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The new point always stays inside the bracket, so the method does not leave the interval; it stops when the bracket is narrower than the tolerance, or when the step is within the tolerance and the point is confirmed by a sign change at a distance of two tolerances.

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.regula-falsi/regula-falsi]], [[fastmath.solver.pegasus/pegasus]], [[fastmath.solver.brent/brent]].
raw docstring

mullerclj

(muller f lower upper)
(muller f lower upper options)

Finds a root of f on [lower, upper] with Muller's method safeguarded by the bracket.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - the third point of the first parabola, strictly inside the interval, default the middle of the interval.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. A negative discriminant or a parabola without a root inside the bracket never produces NaN: a bisection step is made instead.

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.muller2/muller2, fastmath.solver.brent/brent.

Finds a root of `f` on `[lower, upper]` with Muller's method safeguarded by the bracket.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - the third point of the first parabola, strictly inside the interval, default the middle of the interval.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. A negative discriminant or a parabola without a root inside the bracket never produces `NaN`: a bisection step is made instead.

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.muller2/muller2]], [[fastmath.solver.brent/brent]].
raw docstring

muller2clj

(muller2 f lower upper)
(muller2 f lower upper options)

Finds a root of f on [lower, upper] with Muller's method, without a bracket.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped. The interval is also the limit of the search.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - ignored by this method.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The search stops when the step is within the tolerance and the function changes the sign within two tolerances of the point.

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result), the evaluation limit (:max-evals) or a runaway iteration (:diverged): the iterate leaves the interval, or the step converged to a point which is not a root. The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.muller/muller, fastmath.solver.secant/secant.

Finds a root of `f` on `[lower, upper]` with Muller's method, without a bracket.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped. The interval is also the limit of the search.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - ignored by this method.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The search stops when the step is within the tolerance and the function changes the sign within two tolerances of the point.

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`), the evaluation limit (`:max-evals`) or a runaway iteration (`:diverged`): the iterate leaves the interval, or the step converged to a point which is not a root. The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.muller/muller]], [[fastmath.solver.secant/secant]].
raw docstring

newtonclj

(newton f lower upper)
(newton f lower upper options)

Finds a root of f on [lower, upper] with Newton's method safeguarded by the bracket.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :df - the first derivative of f, a function of one number returning a number. Default a central finite difference of order 4 (the evaluations of f made by it count to :max-iters).
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of evaluations of f (an integer), default 1200; a call of a derivative is made at most once per evaluation of f. Exceeding the limit throws.
    • :initial-value - the first point, strictly inside the interval, default the middle of the interval.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. A derivative which is zero, NaN or infinite never stops the search: the step is replaced by a bisection step.

Throws ex-info for an invalid argument (an option which is not a function included), and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result of f or of a derivative (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.halley/halley, fastmath.solver.brent/brent.

Finds a root of `f` on `[lower, upper]` with Newton's method safeguarded by the bracket.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:df` - the first derivative of `f`, a function of one number returning a number. Default a central finite difference of order 4 (the evaluations of `f` made by it count to `:max-iters`).
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of evaluations of `f` (an integer), default `1200`; a call of a derivative is made at most once per evaluation of `f`. Exceeding the limit throws.
    - `:initial-value` - the first point, strictly inside the interval, default the middle of the interval.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. A derivative which is zero, `NaN` or infinite never stops the search: the step is replaced by a bisection step.

Throws `ex-info` for an invalid argument (an option which is not a function included), and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result of `f` or of a derivative (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.halley/halley]], [[fastmath.solver.brent/brent]].
raw docstring

pegasusclj

(pegasus f lower upper)
(pegasus f lower upper options)

Finds a root of f on [lower, upper] with the Pegasus method.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - ignored by this method.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The new point always stays inside the bracket, so the method does not leave the interval; it stops when the bracket is narrower than the tolerance, or when the step is within the tolerance and the point is confirmed by a sign change at a distance of two tolerances.

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.regula-falsi/regula-falsi, fastmath.solver.illinois/illinois, fastmath.solver.brent/brent.

Finds a root of `f` on `[lower, upper]` with the Pegasus method.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - ignored by this method.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The new point always stays inside the bracket, so the method does not leave the interval; it stops when the bracket is narrower than the tolerance, or when the step is within the tolerance and the point is confirmed by a sign change at a distance of two tolerances.

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.regula-falsi/regula-falsi]], [[fastmath.solver.illinois/illinois]], [[fastmath.solver.brent/brent]].
raw docstring

polyrootclj

(polyroot coeffs)
(polyroot coeffs options)

Finds all the complex roots of a polynomial.

Parameters:

  • coeffs: the coefficients c0, c1, ..., cn in ascending order of the power (the polynomial c0 + c1 z + ... + cn z^n, the order of fastmath.polynomials/polynomial), as a sequence or an array, or a polynomial object of fastmath.polynomials. A coefficient is a real number, a Vec2 (the complex number re + i im) or a pair [re im]. All coefficients have to be finite.
  • options (map, optional):
    • :max-iters - limit of the iterations, the sweeps over the roots not converged yet, default 1200.
    • :info? - when truthy, returns the map described below instead of the vector of roots. Default false.

Returns a vector of the n roots as Vec2 (the complex numbers), each root repeated according to its multiplicity (a multiple root is found with the accuracy of the order of the root of the machine epsilon of the multiplicity). Leading zero coefficients are dropped, trailing zero coefficients give exact zero roots, a constant gives no roots.

Order: for real coefficients the real roots in ascending order, then the pairs of complex conjugates by the real part of the pair with the positive imaginary part first; the conjugates are exact and a root whose imaginary part is within the backward error of zero is real. For complex coefficients by the real part, then the imaginary part.

With :info? returns the map {:roots :backward-errors :condition-numbers :converged? :iterations :status}. The vectors have the order of :roots. A backward error is |p(z)| / sum |c_k| |z|^k of the root, the smallest relative perturbation of the coefficients for which z is an exact root; a condition number sum |c_k| |z|^k / (|z| |p'(z)|) is the factor between the relative perturbation of the coefficients and the relative error of the root; the product bounds the relative error of the root. A zero root has the condition number Infinity. :converged? is a vector of booleans. The status is :converged, :max-evals (the iteration limit is reached) or :diverged (a value is not finite).

Throws ex-info for an invalid argument (an empty or zero polynomial, an invalid coefficient: the data holds its :index, never the coefficients). A polynomial whose roots did not converge throws with the status :max-evals or :diverged and the number of the roots not converged as :unconverged; with :info? the status is returned.

See also fastmath.solver/cubic, fastmath.solver/quadratic, fastmath.solver/find-roots.

Finds all the complex roots of a polynomial.

Parameters:

- `coeffs`: the coefficients `c0, c1, ..., cn` in ascending order of the power (the polynomial `c0 + c1 z + ... + cn z^n`, the order of [[fastmath.polynomials/polynomial]]), as a sequence or an array, or a polynomial object of `fastmath.polynomials`. A coefficient is a real number, a `Vec2` (the complex number `re + i im`) or a pair `[re im]`. All coefficients have to be finite.
- `options` (map, optional):
    - `:max-iters` - limit of the iterations, the sweeps over the roots not converged yet, default `1200`.
    - `:info?` - when truthy, returns the map described below instead of the vector of roots. Default `false`.

Returns a vector of the `n` roots as `Vec2` (the complex numbers), each root repeated according to its multiplicity (a multiple root is found with the accuracy of the order of the root of the machine epsilon of the multiplicity). Leading zero coefficients are dropped, trailing zero coefficients give exact zero roots, a constant gives no roots.

Order: for real coefficients the real roots in ascending order, then the pairs of complex conjugates by the real part of the pair with the positive imaginary part first; the conjugates are exact and a root whose imaginary part is within the backward error of zero is real. For complex coefficients by the real part, then the imaginary part.

With `:info?` returns the map `{:roots :backward-errors :condition-numbers :converged? :iterations :status}`. The vectors have the order of `:roots`. A backward error is `|p(z)| / sum |c_k| |z|^k` of the root, the smallest relative perturbation of the coefficients for which `z` is an exact root; a condition number `sum |c_k| |z|^k / (|z| |p'(z)|)` is the factor between the relative perturbation of the coefficients and the relative error of the root; the product bounds the relative error of the root. A zero root has the condition number `Infinity`. `:converged?` is a vector of booleans. The status is `:converged`, `:max-evals` (the iteration limit is reached) or `:diverged` (a value is not finite).

Throws `ex-info` for an invalid argument (an empty or zero polynomial, an invalid coefficient: the data holds its `:index`, never the coefficients). A polynomial whose roots did not converge throws with the status `:max-evals` or `:diverged` and the number of the roots not converged as `:unconverged`; with `:info?` the status is returned.

See also [[fastmath.solver/cubic]], [[fastmath.solver/quadratic]], [[fastmath.solver/find-roots]].
raw docstring

quadraticclj

(quadratic a b c)

Real roots of the quadratic equation a x^2 + b x + c = 0.

Parameters: a, b, c (numbers): the coefficients.

Returns a Vec2 [r0 r1] with the real roots in ascending order. A double root is returned twice. NaN fills the places of the missing roots: [NaN NaN] when the roots are not real (negative discriminant), when all the coefficients but c are zero (c = 0 is an identity, c != 0 has no solution, the two cases are not distinguished), and when any coefficient is not finite. For a = 0 the equation is linear and the result is [-c/b NaN].

The coefficients are scaled by a power of two before the discriminant is formed, so the result does not overflow or underflow when the roots are representable. The roots are computed without cancellation, each with a relative error of a few units in the last place.

See also cubic, fastmath.solver/polyroot.

Real roots of the quadratic equation `a x^2 + b x + c = 0`.

Parameters: `a`, `b`, `c` (numbers): the coefficients.

Returns a `Vec2` `[r0 r1]` with the real roots in ascending order. A double root is returned twice. `NaN` fills the places of the missing roots: `[NaN NaN]` when the roots are not real (negative discriminant), when all the coefficients but `c` are zero (`c = 0` is an identity, `c != 0` has no solution, the two cases are not distinguished), and when any coefficient is not finite. For `a = 0` the equation is linear and the result is `[-c/b NaN]`.

The coefficients are scaled by a power of two before the discriminant is formed, so the result does not overflow or underflow when the roots are representable. The roots are computed without cancellation, each with a relative error of a few units in the last place.

See also [[cubic]], [[fastmath.solver/polyroot]].
raw docstring

regula-falsiclj

(regula-falsi f lower upper)
(regula-falsi f lower upper options)

Finds a root of f on [lower, upper] with the regula falsi method.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - ignored by this method.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The new point always stays inside the bracket, so the method does not leave the interval; it stops when the bracket is narrower than the tolerance, or when the step is within the tolerance and the point is confirmed by a sign change at a distance of two tolerances.

The convergence is linear and can be extremely slow: a flat multiple root such as (x-1)^3, a pole or a strongly convex function like exp(x) - 1e10 on [0, 100] do not converge within thousands of evaluations (raise :max-iters, or use fastmath.solver.illinois/illinois or fastmath.solver.brent/brent).

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.illinois/illinois, fastmath.solver.pegasus/pegasus, fastmath.solver.brent/brent.

Finds a root of `f` on `[lower, upper]` with the regula falsi method.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - ignored by this method.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The new point always stays inside the bracket, so the method does not leave the interval; it stops when the bracket is narrower than the tolerance, or when the step is within the tolerance and the point is confirmed by a sign change at a distance of two tolerances.

The convergence is linear and can be extremely slow: a flat multiple root such as `(x-1)^3`, a pole or a strongly convex function like `exp(x) - 1e10` on `[0, 100]` do not converge within thousands of evaluations (raise `:max-iters`, or use [[fastmath.solver.illinois/illinois]] or [[fastmath.solver.brent/brent]]).

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.illinois/illinois]], [[fastmath.solver.pegasus/pegasus]], [[fastmath.solver.brent/brent]].
raw docstring

riddersclj

(ridders f lower upper)
(ridders f lower upper options)

Finds a root of f on [lower, upper] with Ridders' method.

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - ignored by this method.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. Two function evaluations are made per step.

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.brent/brent, fastmath.solver.illinois/illinois.

Finds a root of `f` on `[lower, upper]` with Ridders' method.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - ignored by this method.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. Two function evaluations are made per step.

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.brent/brent]], [[fastmath.solver.illinois/illinois]].
raw docstring

secantclj

(secant f lower upper)
(secant f lower upper options)

Finds a root of f with the secant method started from the ends of [lower, upper].

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped. The result has to lie in the interval, the iteration itself may leave it.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - ignored by this method.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The search stops when the step is within the tolerance and the function changes the sign within two tolerances of the point.

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result), the evaluation limit (:max-evals) or a runaway iteration (:diverged): an iterate is not finite, two consecutive values are equal, or the step converged to a point which is not a root of the interval. The function is evaluated outside the interval when an iterate leaves it, which gives :nan when the function is not defined there. The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.brent/brent, fastmath.solver.illinois/illinois.

Finds a root of `f` with the secant method started from the ends of `[lower, upper]`.

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped. The result has to lie in the interval, the iteration itself may leave it.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - ignored by this method.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double. A root at an end of the interval is returned after at most two evaluations. The search stops when the step is within the tolerance and the function changes the sign within two tolerances of the point.

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`), the evaluation limit (`:max-evals`) or a runaway iteration (`:diverged`): an iterate is not finite, two consecutive values are equal, or the step converged to a point which is not a root of the interval. The function is evaluated outside the interval when an iterate leaves it, which gives `:nan` when the function is not defined there. The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.brent/brent]], [[fastmath.solver.illinois/illinois]].
raw docstring

solversclj

Map from the keyword of the option :solver of find-root to the solver function (solve f lower upper options).

Map from the keyword of the option `:solver` of [[find-root]] to the solver function `(solve f lower upper options)`.
raw docstring

toms748clj

(toms748 f lower upper)
(toms748 f lower upper options)

Finds a root of f on [lower, upper] with Algorithm 748 (TOMS748).

Parameters:

  • f (function): a function of one number returning a number; it has to change the sign on the interval.
  • lower, upper (finite numbers): ends of the interval; reversed ends are swapped.
  • options (map, optional):
    • :absolute-accuracy - absolute tolerance of the root, default 1.0e-300.
    • :relative-accuracy - tolerance relative to the root, default four times the machine epsilon. The root is found within absolute + relative * |root|.
    • :max-iters - limit of function evaluations (an integer), default 1200. Exceeding it throws.
    • :initial-value - ignored by this method.
    • :info? - when truthy, returns the map {:root :evaluations :status} instead of the root, and returns the failure status instead of throwing. Default false.

Returns the root as a double, the end of the final bracket with the smaller absolute value of the function. A root at an end of the interval is returned after at most two evaluations.

Throws ex-info for an invalid argument, and for a failed solve: no sign change on the interval (:status :no-bracket), NaN function values (:nan), a non number result (:invalid-result) or the evaluation limit (:max-evals). The data of the exception holds the :status and a few scalars.

See also fastmath.solver/find-root, fastmath.solver.brent/brent, fastmath.solver.bisection/bisection.

Finds a root of `f` on `[lower, upper]` with Algorithm 748 (TOMS748).

Parameters:

- `f` (function): a function of one number returning a number; it has to change the sign on the interval.
- `lower`, `upper` (finite numbers): ends of the interval; reversed ends are swapped.
- `options` (map, optional):
    - `:absolute-accuracy` - absolute tolerance of the root, default `1.0e-300`.
    - `:relative-accuracy` - tolerance relative to the root, default four times the machine epsilon. The root is found within `absolute + relative * |root|`.
    - `:max-iters` - limit of function evaluations (an integer), default `1200`. Exceeding it throws.
    - `:initial-value` - ignored by this method.
    - `:info?` - when truthy, returns the map `{:root :evaluations :status}` instead of the root, and returns the failure status instead of throwing. Default `false`.

Returns the root as a double, the end of the final bracket with the smaller absolute value of the function. A root at an end of the interval is returned after at most two evaluations.

Throws `ex-info` for an invalid argument, and for a failed solve: no sign change on the interval (`:status :no-bracket`), `NaN` function values (`:nan`), a non number result (`:invalid-result`) or the evaluation limit (`:max-evals`). The data of the exception holds the `:status` and a few scalars.

See also [[fastmath.solver/find-root]], [[fastmath.solver.brent/brent]], [[fastmath.solver.bisection/bisection]].
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