Moved array utilities from "MathUtils" to "MathArrays".


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1182137 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-10-11 23:10:46 +00:00
parent 63a3a9d080
commit d64c7a3bfb
5 changed files with 149 additions and 154 deletions

View File

@ -32,6 +32,7 @@ import org.apache.commons.math.stat.descriptive.summary.Sum;
import org.apache.commons.math.stat.descriptive.summary.SumOfLogs; import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
import org.apache.commons.math.stat.descriptive.summary.SumOfSquares; import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
import org.apache.commons.math.util.MathUtils; import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.MathArrays;
import org.apache.commons.math.util.Precision; import org.apache.commons.math.util.Precision;
import org.apache.commons.math.util.FastMath; import org.apache.commons.math.util.FastMath;
@ -373,14 +374,14 @@ public class MultivariateSummaryStatistics
return false; return false;
} }
MultivariateSummaryStatistics stat = (MultivariateSummaryStatistics) object; MultivariateSummaryStatistics stat = (MultivariateSummaryStatistics) object;
return MathUtils.equalsIncludingNaN(stat.getGeometricMean(), getGeometricMean()) && return MathArrays.equalsIncludingNaN(stat.getGeometricMean(), getGeometricMean()) &&
MathUtils.equalsIncludingNaN(stat.getMax(), getMax()) && MathArrays.equalsIncludingNaN(stat.getMax(), getMax()) &&
MathUtils.equalsIncludingNaN(stat.getMean(), getMean()) && MathArrays.equalsIncludingNaN(stat.getMean(), getMean()) &&
MathUtils.equalsIncludingNaN(stat.getMin(), getMin()) && MathArrays.equalsIncludingNaN(stat.getMin(), getMin()) &&
Precision.equalsIncludingNaN(stat.getN(), getN()) && Precision.equalsIncludingNaN(stat.getN(), getN()) &&
MathUtils.equalsIncludingNaN(stat.getSum(), getSum()) && MathArrays.equalsIncludingNaN(stat.getSum(), getSum()) &&
MathUtils.equalsIncludingNaN(stat.getSumSq(), getSumSq()) && MathArrays.equalsIncludingNaN(stat.getSumSq(), getSumSq()) &&
MathUtils.equalsIncludingNaN(stat.getSumLog(), getSumLog()) && MathArrays.equalsIncludingNaN(stat.getSumLog(), getSumLog()) &&
stat.getCovariance().equals( getCovariance()); stat.getCovariance().equals( getCovariance());
} }

View File

@ -922,4 +922,106 @@ public class MathArrays {
return result; return result;
} }
/**
* Returns true iff both arguments are null or have same dimensions and all
* their elements are equal as defined by
* {@link Precision#equals(float,float)}.
*
* @param x first array
* @param y second array
* @return true if the values are both null or have same dimension
* and equal elements.
*/
public static boolean equals(float[] x, float[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; ++i) {
if (!Precision.equals(x[i], y[i])) {
return false;
}
}
return true;
}
/**
* Returns true iff both arguments are null or have same dimensions and all
* their elements are equal as defined by
* {@link Precision#equalsIncludingNaN(double,double) this method}.
*
* @param x first array
* @param y second array
* @return true if the values are both null or have same dimension and
* equal elements
* @since 2.2
*/
public static boolean equalsIncludingNaN(float[] x, float[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; ++i) {
if (!Precision.equalsIncludingNaN(x[i], y[i])) {
return false;
}
}
return true;
}
/**
* Returns {@code true} iff both arguments are {@code null} or have same
* dimensions and all their elements are equal as defined by
* {@link Precision#equals(double,double)}.
*
* @param x First array.
* @param y Second array.
* @return {@code true} if the values are both {@code null} or have same
* dimension and equal elements.
*/
public static boolean equals(double[] x, double[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; ++i) {
if (!Precision.equals(x[i], y[i])) {
return false;
}
}
return true;
}
/**
* Returns {@code true} iff both arguments are {@code null} or have same
* dimensions and all their elements are equal as defined by
* {@link Precision#equalsIncludingNaN(double,double) this method}.
*
* @param x First array.
* @param y Second array.
* @return {@code true} if the values are both {@code null} or have same
* dimension and equal elements.
* @since 2.2
*/
public static boolean equalsIncludingNaN(double[] x, double[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; ++i) {
if (!Precision.equalsIncludingNaN(x[i], y[i])) {
return false;
}
}
return true;
}
} }

View File

@ -383,108 +383,6 @@ public final class MathUtils {
return (FastMath.exp(x) + FastMath.exp(-x)) / 2.0; return (FastMath.exp(x) + FastMath.exp(-x)) / 2.0;
} }
/**
* Returns true iff both arguments are null or have same dimensions and all
* their elements are equal as defined by
* {@link Precision#equals(float,float)}.
*
* @param x first array
* @param y second array
* @return true if the values are both null or have same dimension
* and equal elements.
*/
public static boolean equals(float[] x, float[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; ++i) {
if (!Precision.equals(x[i], y[i])) {
return false;
}
}
return true;
}
/**
* Returns true iff both arguments are null or have same dimensions and all
* their elements are equal as defined by
* {@link Precision#equalsIncludingNaN(double,double) this method}.
*
* @param x first array
* @param y second array
* @return true if the values are both null or have same dimension and
* equal elements
* @since 2.2
*/
public static boolean equalsIncludingNaN(float[] x, float[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; ++i) {
if (!Precision.equalsIncludingNaN(x[i], y[i])) {
return false;
}
}
return true;
}
/**
* Returns {@code true} iff both arguments are {@code null} or have same
* dimensions and all their elements are equal as defined by
* {@link Precision#equals(double,double)}.
*
* @param x First array.
* @param y Second array.
* @return {@code true} if the values are both {@code null} or have same
* dimension and equal elements.
*/
public static boolean equals(double[] x, double[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; ++i) {
if (!Precision.equals(x[i], y[i])) {
return false;
}
}
return true;
}
/**
* Returns {@code true} iff both arguments are {@code null} or have same
* dimensions and all their elements are equal as defined by
* {@link Precision#equalsIncludingNaN(double,double) this method}.
*
* @param x First array.
* @param y Second array.
* @return {@code true} if the values are both {@code null} or have same
* dimension and equal elements.
* @since 2.2
*/
public static boolean equalsIncludingNaN(double[] x, double[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; ++i) {
if (!Precision.equalsIncludingNaN(x[i], y[i])) {
return false;
}
}
return true;
}
/** /**
* Returns n!. Shorthand for {@code n} <a * Returns n!. Shorthand for {@code n} <a
* href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the

View File

@ -547,4 +547,43 @@ public class MathArraysTest {
a[7][3], b[7][3]))); a[7][3], b[7][3])));
Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7], b[7]))); Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7], b[7])));
} }
@Test
public void testArrayEquals() {
Assert.assertFalse(MathArrays.equals(new double[] { 1d }, null));
Assert.assertFalse(MathArrays.equals(null, new double[] { 1d }));
Assert.assertTrue(MathArrays.equals((double[]) null, (double[]) null));
Assert.assertFalse(MathArrays.equals(new double[] { 1d }, new double[0]));
Assert.assertTrue(MathArrays.equals(new double[] { 1d }, new double[] { 1d }));
Assert.assertTrue(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 1d, 0d },
new double[] { Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 1d, 0d }));
Assert.assertFalse(MathArrays.equals(new double[] { Double.NaN },
new double[] { Double.NaN }));
Assert.assertFalse(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY },
new double[] { Double.NEGATIVE_INFINITY }));
Assert.assertFalse(MathArrays.equals(new double[] { 1d },
new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) }));
}
@Test
public void testArrayEqualsIncludingNaN() {
Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, null));
Assert.assertFalse(MathArrays.equalsIncludingNaN(null, new double[] { 1d }));
Assert.assertTrue(MathArrays.equalsIncludingNaN((double[]) null, (double[]) null));
Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[0]));
Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[] { 1d }));
Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] { Double.NaN, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 1d, 0d },
new double[] { Double.NaN, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 1d, 0d }));
Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { Double.POSITIVE_INFINITY },
new double[] { Double.NEGATIVE_INFINITY }));
Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d },
new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) }));
}
} }

View File

@ -318,51 +318,6 @@ public final class MathUtilsTest {
Assert.assertTrue(Double.isNaN(MathUtils.cosh(Double.NaN))); Assert.assertTrue(Double.isNaN(MathUtils.cosh(Double.NaN)));
} }
@Test
public void testArrayEquals() {
Assert.assertFalse(MathUtils.equals(new double[] { 1d }, null));
Assert.assertFalse(MathUtils.equals(null, new double[] { 1d }));
Assert.assertTrue(MathUtils.equals((double[]) null, (double[]) null));
Assert.assertFalse(MathUtils.equals(new double[] { 1d }, new double[0]));
Assert.assertTrue(MathUtils.equals(new double[] { 1d }, new double[] { 1d }));
Assert.assertTrue(MathUtils.equals(new double[] {
Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 1d, 0d
}, new double[] {
Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 1d, 0d
}));
Assert.assertFalse(MathUtils.equals(new double[] { Double.NaN },
new double[] { Double.NaN }));
Assert.assertFalse(MathUtils.equals(new double[] { Double.POSITIVE_INFINITY },
new double[] { Double.NEGATIVE_INFINITY }));
Assert.assertFalse(MathUtils.equals(new double[] { 1d },
new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) }));
}
@Test
public void testArrayEqualsIncludingNaN() {
Assert.assertFalse(MathUtils.equalsIncludingNaN(new double[] { 1d }, null));
Assert.assertFalse(MathUtils.equalsIncludingNaN(null, new double[] { 1d }));
Assert.assertTrue(MathUtils.equalsIncludingNaN((double[]) null, (double[]) null));
Assert.assertFalse(MathUtils.equalsIncludingNaN(new double[] { 1d }, new double[0]));
Assert.assertTrue(MathUtils.equalsIncludingNaN(new double[] { 1d }, new double[] { 1d }));
Assert.assertTrue(MathUtils.equalsIncludingNaN(new double[] {
Double.NaN, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 1d, 0d
}, new double[] {
Double.NaN, Double.POSITIVE_INFINITY,
Double.NEGATIVE_INFINITY, 1d, 0d
}));
Assert.assertFalse(MathUtils.equalsIncludingNaN(new double[] { Double.POSITIVE_INFINITY },
new double[] { Double.NEGATIVE_INFINITY }));
Assert.assertFalse(MathUtils.equalsIncludingNaN(new double[] { 1d },
new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) }));
}
@Test @Test
public void testFactorial() { public void testFactorial() {
for (int i = 1; i < 21; i++) { for (int i = 1; i < 21; i++) {