MathUtils { public static int hash(double value) { return new Double(value).hashCode(); } private  MathUtils(); static int hash(double value); static boolean equals(double x, double y); static int hash(double[] value); static double normalizeAngle(double a, double center); static double reduce(double a,
                                double period,
                                double offset); static byte copySign(byte magnitude, byte sign); static short copySign(short magnitude, short sign); static int copySign(int magnitude, int sign); static long copySign(long magnitude, long sign); static void checkFinite(final double x); static void checkFinite(final double[] val); static void checkNotNull(Object o,
                                    Localizable pattern,
                                    Object ... args); static void checkNotNull(Object o); static final double TWO_PI; static final double PI_SQUARED; }
@Test public void testHash() { double[] testArray = { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d, 1E-14, (1 + 1E-14), Double.MIN_VALUE, Double.MAX_VALUE }; for (int i = 0; i < testArray.length; i++) { for (int j = 0; j < testArray.length; j++) { if (i == j) { Assert.assertEquals(MathUtils.hash(testArray[i]), MathUtils.hash(testArray[j])); Assert.assertEquals(MathUtils.hash(testArray[j]), MathUtils.hash(testArray[i])); } else { Assert.assertTrue(MathUtils.hash(testArray[i]) != MathUtils.hash(testArray[j])); Assert.assertTrue(MathUtils.hash(testArray[j]) != MathUtils.hash(testArray[i])); } } } }
@Test public void testArrayHash() { Assert.assertEquals(0, MathUtils.hash((double[]) null)); Assert.assertEquals(MathUtils.hash(new double[] { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d }), MathUtils.hash(new double[] { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d })); Assert.assertFalse(MathUtils.hash(new double[] { 1d }) == MathUtils.hash(new double[] { FastMath.nextAfter(1d, 2d) })); Assert.assertFalse(MathUtils.hash(new double[] { 1d }) == MathUtils.hash(new double[] { 1d, 1d })); }
@Test public void testPermutedArrayHash() { double[] original = new double[10]; double[] permuted = new double[10]; RandomDataGenerator random = new RandomDataGenerator(); for (int i = 0; i < 10; i++) { final RealDistribution u = new UniformRealDistribution(i + 0.5, i + 0.75); original[i] = u.sample(); } boolean isIdentity = true; do { int[] permutation = random.nextPermutation(10, 10); for (int i = 0; i < 10; i++) { if (i != permutation[i]) { isIdentity = false; } permuted[i] = original[permutation[i]]; } } while (isIdentity); Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted)); }