packages

interpolate::scatter::kdtree

On this page

A k-d tree over points in any dimension (Bentley 1975): median splits on the widest axis found by quickselect, leaf buckets, and branch-and-bound queries that visit the nearer child first and skip a far child once its splitting plane is farther than the current k-th neighbor. Everything sits in GC buffers so batch queries can share the tree across par workers.

Structs

NeighborsSource

pub struct Neighbors

The k nearest neighbors of a point: their row indices into the tree's points and their Euclidean distances, nearest first.

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

let p: Matrix<f64> = [[0, 0], [1, 0], [0, 2]];
let nb = try (try KdTree::new(p)).knn([0.9, 0.1], 2);
assert(nb.indices[0] == 1 && nb.indices[1] == 0);
assert((nb.distances[0] - 0.02.sqrt()).abs() < 1e-15);

KdTreeSource

pub struct KdTree

A k-d tree over the rows of an n x d matrix. Build O(n log n); a query for k neighbors takes O(log n + k) expected time on well-spread points in low dimension (d up to about 10; past that a query tends to visit most leaves).

nabi
use interpolate::scatter::KdTree;
use core::array::{Matrix, Vector};

let p: Matrix<f64> = [[0, 0], [1, 0], [0, 1], [1, 1]];
let t = try KdTree::new(p);
let (i, d) = try t.nearest([0.8, 0.9]);
assert(i == 3 && (d - 0.05.sqrt()).abs() < 1e-15);
assert(try t.radius([0.0, 0.0], 1.0) == Vector::<usize>::from([0, 1, 2]));
  • pub fn new(points: Matrix<f64>) throws (InterpError) -> KdTree§

    Source

    The tree over the rows of points (n x d, d >= 1, all finite).

    nabi
    use interpolate::scatter::KdTree;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[3], [1], [2]];          // d = 1 works too
    assert((try KdTree::new(p)).len() == 3);
    
  • pub fn len(self) -> usize§

    Source

    The number of points.

    nabi
    use interpolate::scatter::KdTree;
    use core::array::Matrix;
    
    let p: Matrix<f64> = Matrix::zeros([0, 2]);
    assert((try KdTree::new(p)).len() == 0);
    
  • pub fn dim(self) -> usize§

    Source

    The dimension d.

    nabi
    use interpolate::scatter::KdTree;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0, 0, 0]];
    assert((try KdTree::new(p)).dim() == 3);
    
  • pub fn knn(self, q: Vector<f64>, k: usize) throws (InterpError) -> Neighbors§

    Source

    The k nearest points to q (fewer when the tree holds fewer), nearest first; ties keep the lower index first.

    nabi
    use interpolate::scatter::KdTree;
    use core::array::{Matrix, Vector};
    
    let p: Matrix<f64> = [[0], [10], [3], [7]];
    let nb = try (try KdTree::new(p)).knn([4.0], 3);
    assert(nb.indices == Vector::<usize>::from([2, 3, 0]));
    
  • pub fn nearest(self, q: Vector<f64>) throws (InterpError) -> (usize, f64)§

    Source

    The nearest point to q: its row and distance. BadArgument on an empty tree.

    nabi
    use interpolate::scatter::KdTree;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0, 0], [5, 5]];
    assert((try (try KdTree::new(p)).nearest([4.0, 4.0])).0 == 1);
    
  • pub fn radius(self, q: Vector<f64>, r: f64) throws (InterpError) -> Vector<usize>§

    Source

    The rows of every point within distance r of q (the boundary included), in rising row order.

    nabi
    use interpolate::scatter::KdTree;
    use core::array::{Matrix, Vector};
    
    let p: Matrix<f64> = [[0], [1], [2], [3]];
    assert(try (try KdTree::new(p)).radius([1.5], 0.5) == Vector::<usize>::from([1, 2]));
    
  • pub fn query(self, qs: Matrix<f64>, k: usize) throws (InterpError) -> (Matrix<usize>, Matrix<f64>)§

    Source

    The k nearest points to every row of qs (m x d): rows of indices and of distances, m x min(k, n), nearest first. Batches of 2048 queries and more run across the workers.

    nabi
    use interpolate::scatter::KdTree;
    use core::array::Matrix;
    
    let p: Matrix<f64> = [[0, 0], [1, 0], [0, 1]];
    let qs: Matrix<f64> = [[0.9, 0], [0, 0.8]];
    let (idx, dist) = try (try KdTree::new(p)).query(qs, 1);
    assert(idx[0, 0] == 1 && idx[1, 0] == 2 && (dist[1, 0] - 0.2).abs() < 1e-15);