interpolate::scatter::kdtree
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 NeighborsThe k nearest neighbors of a point: their row indices into the
tree's points and their Euclidean distances, nearest first.
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 KdTreeA 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).
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]));
Sourcepub fn new(§points: Matrix<f64>) throws (InterpError) -> KdTree The tree over the rows of
points(n x d, d >= 1, all finite).nabiuse 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);The number of points.
nabiuse interpolate::scatter::KdTree; use core::array::Matrix; let p: Matrix<f64> = Matrix::zeros([0, 2]); assert((try KdTree::new(p)).len() == 0);The dimension d.
nabiuse interpolate::scatter::KdTree; use core::array::Matrix; let p: Matrix<f64> = [[0, 0, 0]]; assert((try KdTree::new(p)).dim() == 3);
Sourcepub fn knn(§self, q: Vector<f64>, k: usize) throws (InterpError) -> Neighbors The
knearest points toq(fewer when the tree holds fewer), nearest first; ties keep the lower index first.nabiuse 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]));
Sourcepub fn nearest(§self, q: Vector<f64>) throws (InterpError) -> (usize, f64) The nearest point to
q: its row and distance.BadArgumenton an empty tree.nabiuse 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);
Sourcepub fn radius(§self, q: Vector<f64>, r: f64) throws (InterpError) -> Vector<usize> The rows of every point within distance
rofq(the boundary included), in rising row order.nabiuse 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]));
Sourcepub fn query(§self, qs: Matrix<f64>, k: usize) throws (InterpError) -> (Matrix<usize>, Matrix<f64>) The
knearest points to every row ofqs(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.nabiuse 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);