KDTree { public double[] estimateL2DistanceSquared(Datum queryDatum) { RealVector vector = queryDatum.metrics(); double[] estimates = new double[2]; for (int i=0; i<k; i++) { double deltaLo = vector.getEntry(i) - this.boundaries[i][0]; double deltaHi = this.boundaries[i][1] - vector.getEntry(i); double sqDeltaLo = deltaLo * deltaLo; double sqDeltaHi = deltaHi * deltaHi; if (deltaLo < 0 || deltaHi < 0) { if (sqDeltaHi < sqDeltaLo) { estimates[0] += sqDeltaHi; estimates[1] += sqDeltaLo; } else { estimates[0] += sqDeltaLo; estimates[1] += sqDeltaHi; } } else { estimates[1] += Math.max(sqDeltaHi, sqDeltaLo); } } return estimates; } KDTree(List<Datum> data, int leafCapacity); RealVector[] getMinMaxDistanceVectors(Datum queryDatum); double[] estimateL2DistanceSquared(Datum queryDatum); boolean isInsideBoundaries(Datum queryDatum); List<Datum> getItems(); RealVector getMean(); double[][] getBoundaries(); KDTree getLoChild(); KDTree getHiChild(); boolean isLeaf(); int getnBelow(); int getSplitDimension(); String toString(int indent); String toString();  }
@Test public void testEstimateDistanceSquared() throws Exception { this.setUpVerySimpleCsv(); KDTree node; node = new KDTree(data, 2); for (Datum datum : data) { assertEquals(0, node.estimateL2DistanceSquared(datum)[0], 1e-7); } double[] farAwayPoint = {-10, 5, 21}; double[] distancesSquaredFromFarAwayPoint = {265, 857}; Datum datum = new Datum(new ArrayList<>(), new ArrayRealVector(farAwayPoint)); assertArrayEquals(distancesSquaredFromFarAwayPoint, node.estimateL2DistanceSquared(datum), 1e-7); }