@Test public void testNearest() { System.out.println("nearest"); double[][] data = Matrix.randn(1000, 10).toArray(); KDTree<double[]> kdtree = new KDTree<>(data, data); LinearSearch<double[]> naive = new LinearSearch<>(data, new EuclideanDistance()); for (int i = 0; i < data.length; i++) { Neighbor<double[], double[]> n1 = naive.nearest(data[i]); Neighbor<double[], double[]> n2 = kdtree.nearest(data[i]); assertEquals(n1.index, n2.index); assertEquals(n1.value, n2.value); assertEquals(n1.distance, n2.distance, 1E-7); } }
@Override public Neighbor<double[], E> nearest(double[] q) { NeighborBuilder<double[], E> neighbor = new NeighborBuilder<>(); search(q, root, neighbor); neighbor.key = keys[neighbor.index]; neighbor.value = data[neighbor.index]; return neighbor.toNeighbor(); }
KDTree implements NearestNeighborSearch<double[], E>, KNNSearch<double[], E>, RNNSearch<double[], E>, Serializable { @Override public Neighbor<double[], E> nearest(double[] q) { NeighborBuilder<double[], E> neighbor = new NeighborBuilder<>(); search(q, root, neighbor); neighbor.key = keys[neighbor.index]; neighbor.value = data[neighbor.index]; return neighbor.toNeighbor(); } }
KDTree implements NearestNeighborSearch<double[], E>, KNNSearch<double[], E>, RNNSearch<double[], E>, Serializable { @Override public Neighbor<double[], E> nearest(double[] q) { NeighborBuilder<double[], E> neighbor = new NeighborBuilder<>(); search(q, root, neighbor); neighbor.key = keys[neighbor.index]; neighbor.value = data[neighbor.index]; return neighbor.toNeighbor(); } KDTree(double[][] key, E[] data); }
KDTree implements NearestNeighborSearch<double[], E>, KNNSearch<double[], E>, RNNSearch<double[], E>, Serializable { @Override public Neighbor<double[], E> nearest(double[] q) { NeighborBuilder<double[], E> neighbor = new NeighborBuilder<>(); search(q, root, neighbor); neighbor.key = keys[neighbor.index]; neighbor.value = data[neighbor.index]; return neighbor.toNeighbor(); } KDTree(double[][] key, E[] data); @Override String toString(); @Override Neighbor<double[], E> nearest(double[] q); @Override @SuppressWarnings("unchecked") Neighbor<double[], E>[] knn(double[] q, int k); @Override void range(double[] q, double radius, List<Neighbor<double[], E>> neighbors); }
KDTree implements NearestNeighborSearch<double[], E>, KNNSearch<double[], E>, RNNSearch<double[], E>, Serializable { @Override public Neighbor<double[], E> nearest(double[] q) { NeighborBuilder<double[], E> neighbor = new NeighborBuilder<>(); search(q, root, neighbor); neighbor.key = keys[neighbor.index]; neighbor.value = data[neighbor.index]; return neighbor.toNeighbor(); } KDTree(double[][] key, E[] data); @Override String toString(); @Override Neighbor<double[], E> nearest(double[] q); @Override @SuppressWarnings("unchecked") Neighbor<double[], E>[] knn(double[] q, int k); @Override void range(double[] q, double radius, List<Neighbor<double[], E>> neighbors);  }