-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
201 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2016-2024 Tilmann Zaeschke | ||
* | ||
* This file is part of TinSpin. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.tinspin.index.util; | ||
|
||
public class MathTools { | ||
|
||
private MathTools() {} | ||
|
||
/** | ||
* Similar to Math.ceil() with the ceiling being the next higher power of 2. | ||
* The resulting number can repeatedly and (almost) always be divided by two without loss of precision. | ||
* @param d input | ||
* @return next power of two above or equal to 'input' | ||
*/ | ||
public static double ceilPowerOfTwo(double d) { | ||
double ceil = floorPowerOfTwo(d); | ||
return ceil == d ? ceil : ceil * 2; | ||
} | ||
|
||
/** | ||
* Similar to Math.floor() with the floor being the next lower power of 2. | ||
* The resulting number can repeatedly and (almost) always be divided by two without loss of precision. | ||
* We calculate the "floor" by setting the "fraction" of the bit representation to 0. | ||
* @param d input | ||
* @return next power of two below or equal to 'input' | ||
*/ | ||
public static double floorPowerOfTwo(double d) { | ||
// Set fraction to "0". | ||
return Double.longBitsToDouble(Double.doubleToRawLongBits(d) & 0xFFF0_0000_0000_0000L); | ||
} | ||
|
||
/** | ||
* Calculates the {@link #floorPowerOfTwo(double)} of an array. | ||
* @param d input vector | ||
* @return copied vector with next lower power of two below 'input' | ||
* @see #floorPowerOfTwo(double) | ||
*/ | ||
public static double[] floorPowerOfTwoCopy(double[] d) { | ||
double[] d2 = new double[d.length]; | ||
for (int i = 0; i < d.length; i++) { | ||
d2[i] = floorPowerOfTwo(d[i]); | ||
} | ||
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 (positive or zero). | ||
*/ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2016-2024 Tilmann Zaeschke | ||
* | ||
* This file is part of TinSpin. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.tinspin.util; | ||
|
||
import static org.junit.Assert.*; | ||
import org.junit.Test; | ||
import org.tinspin.index.util.MathTools; | ||
|
||
import java.util.Arrays; | ||
|
||
public class MathToolsTest { | ||
|
||
@Test | ||
public void powerOfTwoCeil() { | ||
assertEquals(1./32., MathTools.ceilPowerOfTwo(0.03), 0.0); | ||
assertEquals(0.5, MathTools.ceilPowerOfTwo(0.3), 0.0); | ||
assertEquals(4, MathTools.ceilPowerOfTwo(3), 0.0); | ||
assertEquals(32, MathTools.ceilPowerOfTwo(30), 0.0); | ||
assertEquals(512, MathTools.ceilPowerOfTwo(300), 0.0); | ||
|
||
assertEquals(-0.5, MathTools.ceilPowerOfTwo(-0.3), 0.0); | ||
assertEquals(-4, MathTools.ceilPowerOfTwo(-3), 0.0); | ||
assertEquals(-32, MathTools.ceilPowerOfTwo(-30), 0.0); | ||
|
||
// identity | ||
assertEquals(0, MathTools.ceilPowerOfTwo(0), 0.0); | ||
assertEquals(-0.5, MathTools.ceilPowerOfTwo(-0.5), 0.0); | ||
assertEquals(0.5, MathTools.ceilPowerOfTwo(0.5), 0.0); | ||
assertEquals(-1, MathTools.ceilPowerOfTwo(-1), 0.0); | ||
assertEquals(1, MathTools.ceilPowerOfTwo(1), 0.0); | ||
assertEquals(-2, MathTools.ceilPowerOfTwo(-2), 0.0); | ||
assertEquals(2, MathTools.ceilPowerOfTwo(2), 0.0); | ||
} | ||
|
||
@Test | ||
public void powerOfTwoFloor() { | ||
assertEquals(1./64., MathTools.floorPowerOfTwo(0.03), 0.0); | ||
assertEquals(0.25, MathTools.floorPowerOfTwo(0.3), 0.0); | ||
assertEquals(2, MathTools.floorPowerOfTwo(3), 0.0); | ||
assertEquals(16, MathTools.floorPowerOfTwo(30), 0.0); | ||
assertEquals(256, MathTools.floorPowerOfTwo(300), 0.0); | ||
|
||
assertEquals(-0.25, MathTools.floorPowerOfTwo(-0.3), 0.0); | ||
assertEquals(-2, MathTools.floorPowerOfTwo(-3), 0.0); | ||
assertEquals(-16, MathTools.floorPowerOfTwo(-30), 0.0); | ||
|
||
// identity | ||
assertEquals(0, MathTools.ceilPowerOfTwo(0), 0.0); | ||
assertEquals(-0.5, MathTools.ceilPowerOfTwo(-0.5), 0.0); | ||
assertEquals(0.5, MathTools.ceilPowerOfTwo(0.5), 0.0); | ||
assertEquals(-1, MathTools.ceilPowerOfTwo(-1), 0.0); | ||
assertEquals(1, MathTools.ceilPowerOfTwo(1), 0.0); | ||
assertEquals(-2, MathTools.ceilPowerOfTwo(-2), 0.0); | ||
assertEquals(2, MathTools.ceilPowerOfTwo(2), 0.0); | ||
} | ||
|
||
@Test | ||
public void powerOfTwoFloor_vector() { | ||
double[] d = {0.03, 0.3, 3, 30, 300}; | ||
double[] dCopy = MathTools.floorPowerOfTwoCopy(d); | ||
assertFalse(Arrays.equals(d, dCopy)); | ||
assertEquals(1./64., dCopy[0], 0.0); | ||
assertEquals(0.25, dCopy[1], 0.0); | ||
assertEquals(2, dCopy[2], 0.0); | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters