MATH-1581: Removed "hash" methods.

This commit is contained in:
Gilles Sadowski 2021-05-25 23:17:11 +02:00
parent ad5f0dac37
commit 35d56cc547
10 changed files with 30 additions and 129 deletions

View File

@ -16,6 +16,7 @@
*/ */
package org.apache.commons.math4.legacy.analysis.differentiation; package org.apache.commons.math4.legacy.analysis.differentiation;
import java.util.Arrays;
import java.io.Serializable; import java.io.Serializable;
import org.apache.commons.numbers.arrays.LinearCombination; import org.apache.commons.numbers.arrays.LinearCombination;
@ -1201,7 +1202,7 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
return 227 + 229 * getFreeParameters() + 233 * getOrder() + 239 * MathUtils.hash(data); return 227 + 229 * getFreeParameters() + 233 * getOrder() + 239 * Arrays.hashCode(data);
} }
/** /**

View File

@ -931,7 +931,7 @@ public class SparseGradient implements RealFieldElement<SparseGradient>, Seriali
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
return 743 + 809 * MathUtils.hash(value) + 167 * derivatives.hashCode(); return 743 + 809 * Double.hashCode(value) + 167 * derivatives.hashCode();
} }
} }

View File

@ -993,7 +993,7 @@ public abstract class AbstractRealMatrix
for (int row = 0; row < nRows; ++row) { for (int row = 0; row < nRows; ++row) {
for (int col = 0; col < nCols; ++col) { for (int col = 0; col < nCols; ++col) {
ret = ret * 31 + (11 * (row+1) + 17 * (col+1)) * ret = ret * 31 + (11 * (row+1) + 17 * (col+1)) *
MathUtils.hash(getEntry(row, col)); Double.hashCode(getEntry(row, col));
} }
} }
return ret; return ret;

View File

@ -832,7 +832,7 @@ public class ArrayRealVector extends RealVector implements Serializable {
if (isNaN()) { if (isNaN()) {
return 9; return 9;
} }
return MathUtils.hash(data); return Arrays.hashCode(data);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */

View File

@ -186,7 +186,7 @@ public abstract class AbstractStorelessUnivariateStatistic
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
return 31 * (31 + MathUtils.hash(getResult())) + MathUtils.hash(getN()); return 31 * (31 + Double.hashCode(getResult())) + Double.hashCode(getN());
} }
} }

View File

@ -402,15 +402,15 @@ public class MultivariateSummaryStatistics
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
int result = 31 + MathUtils.hash(getGeometricMean()); int result = 31 + Arrays.hashCode(getGeometricMean());
result = result * 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + Arrays.hashCode(getGeometricMean());
result = result * 31 + MathUtils.hash(getMax()); result = result * 31 + Arrays.hashCode(getMax());
result = result * 31 + MathUtils.hash(getMean()); result = result * 31 + Arrays.hashCode(getMean());
result = result * 31 + MathUtils.hash(getMin()); result = result * 31 + Arrays.hashCode(getMin());
result = result * 31 + MathUtils.hash(getN()); result = result * 31 + Double.hashCode(getN());
result = result * 31 + MathUtils.hash(getSum()); result = result * 31 + Arrays.hashCode(getSum());
result = result * 31 + MathUtils.hash(getSumSq()); result = result * 31 + Arrays.hashCode(getSumSq());
result = result * 31 + MathUtils.hash(getSumLog()); result = result * 31 + Arrays.hashCode(getSumLog());
result = result * 31 + getCovariance().hashCode(); result = result * 31 + getCovariance().hashCode();
return result; return result;
} }

View File

@ -159,12 +159,12 @@ public class StatisticalSummaryValues implements Serializable,
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
int result = 31 + MathUtils.hash(getMax()); int result = 31 + Double.hashCode(getMax());
result = result * 31 + MathUtils.hash(getMean()); result = result * 31 + Double.hashCode(getMean());
result = result * 31 + MathUtils.hash(getMin()); result = result * 31 + Double.hashCode(getMin());
result = result * 31 + MathUtils.hash(getN()); result = result * 31 + Double.hashCode(getN());
result = result * 31 + MathUtils.hash(getSum()); result = result * 31 + Double.hashCode(getSum());
result = result * 31 + MathUtils.hash(getVariance()); result = result * 31 + Double.hashCode(getVariance());
return result; return result;
} }

View File

@ -412,15 +412,15 @@ public class SummaryStatistics implements StatisticalSummary, Serializable {
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
int result = 31 + MathUtils.hash(getGeometricMean()); int result = 31 + Double.hashCode(getGeometricMean());
result = result * 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + Double.hashCode(getGeometricMean());
result = result * 31 + MathUtils.hash(getMax()); result = result * 31 + Double.hashCode(getMax());
result = result * 31 + MathUtils.hash(getMean()); result = result * 31 + Double.hashCode(getMean());
result = result * 31 + MathUtils.hash(getMin()); result = result * 31 + Double.hashCode(getMin());
result = result * 31 + MathUtils.hash(getN()); result = result * 31 + Double.hashCode(getN());
result = result * 31 + MathUtils.hash(getSum()); result = result * 31 + Double.hashCode(getSum());
result = result * 31 + MathUtils.hash(getSumsq()); result = result * 31 + Double.hashCode(getSumsq());
result = result * 31 + MathUtils.hash(getVariance()); result = result * 31 + Double.hashCode(getVariance());
return result; return result;
} }

View File

@ -51,17 +51,6 @@ public final class MathUtils {
*/ */
private MathUtils() {} private MathUtils() {}
/**
* Returns an integer hash code representing the given double value.
*
* @param value the value to be hashed
* @return the hash code
*/
public static int hash(double value) {
return Double.valueOf(value).hashCode();
}
/** /**
* Returns {@code true} if the values are equal according to semantics of * Returns {@code true} if the values are equal according to semantics of
* {@link Double#equals(Object)}. * {@link Double#equals(Object)}.
@ -74,17 +63,6 @@ public final class MathUtils {
return Double.valueOf(x).equals(Double.valueOf(y)); return Double.valueOf(x).equals(Double.valueOf(y));
} }
/**
* Returns an integer hash code representing the given double array.
*
* @param value the value to be hashed (may be null)
* @return the hash code
* @since 1.2
*/
public static int hash(double[] value) {
return Arrays.hashCode(value);
}
/** Find the maximum of two field elements. /** Find the maximum of two field elements.
* @param <T> the type of the field elements * @param <T> the type of the field elements
* @param e1 first element * @param e1 first element

View File

@ -48,84 +48,6 @@ public final class MathUtilsTest {
Assert.assertFalse(MathUtils.equals(mZero, zero)); Assert.assertFalse(MathUtils.equals(mZero, zero));
} }
@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 }));
}
/**
* Make sure that permuted arrays do not hash to the same value.
*/
@Test
public void testPermutedArrayHash() {
double[] original = new double[10];
double[] permuted = new double[10];
final UniformRandomProvider random = RandomSource.create(RandomSource.WELL_512_A,
64925784252L);
// Generate 10 distinct random values
for (int i = 0; i < 10; i++) {
final ContinuousDistribution.Sampler u
= new UniformContinuousDistribution(i + 0.5, i + 0.75).createSampler(random);
original[i] = u.sample();
}
// Generate a random permutation, making sure it is not the identity
boolean isIdentity = true;
do {
int[] permutation = PermutationSampler.natural(10);
PermutationSampler.shuffle(random, permutation);
for (int i = 0; i < 10; i++) {
if (i != permutation[i]) {
isIdentity = false;
break;
}
permuted[i] = original[permutation[i]];
}
} while (isIdentity);
// Verify that permuted array has different hash
Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
@Test @Test
public void testIndicatorByte() { public void testIndicatorByte() {
Assert.assertEquals((byte)1, MathUtils.copySign((byte)1, (byte)2)); Assert.assertEquals((byte)1, MathUtils.copySign((byte)1, (byte)2));