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.interpolation.linear

Linear Interpolation

See Also:
Authors:
Ilya Yaroshenko
struct LinearInterpolation(RangeG, RangeV);
Unbounded linear interpolation.
RangeG _grid;
RangeV _values;
size_t interval(T)(in T x);
Interval index for x.
auto opCall(T)(in T x);
(x) and [x] operators.

Complexity: O(log(_grid.length))

auto opCall(T)(in T x, size_t interval);
(x, interval) and [x, interval] operators.

Complexity: O(1)

auto opCall(T)(G x0, G x1, V y0, V y1, in T x);

alias opIndex = opCall;
LinearInterpolation!(RangeG, RangeV) linearInterpolation(RangeG, RangeV)(RangeG grid, RangeV values);
Linear interpolation.
Parameters:
RangeG grid x values for interpolation
RangeV values f(x) values for interpolation

Constraints: grid, values must have the same length >= 3

Examples:
import mir.ndslice;
import std.math: approxEqual;

auto x = [0, 1, 2, 3, 5.00274, 7.00274, 10.0055, 20.0137, 30.0192];
auto y = [0.0011, 0.0011, 0.0030, 0.0064, 0.0144, 0.0207, 0.0261, 0.0329, 0.0356,];
auto xs = [1, 2, 3, 4.00274, 5.00274, 6.00274, 7.00274, 8.00548, 9.00548, 10.0055, 11.0055, 12.0082, 13.0082, 14.0082, 15.0082, 16.011, 17.011, 18.011, 19.011, 20.0137, 21.0137, 22.0137, 23.0137, 24.0164, 25.0164, 26.0164, 27.0164, 28.0192, 29.0192, 30.0192];

auto interpolation = linearInterpolation(x, y);

auto data = [0.0011, 0.0030, 0.0064, 0.0104, 0.0144, 0.0176, 0.0207, 0.0225, 0.0243, 0.0261, 0.0268, 0.0274, 0.0281, 0.0288, 0.0295, 0.0302, 0.0309, 0.0316, 0.0322, 0.0329, 0.0332, 0.0335, 0.0337, 0.0340, 0.0342, 0.0345, 0.0348, 0.0350, 0.0353, 0.0356];

assert(approxEqual(xs.sliced.map!interpolation, data, 1e-4, 1e-4));