Search
View source code
Display the source code in std/numeric.d from which this page was generated on github.
Report a bug
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

# `std.numeric.findRoot` - multiple declarations

## Function findRoot

Find a real root of a real function f(x) via bracketing.

``` T findRoot(T, DF, DT) (   scope DF f,   const T a,   const T b,   scope DT tolerance ) if (isFloatingPoint!T && is(typeof(tolerance(T.init, T.init)) : bool) && is(typeof(f(T.init)) == R, R) && isFloatingPoint!R); T findRoot(T, DF) (   scope DF f,   const T a,   const T b ); ```

Given a function `f` and a range `[a .. b]` such that `f(a)` and `f(b)` have opposite signs or at least one of them equals ±0, returns the value of `x` in the range which is closest to a root of `f(x)`. If `f(x)` has more than one root in the range, one will be chosen arbitrarily. If `f(x)` returns NaN, NaN will be returned; otherwise, this algorithm is guaranteed to succeed.

Uses an algorithm based on TOMS748, which uses inverse cubic interpolation whenever possible, otherwise reverting to parabolic or secant interpolation. Compared to TOMS748, this implementation improves worst-case performance by a factor of more than 100, and typical performance by a factor of 2. For 80-bit reals, most problems require 8 to 15 calls to `f(x)` to achieve full machine precision. The worst-case performance (pathological cases) is approximately twice the number of bits.

### References

"On Enclosing Simple Roots of Nonlinear Equations", G. Alefeld, F.A. Potra, Yixun Shi, Mathematics of Computation 61, pp733-744 (1993). Fortran code available from www.netlib.org as algorithm TOMS478.

## Function findRoot

Find root of a real function f(x) by bracketing, allowing the termination condition to be specified.

``` Tuple!(T,T,R,R) findRoot(T, R, DF, DT) (   scope DF f,   const T ax,   const T bx,   const R fax,   const R fbx,   scope DT tolerance ) if (isFloatingPoint!T && is(typeof(tolerance(T.init, T.init)) : bool) && is(typeof(f(T.init)) == R) && isFloatingPoint!R); Tuple!(T,T,R,R) findRoot(T, R, DF) (   scope DF f,   const T ax,   const T bx,   const R fax,   const R fbx ); T findRoot(T, R) (   scope R delegate(T) f,   const T a,   const T b,   scope bool delegate(T lo, T hi) tolerance = ); ```

### Parameters

NameDescription
f Function to be analyzed
ax Left bound of initial range of `f` known to contain the root.
bx Right bound of initial range of `f` known to contain the root.
fax Value of `f(ax)`.
fbx Value of `f(bx)`. `fax` and `fbx` should have opposite signs. (`f(ax)` and `f(bx)` are commonly known in advance.)
tolerance Defines an early termination condition. Receives the current upper and lower bounds on the root. The delegate must return `true` when these bounds are acceptable. If this function always returns `false`, full machine precision will be achieved.

### Returns

A tuple consisting of two ranges. The first two elements are the range (in `x`) of the root, while the second pair of elements are the corresponding function values at those points. If an exact root was found, both of the first two elements will contain the root, and the second pair of elements will be 0.

## Authors

Andrei Alexandrescu, Don Clugston, Robert Jacques, Ilya Yaroshenko