License

.

Copyright

Copyright © 2019, Symmetry Investments & Kaleidic Associates Advisory Limited

Authors

Ilya Yaroshenko

See Also:

Example

import mir.algorithm.iteration: all;
import mir.math;
import mir.ndslice;

auto f(double x) { return (x-2) * (x-5) * (x-9); }
auto fd(double x) { return (x-5) * (x-9) + (x-2) * (x-5) + (x-2) * (x-9); }
auto fd2(double x) { return (x-5) + (x-9) + (x-2) + (x-5) + (x-2) + (x-9); }
double[2] fWithDerivative(double x) { return [f(x), fd(x)]; }
double[3] fWithTwoDerivatives(double x) { return [f(x), fd(x), fd2(x)]; }

// 
auto x = [0.0, 3, 5, 10];
auto y = x.map!f.slice.field;
// `!2` adds first two derivatives: f, f', and f''
// default parameter is 0
auto l = x.lagrange!2(y);

foreach(test; x ~ [2.0, 5, 9] ~ [double(PI), double(E), 10, 100, 1e3])
foreach(scale; [-1, +1, 1 + double.epsilon, 1 + double.epsilon.sqrt])
foreach(shift; [0, double.min_normal/2, double.min_normal*2, double.epsilon, double.epsilon.sqrt])
{
    auto testX = test * scale + shift;

    auto lx = l(testX);
    auto l_ldx = l.withDerivative(testX);
    auto l_ld_ld2x = l.withTwoDerivatives(testX);

    assert(lx.approxEqual(f(testX)));
    assert(l_ldx[].all!approxEqual(fWithDerivative(testX)[]));
    assert(l_ld_ld2x[].all!approxEqual(fWithTwoDerivatives(testX)[]));
}

Functions

lagrange
lagrange@trusted
inversedBarycentricWeights
polynomialDerivativeValuesReturns:

Derivative values in the same points

polynomialDerivativeValues

Structs

Lagrange