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 a local clone.

# mir.random.flex.internal.types

struct `Interval`(S) if (isFloatingPoint!S);
Major data unit of the Flex algorithm. It is used to store - (cached) values of the transformation (and its derivatives) - area below the hat and squeeze function - linked-list like reference to the right part of the interval (there will always be exactly one interval with right = 0)
S `lx`;
left position of the interval
S `rx`;
right position of the interval
S `c`;
T_c family of the interval
S `ltx`;
transformed left value of lx
S `lt1x`;
transformed value of the first derivate of the left lx value
S `lt2x`;
transformed value of the second derivate of the left lx value
S `rtx`;
transformed right value of rx
S `rt1x`;
transformed value of the first derivate of the right rx value
S `rt2x`;
transformed value of the second derivate of the right rx value
LinearFun!S `hat`;
hat function of the interval
LinearFun!S `squeeze`;
squeeze function of the interval
S `hatArea`;
calculated area of the integrated hat function
S `squeezeArea`;
calculated area of the integrated squeeze function
string `logHex`();
enum `FunType`: int;
Notations of different function types according to Botts et al. (2013). It is based on this naming scheme:
- a: concAve - b: convex - Type 4 is the pure case without any inflection point
FunType `determineType`(S)(in Interval!S `iv`);
Determine the function type of an interval. Based on Theorem 1 of the Flex paper.
Parameters:
 Interval!S `iv` interval
struct `LinearFun`(S);
Representation of linear function of the form:
y = slope * (x - y) + a
This representation allows a bit higher precision than the typical representation y = slope * x + a.
S `slope`;
direction and steepness (aka beta)
S `y`;
boundary point where f obtains it's maximum
S `a`;
constant intercept
this(S `slope`, S `y`, S `a`);
Parameters:
 S `slope` direction and steepness S `y` boundary point, often f(x) S `a` constant intercept
const void `toString`(W)(auto ref W `w`, ref const FormatSpec!char `fmt`);
textual representation of the function
const S `opCall`(in S `x`);
call the linear function with x
const S `inverse`(S `x`);
calculate inverse of x
string `logHex`();
LinearFun!S `linearFun`(S)(S `slope`, S `y`, S `a`);
Constructs a linear function of the form `y` = `slope` * (x - `y`) + `a`.
Parameters:
 S `slope` direction and steepness S `y` boundary point, often f(x) S `a` constant intercept
Returns:
A linear function constructed with the given parameters.
Examples:
tangent of a point
```import std.format : format;
auto f = (double x) => x * x + 1;
auto df = (double x) => 2 * x;
auto buildTan = (double x) => linearFun(df(x), x, f(x));

auto t0 = buildTan(0);
assert("%l".format(t0)== "1");
assert(t0(0) == 1);
assert(t0(42) == 1);

auto t1 = buildTan(1);
assert("%l".format(t1) == "2x");
assert(t1(1) == 2);
assert(t1(2) == 4);

auto t2 = buildTan(2);
assert("%l".format(t2) == "4x - 3");
assert(t2(1) == 1);
assert(t2(2) == 5);
```
Examples:
secant of two points
```import std.format : format;
auto f = (double x) => x * x + 1;
auto lx = 1, rx = 3;
// compute the slope between lx and rx
auto lf = linearFun((f(rx) - f(lx)) / (rx - lx), lx, f(lx));

assert("%l".format(lf) == "4x - 2");
assert(lf(1) == 2); // f(1)
assert(lf(3) == 10); // f(3)
```
Examples:
construct an arbitrary linear function
```import std.format : format;

// 2 * x + 1
auto t = linearFun!double(2, 0, 1);
assert("%l".format(t) == "2x + 1");
assert(t(1) == 3);
assert(t(-2) == -3);
```
bool `approxEqual`(S)(LinearFun!S `x`, LinearFun!S `y`, S `maxRelDiff` = 0.01, S `maxAbsDiff` = 1e-05);
Compares whether to linear functions are approximately equal.
Parameters:
 LinearFun!S `x` first linear function to compare LinearFun!S `y` second linear function to compare S `maxRelDiff` maximum relative difference S `maxAbsDiff` maximum absolute difference
Returns:
True if both linear functions are approximately equal.
Examples:
```auto x = linearFun!double(2, 0, 1);
auto x2 = linearFun!double(2, 0, 1);
assert(x.approxEqual(x2));

auto y = linearFun!double(2, 1e-9, 1);
assert(x.approxEqual(y));

auto z = linearFun!double(2, 4, 1);
assert(!x.approxEqual(z));
```