Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
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.polynomial

Polynomial ref-counted structure.
License:
Authors:
Ilia Ki
struct Polynomial(F);

Polynomial!F polynomial(F)(RCArray!(const(F)) coefficients);
Polynomial callable ref-counted structure.
Examples:
import mir.test;
import mir.rc.array;
auto a = rcarray!(const double)(3.0, 4.5, 1.9, 2);
auto p = a.polynomial;

alias f = (x) => 3.0 + 4.5 * x^^1 + 1.9 * x^^2 + 2 * x^^3;
alias df = (x) => 4.5 + 2 * 1.9 * x^^1 + 3 * 2 * x^^2;
alias d2f = (x) => 2 * 1.9  + 6 * 2 * x^^1;

p(3.3).shouldApprox == f(3.3);
p(7.2).shouldApprox == f(7.2);

p.opCall!1(3.3).shouldApprox == df(3.3);
p.opCall!1(7.2).shouldApprox == df(7.2);

p.opCall!2(3.3).shouldApprox == d2f(3.3);
p.opCall!2(7.2).shouldApprox == d2f(7.2);
RCArray!(const(F)) coefficients;
this(RCArray!(const(F)) coefficients);
Parameters:
RCArray!(const(F)) coefficients coefficients c[i] for polynomial function f(x)=c[0]+c[1]*x^^1+...+c[n]*x^^n
template opCall(uint derivative = 0)
Parameters:
derivative derivative order
const typeof(F.init * X.init * 1.0F + F.init) opCall(X)(in X x);
Parameters:
X x x point
template poly(uint derivative = 0)
Evaluate polynomial.
Coefficients assumed to be in the order a0 + a1 * x ^^ 1 + ... + aN * x ^^ N
Parameters:
F controls type of output
derivative order of derivatives (default = 0)
Returns:
Value of the polynomial, evaluated at x
See Also:
Examples:
import mir.math.common: approxEqual;

double[] x = [3.0, 4.5, 1.9, 2];

alias   f = (x) =>     3.0 +     4.5 * x^^1 +   1.9 * x^^2 + 2 * x^^3;
alias  df = (x) =>     4.5 + 2 * 1.9 * x^^1 + 3 * 2 * x^^2;
alias d2f = (x) => 2 * 1.9 + 6 *   2 * x^^1;

assert(poly(3.3, x).approxEqual(f(3.3)));
assert(poly(7.2, x).approxEqual(f(7.2)));

assert(poly!1(3.3, x).approxEqual(df(3.3)));
assert(poly!1(7.2, x).approxEqual(df(7.2)));

assert(poly!2(3.3, x).approxEqual(d2f(3.3)));
assert(poly!2(7.2, x).approxEqual(d2f(7.2)));
typeof(F.init * X.init * 1.0F + F.init) poly(X, F)(in X x, scope const F[] coefficients...);
Parameters:
X x value to evaluate
F[] coefficients coefficients of polynomial