@Test public void testRange() { System.out.println("range 0.5"); double[][] data = Matrix.randn(1000, 10).toArray(); KDTree<double[]> kdtree = new KDTree<>(data, data); LinearSearch<double[]> naive = new LinearSearch<>(data, new EuclideanDistance()); List<Neighbor<double[], double[]>> n1 = new ArrayList<>(); List<Neighbor<double[], double[]>> n2 = new ArrayList<>(); for (int i = 0; i < data.length; i++) { kdtree.range(data[i], 0.5, n1); naive.range(data[i], 0.5, n2); Collections.sort(n1); Collections.sort(n2); assertEquals(n1.size(), n2.size()); for (int j = 0; j < n1.size(); j++) { assertEquals(n1.get(j).index, n2.get(j).index); assertEquals(n1.get(j).value, n2.get(j).value); assertEquals(n1.get(j).distance, n2.get(j).distance, 1E-7); } n1.clear(); n2.clear(); } System.out.println("range 1.5"); for (int i = 0; i < data.length; i++) { naive.range(data[i], 1.5, n1); kdtree.range(data[i], 1.5, n2); Collections.sort(n1); Collections.sort(n2); assertEquals(n1.size(), n2.size()); for (int j = 0; j < n1.size(); j++) { assertEquals(n1.get(j).index, n2.get(j).index); assertEquals(n1.get(j).value, n2.get(j).value); assertEquals(n1.get(j).distance, n2.get(j).distance, 1E-7); } n1.clear(); n2.clear(); } }
@Override public void range(double[] q, double radius, List<Neighbor<double[], E>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } search(q, root, radius, neighbors); }
KDTree implements NearestNeighborSearch<double[], E>, KNNSearch<double[], E>, RNNSearch<double[], E>, Serializable { @Override public void range(double[] q, double radius, List<Neighbor<double[], E>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } search(q, root, radius, neighbors); } }
KDTree implements NearestNeighborSearch<double[], E>, KNNSearch<double[], E>, RNNSearch<double[], E>, Serializable { @Override public void range(double[] q, double radius, List<Neighbor<double[], E>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } search(q, root, radius, neighbors); } KDTree(double[][] key, E[] data); }
KDTree implements NearestNeighborSearch<double[], E>, KNNSearch<double[], E>, RNNSearch<double[], E>, Serializable { @Override public void range(double[] q, double radius, List<Neighbor<double[], E>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } search(q, root, radius, neighbors); } 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 void range(double[] q, double radius, List<Neighbor<double[], E>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } search(q, root, radius, neighbors); } 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);  }