The OpenD Programming Language

poly

Evaluate polynomial.

Coefficients assumed to be in the order a0 + a1 * x ^^ 1 + ... + aN * x ^^ N

template poly(uint derivative = 0)
typeof(F.init * X.init * 1f + F.init)
poly
(
X
F
)
(
in X x
,
scope const F[] coefficients...
)

Members

Functions

poly
typeof(F.init * X.init * 1f + F.init) poly(X x, F[] coefficients)

Parameters

F

controls type of output

derivative

order of derivatives (default = 0)

Return Value

Value of the polynomial, evaluated at x

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)));

See Also

Meta