Skip to content

Commit

Permalink
Fix root-node precision problems
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaeschke committed Jun 30, 2024
1 parent 9c04601 commit f55e4b8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/main/java/org/tinspin/index/qthypercube2/QuadTreeKD2.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ private void adjustRootSize(double[] key) {
return;
}
if (root.getRadius() == INITIAL_RADIUS) {
double dMax = maxOrthoDistance(key, root.getCenter());
double dMax = MathTools.maxDelta(key, root.getCenter());
for (int i = 0; i < root.getValueCount(); i++) {
dMax = Math.max(dMax, maxOrthoDistance(root.getValues()[i].point(), root.getCenter()));
dMax = Math.max(dMax, MathTools.maxDelta(root.getValues()[i].point(), root.getCenter()));
}
double radius = MathTools.floorPowerOfTwo(dMax) * 2;
if (radius > 0) {
Expand All @@ -142,14 +142,6 @@ private void adjustRootSize(double[] key) {
}
}

private static double maxOrthoDistance(double[] v1, double[] v2) {
double dMax = 0;
for (int i = 0; i < v1.length; i++) {
dMax = Math.max(dMax, Math.abs(v1[i] - v2[i]));
}
return dMax;
}

/**
* Check whether a given key exists.
* @param key the key to check
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/tinspin/index/util/MathTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ public static double[] floorPowerOfTwoCopy(double[] d) {
}
return d2;
}

/**
* Returns the maximal delta between any pair of scalars in the vector.
* @param v1 vector 1
* @param v2 vector 2
* @return maximal delta (always >= 0).
*/
public static double maxDelta(double[] v1, double[] v2) {
double dMax = 0;
for (int i = 0; i < v1.length; i++) {
dMax = Math.max(dMax, Math.abs(v1[i] - v2[i]));
}
return dMax;
}
}
8 changes: 8 additions & 0 deletions src/test/java/org/tinspin/util/MathToolsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ public void powerOfTwoFloor_vector() {
assertEquals(16, dCopy[3], 0.0);
assertEquals(256, dCopy[4], 0.0);
}

@Test
public void maxDelta() {
assertEquals(12, MathTools.maxDelta(new double[]{-4.}, new double[]{8.}), 0.0);
assertEquals(12, MathTools.maxDelta(new double[]{8.}, new double[]{-4.}), 0.0);

assertEquals(4, MathTools.maxDelta(new double[]{2, 4, 2}, new double[]{3, 8, 4}), 0.0);
}
}

0 comments on commit f55e4b8

Please sign in to comment.