@Test public void testInsert() { int elements = 10; List<Double> digits = Arrays.asList(1.0, 0.0, 2.0, 3.0); KDTree kdTree = new KDTree(digits.size()); List<List<Double>> lists = new ArrayList<>(); for (int i = 0; i < elements; i++) { List<Double> thisList = new ArrayList<>(digits.size()); for (int k = 0; k < digits.size(); k++) { thisList.add(digits.get(k) + i); } lists.add(thisList); } for (int i = 0; i < elements; i++) { double[] features = Doubles.toArray(lists.get(i)); INDArray ind = Nd4j.create(features, new long[]{1, features.length}, DataType.FLOAT); kdTree.insert(ind); assertEquals(i + 1, kdTree.size()); } }
public void insert(INDArray point) { if (!point.isVector() || point.length() != dims) throw new IllegalArgumentException("Point must be a vector of length " + dims); if (root == null) { root = new KDNode(point); rect = new HyperRect( point.toFloatVector()); } else { int disc = 0; KDNode node = root; KDNode insert = new KDNode(point); int successor; while (true) { INDArray pt = node.getPoint(); INDArray countEq = Nd4j.getExecutioner().execAndReturn(new Any(pt.neq(point))).z(); if (countEq.getInt(0) == 0) { return; } else { successor = successor(node, point, disc); KDNode child; if (successor < 1) child = node.getLeft(); else child = node.getRight(); if (child == null) break; disc = (disc + 1) % dims; node = child; } } if (successor < 1) node.setLeft(insert); else node.setRight(insert); rect.enlargeTo(point); insert.setParent(node); } size++; }
KDTree implements Serializable { public void insert(INDArray point) { if (!point.isVector() || point.length() != dims) throw new IllegalArgumentException("Point must be a vector of length " + dims); if (root == null) { root = new KDNode(point); rect = new HyperRect( point.toFloatVector()); } else { int disc = 0; KDNode node = root; KDNode insert = new KDNode(point); int successor; while (true) { INDArray pt = node.getPoint(); INDArray countEq = Nd4j.getExecutioner().execAndReturn(new Any(pt.neq(point))).z(); if (countEq.getInt(0) == 0) { return; } else { successor = successor(node, point, disc); KDNode child; if (successor < 1) child = node.getLeft(); else child = node.getRight(); if (child == null) break; disc = (disc + 1) % dims; node = child; } } if (successor < 1) node.setLeft(insert); else node.setRight(insert); rect.enlargeTo(point); insert.setParent(node); } size++; } }
KDTree implements Serializable { public void insert(INDArray point) { if (!point.isVector() || point.length() != dims) throw new IllegalArgumentException("Point must be a vector of length " + dims); if (root == null) { root = new KDNode(point); rect = new HyperRect( point.toFloatVector()); } else { int disc = 0; KDNode node = root; KDNode insert = new KDNode(point); int successor; while (true) { INDArray pt = node.getPoint(); INDArray countEq = Nd4j.getExecutioner().execAndReturn(new Any(pt.neq(point))).z(); if (countEq.getInt(0) == 0) { return; } else { successor = successor(node, point, disc); KDNode child; if (successor < 1) child = node.getLeft(); else child = node.getRight(); if (child == null) break; disc = (disc + 1) % dims; node = child; } } if (successor < 1) node.setLeft(insert); else node.setRight(insert); rect.enlargeTo(point); insert.setParent(node); } size++; } KDTree(int dims); }
KDTree implements Serializable { public void insert(INDArray point) { if (!point.isVector() || point.length() != dims) throw new IllegalArgumentException("Point must be a vector of length " + dims); if (root == null) { root = new KDNode(point); rect = new HyperRect( point.toFloatVector()); } else { int disc = 0; KDNode node = root; KDNode insert = new KDNode(point); int successor; while (true) { INDArray pt = node.getPoint(); INDArray countEq = Nd4j.getExecutioner().execAndReturn(new Any(pt.neq(point))).z(); if (countEq.getInt(0) == 0) { return; } else { successor = successor(node, point, disc); KDNode child; if (successor < 1) child = node.getLeft(); else child = node.getRight(); if (child == null) break; disc = (disc + 1) % dims; node = child; } } if (successor < 1) node.setLeft(insert); else node.setRight(insert); rect.enlargeTo(point); insert.setParent(node); } size++; } KDTree(int dims); void insert(INDArray point); INDArray delete(INDArray point); List<Pair<Float, INDArray>> knn(INDArray point, float distance); Pair<Double, INDArray> nn(INDArray point); int size(); }
KDTree implements Serializable { public void insert(INDArray point) { if (!point.isVector() || point.length() != dims) throw new IllegalArgumentException("Point must be a vector of length " + dims); if (root == null) { root = new KDNode(point); rect = new HyperRect( point.toFloatVector()); } else { int disc = 0; KDNode node = root; KDNode insert = new KDNode(point); int successor; while (true) { INDArray pt = node.getPoint(); INDArray countEq = Nd4j.getExecutioner().execAndReturn(new Any(pt.neq(point))).z(); if (countEq.getInt(0) == 0) { return; } else { successor = successor(node, point, disc); KDNode child; if (successor < 1) child = node.getLeft(); else child = node.getRight(); if (child == null) break; disc = (disc + 1) % dims; node = child; } } if (successor < 1) node.setLeft(insert); else node.setRight(insert); rect.enlargeTo(point); insert.setParent(node); } size++; } KDTree(int dims); void insert(INDArray point); INDArray delete(INDArray point); List<Pair<Float, INDArray>> knn(INDArray point, float distance); Pair<Double, INDArray> nn(INDArray point); int size(); final static int GREATER; final static int LESS; }