packages

interpolate::scatter::linear

On this page

Interpolants of scattered data: piecewise linear on the Delaunay triangulation (2-d) and nearest neighbor through a k-d tree (any d).

Structs

LinearNDSource

pub struct LinearND

Piecewise-linear interpolation of 2-d scattered data: inside each Delaunay triangle the plane through its three values (barycentric weights), so linear functions come back exactly (to rounding). Outside the convex hull it gives fill (NaN unless set). A query finds its triangle by a walk that starts at the triangle of the nearest vertex (k-d tree), O(log n) per point.

nabi
use interpolate::scatter::LinearND;
use core::array::Matrix;

let p: Matrix<f64> = [[0, 0], [1, 0], [0, 1], [1, 1]];
let f = try LinearND::new(p, [0, 1, 2, 3]);     // 1 x + 2 y
assert((f([0.5, 0.25]) - 1.0).abs() < 1e-15);
assert(f([2.0, 0.0]).is_nan());
assert(f.fill(0.0).at([2.0, 0.0]) == 0.0);
  • pub fn new(points: Matrix<f64>, values: Vector<f64>) throws (InterpError) -> LinearND§

    Source

    The interpolant of values[i] at row i of points (n x 2).

    nabi
    use interpolate::scatter::LinearND;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0, 0], [2, 0], [0, 2]];
    assert((try LinearND::new(p, [1, 1, 1])).at([0.5, 0.5]) == 1.0);
    
  • pub fn fill(self, v: f64) -> LinearND§

    Source

    This interpolant giving v outside the convex hull.

    nabi
    use interpolate::scatter::LinearND;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0, 0], [1, 0], [0, 1]];
    assert((try LinearND::new(p, [1, 2, 3])).fill(-1.0).at([5.0, 5.0]) == -1.0);
    
  • pub fn triangulation(self) -> Delaunay§

    Source

    The triangulation underneath.

    nabi
    use interpolate::scatter::LinearND;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0, 0], [1, 0], [0, 1]];
    assert((try LinearND::new(p, [1, 2, 3])).triangulation().len() == 1);
    
  • pub fn at(self, p: Vector<f64>) -> f64§

    Source

    The value at p (two coordinates).

    nabi
    use interpolate::scatter::LinearND;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0, 0], [1, 0], [0, 1]];
    assert((try LinearND::new(p, [0, 1, 1])).at([0.5, 0.5]) == 1.0);
    
  • pub fn eval(self, ps: Matrix<f64>) throws (InterpError) -> Vector<f64>§

    Source

    The value at every row of ps (m x 2). Each walk starts from the previous answer, so nearby rows in order cost O(1) each.

    nabi
    use interpolate::scatter::LinearND;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0, 0], [1, 0], [0, 1]];
    let q: Matrix<f64> = [[0.1, 0.1], [0.2, 0.3]];
    let v = try (try LinearND::new(p, [0, 1, 0])).eval(q);
    assert((v[1] - 0.2).abs() < 1e-15);
    
Trait implementations 1
  • impl ops::Call<Vector<f64>> for LinearND

NearestNDSource

pub struct NearestND

Nearest-neighbor interpolation in any dimension: the value of the closest data point (the lowest row among equally close ones), found with a k-d tree.

nabi
use interpolate::scatter::NearestND;
use core::array::Matrix;

let p: Matrix<f64> = [[0, 0, 0], [1, 1, 1]];
let f = try NearestND::new(p, [10, 20]);
assert(f([0.9, 0.8, 0.7]) == 20.0);
  • pub fn new(points: Matrix<f64>, values: Vector<f64>) throws (InterpError) -> NearestND§

    Source

    The interpolant of values[i] at row i of points (n x d, n >= 1).

    nabi
    use interpolate::scatter::NearestND;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0], [1]];
    assert((try NearestND::new(p, [5, 6])).at([0.4]) == 5.0);
    
  • pub fn at(self, p: Vector<f64>) -> f64§

    Source

    The value at p; NaN when p has the wrong length or is not finite.

    nabi
    use interpolate::scatter::NearestND;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0, 0], [4, 0]];
    assert((try NearestND::new(p, [1, 2])).at([3.0, 9.0]) == 2.0);
    
  • pub fn eval(self, ps: Matrix<f64>) throws (InterpError) -> Vector<f64>§

    Source

    The value at every row of ps (m x d), in parallel for large m.

    nabi
    use interpolate::scatter::NearestND;
    use core::array::{Matrix, Vector};
    
    let p: Matrix<f64> = [[0], [1], [2]];
    let q: Matrix<f64> = [[0.2], [1.9]];
    assert(try (try NearestND::new(p, [7, 8, 9])).eval(q) == Vector::<f64>::from([7.0, 9.0]));
    
Trait implementations 1
  • impl ops::Call<Vector<f64>> for NearestND

Enums

MethodSource

pub enum Method

How griddata interpolates.

nabi
use interpolate::scatter::Method;

assert(Method::Linear != Method::Nearest);
  • LinearND: barycentric on the Delaunay triangles (2-d only).

  • Nearest§

    Source

    NearestND: the closest data point (any d).

Functions

griddataSource

pub fn griddata(points: Matrix<f64>, values: Vector<f64>, xi: Matrix<f64>, method: Method = Method::Linear) throws (InterpError) -> Vector<f64>

The data values at points (n x d) interpolated at the rows of xi (m x d), in one call: LinearND (NaN outside the hull) or NearestND.

nabi
use interpolate::scatter::{Method, griddata};
use core::array::Matrix;

let p: Matrix<f64> = [[0, 0], [1, 0], [0, 1], [1, 1]];
let q: Matrix<f64> = [[0.5, 0.5]];
let v = try griddata(p, [0, 1, 1, 2], q, Method::Linear);
assert((v[0] - 1.0).abs() < 1e-15);