The OpenD Programming Language

findSmileRoots

Find one or two roots of a real function f(x) using combination of FindRoot and FindLocalMin.

findSmileRoots
(
alias tolerance = null
T
)
(
const T ax
,
const T bx
,
const T fax = T.nan
,
const T fbx = T.nan
,
const T relTolerance = sqrt(T.epsilon)
,
const T absTolerance = sqrt(T.epsilon)
,
const T lowerBound = T.nan
,
const T upperBound = T.nan
,
uint maxIterations = T.sizeof * 16
,
uint steps = 0
)
if (
__traits(isFloating, T)
)

Parameters

f

Function to be analyzed. If f(ax) and f(bx) have opposite signs one root is returned, otherwise the implementation tries to find a local minimum and returns two roots. At least one of f(ax) and f(bx) should be greater or equal to zero.

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 or it is null, full machine precision will be achieved.

ax T

Left inner bound of initial range of f known to contain the roots.

bx T

Right inner bound of initial range of f known to contain the roots. Can be equal to ax.

fax T

Value of f(ax) (optional).

fbx T

Value of f(bx) (optional).

relTolerance T

Relative tolerance used by findLocalMin.

absTolerance T

Absolute tolerance used by findLocalMin.

lowerBound T
upperBound T
maxIterations uint

Appr. maximum allowed number of function calls for each findRoot call.

steps uint

Return Value

Type: FindSmileRootsResult!T

Examples

Smile

import mir.math.common: approxEqual;
auto result = findSmileRoots!(x => x ^^ 2 - 1)(-10.0, 10.0);
assert(result.roots.length == 2);
assert(result.roots[0].approxEqual(-1));
assert(result.roots[1].approxEqual(+1));
assert(result.smileRoots.left.approxEqual(-1));
assert(result.smileRoots.right.approxEqual(+1));
assert(result.leftResult);
assert(result.rightResult);
assert(result.localMinResult);
assert(result.localMinResult.get.x.approxEqual(0));
assert(result.localMinResult.get.y.approxEqual(-1));

Skew

import mir.math.common: approxEqual;
auto result = findSmileRoots!(x => x ^^ 2 - 1)(0.5, 10.0);
assert(result.roots.length == 1);
assert(result.roots[0].approxEqual(+1));
assert(result.smileRoots.left.approxEqual(+1));
assert(result.smileRoots.right.approxEqual(+1));
assert(!result.leftResult);
assert(result.rightResult);

Meta