[MATH-1059] Use FastMath instead of Math.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1547633 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-12-03 23:03:06 +00:00
parent b12610d35e
commit ba3c2201c6
35 changed files with 138 additions and 123 deletions

View File

@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</properties>
<body>
<release version="3.3" date="TBD" description="TBD">
<action dev="tn" type="fix" issue="MATH-1059">
Use "FastMath" instead of "Math" within Commons Math.
</action>
<action dev="tn" type="fix" issue="MATH-1068" due-to="Gal Lalouche">
Avoid overflow when calculating Kendall's correlation for large arrays.
</action>

View File

@ -168,7 +168,7 @@ public class Logistic implements UnivariateDifferentiableFunction, Differentiabl
final double gb = factor2 * mMinusX * qExp;
final double gq = factor2 * exp;
final double ga = Logistic.value(mMinusX, 0, b, q, 1, oneOverN);
final double gn = factor1 * Math.log(qExp1) * oneOverN;
final double gn = factor1 * FastMath.log(qExp1) * oneOverN;
return new double[] { gk, gm, gb, gq, ga, gn };
}

View File

@ -130,12 +130,12 @@ public abstract class AbstractIntegerDistribution implements IntegerDistribution
double k = FastMath.sqrt((1.0 - p) / p);
double tmp = mu - k * sigma;
if (tmp > lower) {
lower = ((int) Math.ceil(tmp)) - 1;
lower = ((int) FastMath.ceil(tmp)) - 1;
}
k = 1.0 / k;
tmp = mu + k * sigma;
if (tmp < upper) {
upper = ((int) Math.ceil(tmp)) - 1;
upper = ((int) FastMath.ceil(tmp)) - 1;
}
}

View File

@ -31,6 +31,7 @@ import org.apache.commons.math3.linear.Array2DRowFieldMatrix;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.FieldMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.util.FastMath;
/**
* Implementation of the Kolmogorov-Smirnov distribution.
@ -169,7 +170,7 @@ public class KolmogorovSmirnovDistribution implements Serializable {
} else if (1 - ninv <= d && d < 1) {
return 1 - 2 * Math.pow(1 - d, n);
return 1 - 2 * FastMath.pow(1 - d, n);
} else if (1 <= d) {
@ -193,7 +194,7 @@ public class KolmogorovSmirnovDistribution implements Serializable {
*/
private double exactK(double d) throws MathArithmeticException {
final int k = (int) Math.ceil(n * d);
final int k = (int) FastMath.ceil(n * d);
final FieldMatrix<BigFraction> H = this.createH(d);
final FieldMatrix<BigFraction> Hpower = H.power(n);
@ -225,7 +226,7 @@ public class KolmogorovSmirnovDistribution implements Serializable {
*/
private double roundedK(double d) throws MathArithmeticException {
final int k = (int) Math.ceil(n * d);
final int k = (int) FastMath.ceil(n * d);
final FieldMatrix<BigFraction> HBigFraction = this.createH(d);
final int m = HBigFraction.getRowDimension();
@ -266,7 +267,7 @@ public class KolmogorovSmirnovDistribution implements Serializable {
private FieldMatrix<BigFraction> createH(double d)
throws NumberIsTooLargeException, FractionConversionException {
int k = (int) Math.ceil(n * d);
int k = (int) FastMath.ceil(n * d);
int m = 2 * k - 1;
double hDouble = k - n * d;

View File

@ -19,6 +19,7 @@ package org.apache.commons.math3.distribution.fitting;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.math3.distribution.MultivariateNormalDistribution;
import org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution;
import org.apache.commons.math3.exception.ConvergenceException;
@ -31,6 +32,7 @@ import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.linear.SingularMatrixException;
import org.apache.commons.math3.stat.correlation.Covariance;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.MathArrays;
import org.apache.commons.math3.util.Pair;
@ -165,7 +167,7 @@ public class MultivariateNormalMixtureExpectationMaximization {
fittedModel = new MixtureMultivariateNormalDistribution(initialMixture.getComponents());
while (numIterations++ <= maxIterations &&
Math.abs(previousLogLikelihood - logLikelihood) > threshold) {
FastMath.abs(previousLogLikelihood - logLikelihood) > threshold) {
previousLogLikelihood = logLikelihood;
double sumLogLikelihood = 0d;
@ -197,7 +199,7 @@ public class MultivariateNormalMixtureExpectationMaximization {
for (int i = 0; i < n; i++) {
final double rowDensity = fittedModel.density(data[i]);
sumLogLikelihood += Math.log(rowDensity);
sumLogLikelihood += FastMath.log(rowDensity);
for (int j = 0; j < k; j++) {
gamma[i][j] = weights[j] * mvns[j].density(data[i]) / rowDensity;
@ -251,7 +253,7 @@ public class MultivariateNormalMixtureExpectationMaximization {
newCovMatArrays);
}
if (Math.abs(previousLogLikelihood - logLikelihood) > threshold) {
if (FastMath.abs(previousLogLikelihood - logLikelihood) > threshold) {
// Did not converge before the maximum number of iterations
throw new ConvergenceException();
}

View File

@ -689,8 +689,8 @@ public class BigFraction
if (Double.isNaN(result)) {
// Numerator and/or denominator must be out of range:
// Calculate how far to shift them to put them in range.
int shift = Math.max(numerator.bitLength(),
denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
int shift = FastMath.max(numerator.bitLength(),
denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
result = numerator.shiftRight(shift).doubleValue() /
denominator.shiftRight(shift).doubleValue();
}
@ -742,8 +742,8 @@ public class BigFraction
if (Double.isNaN(result)) {
// Numerator and/or denominator must be out of range:
// Calculate how far to shift them to put them in range.
int shift = Math.max(numerator.bitLength(),
denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
int shift = FastMath.max(numerator.bitLength(),
denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
result = numerator.shiftRight(shift).floatValue() /
denominator.shiftRight(shift).floatValue();
}

View File

@ -22,6 +22,7 @@ import java.text.ParsePosition;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.MathUtils;
/**
@ -101,12 +102,11 @@ public class ProperFractionFormat extends FractionFormat {
if (whole != 0) {
getWholeFormat().format(whole, toAppendTo, pos);
toAppendTo.append(' ');
num = Math.abs(num);
num = FastMath.abs(num);
}
getNumeratorFormat().format(num, toAppendTo, pos);
toAppendTo.append(" / ");
getDenominatorFormat().format(den, toAppendTo,
pos);
getDenominatorFormat().format(den, toAppendTo, pos);
return toAppendTo;
}
@ -215,7 +215,7 @@ public class ProperFractionFormat extends FractionFormat {
int w = whole.intValue();
int n = num.intValue();
int d = den.intValue();
return new Fraction(((Math.abs(w) * d) + n) * MathUtils.copySign(1, w), d);
return new Fraction(((FastMath.abs(w) * d) + n) * MathUtils.copySign(1, w), d);
}
/**

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.math3.optim.univariate;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.Incrementor;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.TooManyEvaluationsException;
@ -136,7 +137,7 @@ public class BracketFinder {
double tmp2 = (xB - xC) * (fB - fA);
double val = tmp2 - tmp1;
double denom = Math.abs(val) < EPS_MIN ? 2 * EPS_MIN : 2 * val;
double denom = FastMath.abs(val) < EPS_MIN ? 2 * EPS_MIN : 2 * val;
double w = xB - ((xB - xC) * tmp2 - (xB - xA) * tmp1) / denom;
double wLim = xB + growLimit * (xC - xB);

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.math3.optimization.univariate;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.Incrementor;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.TooManyEvaluationsException;
@ -138,7 +139,7 @@ public class BracketFinder {
double tmp2 = (xB - xC) * (fB - fA);
double val = tmp2 - tmp1;
double denom = Math.abs(val) < EPS_MIN ? 2 * EPS_MIN : 2 * val;
double denom = FastMath.abs(val) < EPS_MIN ? 2 * EPS_MIN : 2 * val;
double w = xB - ((xB - xC) * tmp2 - (xB - xA) * tmp1) / denom;
double wLim = xB + growLimit * (xC - xB);

View File

@ -18,6 +18,8 @@ package org.apache.commons.math3.random;
import java.io.Serializable;
import org.apache.commons.math3.util.FastMath;
/** This abstract class implements the WELL class of pseudo-random number generator
* from Fran&ccedil;ois Panneton, Pierre L'Ecuyer and Makoto Matsumoto.
@ -156,7 +158,7 @@ public abstract class AbstractWell extends BitsStreamGenerator implements Serial
return;
}
System.arraycopy(seed, 0, v, 0, Math.min(seed.length, v.length));
System.arraycopy(seed, 0, v, 0, FastMath.min(seed.length, v.length));
if (seed.length < v.length) {
for (int i = seed.length; i < v.length; ++i) {

View File

@ -19,6 +19,8 @@ package org.apache.commons.math3.random;
import java.io.Serializable;
import org.apache.commons.math3.util.FastMath;
/**
* <a href="http://burtleburtle.net/bob/rand/isaacafa.html">
* ISAAC: a fast cryptographic pseudo-random number generator</a>
@ -124,7 +126,7 @@ public class ISAACRandom extends BitsStreamGenerator implements Serializable {
}
final int seedLen = seed.length;
final int rslLen = rsl.length;
System.arraycopy(seed, 0, rsl, 0, Math.min(seedLen, rslLen));
System.arraycopy(seed, 0, rsl, 0, FastMath.min(seedLen, rslLen));
if (seedLen < rslLen) {
for (int j = seedLen; j < rslLen; j++) {
long k = rsl[j - seedLen];

View File

@ -95,7 +95,7 @@ public class GTest {
}
double ratio = 1d;
boolean rescale = false;
if (Math.abs(sumExpected - sumObserved) > 10E-6) {
if (FastMath.abs(sumExpected - sumObserved) > 10E-6) {
ratio = sumObserved / sumExpected;
rescale = true;
}
@ -270,7 +270,7 @@ public class GTest {
for (int j = 0; j < k[i].length; j++) {
if (k[i][j] != 0) {
final double p_ij = (double) k[i][j] / sum_k;
h += p_ij * Math.log(p_ij);
h += p_ij * FastMath.log(p_ij);
}
}
}
@ -297,7 +297,7 @@ public class GTest {
for (int i = 0; i < k.length; i++) {
if (k[i] != 0) {
final double p_i = (double) k[i] / sum_k;
h += p_i * Math.log(p_i);
h += p_i * FastMath.log(p_i);
}
}
return -h;

View File

@ -321,7 +321,7 @@ public abstract class AbstractMultipleLinearRegression implements
* @since 2.2
*/
public double estimateRegressionStandardError() {
return Math.sqrt(estimateErrorVariance());
return FastMath.sqrt(estimateErrorVariance());
}
/**

View File

@ -342,14 +342,14 @@ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegressio
double total;
final double eps = this.epsilon;
for (int i = 0; i < nvars; i++) {
this.work_tolset[i] = Math.sqrt(d[i]);
this.work_tolset[i] = FastMath.sqrt(d[i]);
}
tol[0] = eps * this.work_tolset[0];
for (int col = 1; col < nvars; col++) {
pos = col - 1;
total = work_tolset[col];
for (int row = 0; row < col; row++) {
total += Math.abs(r[pos]) * work_tolset[row];
total += FastMath.abs(r[pos]) * work_tolset[row];
pos += nvars - row - 2;
}
tol[col] = eps * total;
@ -383,7 +383,7 @@ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegressio
final double[] ret = new double[nreq];
boolean rankProblem = false;
for (int i = nreq - 1; i > -1; i--) {
if (Math.sqrt(d[i]) < tol[i]) {
if (FastMath.sqrt(d[i]) < tol[i]) {
ret[i] = 0.0;
d[i] = 0.0;
rankProblem = true;
@ -413,7 +413,7 @@ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegressio
private void singcheck() {
int pos;
for (int i = 0; i < nvars; i++) {
work_sing[i] = Math.sqrt(d[i]);
work_sing[i] = FastMath.sqrt(d[i]);
}
for (int col = 0; col < nvars; col++) {
// Set elements within R to zero if they are less than tol(col) in
@ -422,7 +422,7 @@ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegressio
final double temp = tol[col];
pos = col - 1;
for (int row = 0; row < col - 1; row++) {
if (Math.abs(r[pos]) * work_sing[row] < temp) {
if (FastMath.abs(r[pos]) * work_sing[row] < temp) {
r[pos] = 0.0;
}
pos += nvars - row - 2;
@ -625,7 +625,7 @@ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegressio
final int nvm = nvars - 1;
final int base_pos = r.length - (nvm - in) * (nvm - in + 1) / 2;
if (d[in] > 0.0) {
rms[in + rms_off] = 1.0 / Math.sqrt(d[in]);
rms[in + rms_off] = 1.0 / FastMath.sqrt(d[in]);
}
for (int col = in + 1; col < nvars; col++) {
pos = base_pos + col - 1 - in;
@ -635,7 +635,7 @@ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegressio
pos += nvars - row - 2;
}
if (sumxx > 0.0) {
rms[col + rms_off] = 1.0 / Math.sqrt(sumxx);
rms[col + rms_off] = 1.0 / FastMath.sqrt(sumxx);
} else {
rms[col + rms_off] = 0.0;
}
@ -645,7 +645,7 @@ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegressio
sumyy += d[row] * rhs[row] * rhs[row];
}
if (sumyy > 0.0) {
sumyy = 1.0 / Math.sqrt(sumyy);
sumyy = 1.0 / FastMath.sqrt(sumyy);
}
pos = 0;
for (int col1 = in; col1 < nvars; col1++) {
@ -729,10 +729,10 @@ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegressio
// Special cases.
if (d1 > this.epsilon || d2 > this.epsilon) {
X = r[m1];
if (Math.abs(X) * Math.sqrt(d1) < tol[mp1]) {
if (FastMath.abs(X) * FastMath.sqrt(d1) < tol[mp1]) {
X = 0.0;
}
if (d1 < this.epsilon || Math.abs(X) < this.epsilon) {
if (d1 < this.epsilon || FastMath.abs(X) < this.epsilon) {
d[m] = d2;
d[mp1] = d1;
r[m1] = 0.0;
@ -868,7 +868,7 @@ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegressio
}
double hii = 0.0;
for (int col = 0; col < xrow.length; col++) {
if (Math.sqrt(d[col]) < tol[col]) {
if (FastMath.sqrt(d[col]) < tol[col]) {
wk[col] = 0.0;
} else {
pos = col - 1;

View File

@ -342,7 +342,7 @@ public final class ArithmeticUtils {
a >>= aTwos;
final int bTwos = Integer.numberOfTrailingZeros(b);
b >>= bTwos;
final int shift = Math.min(aTwos, bTwos);
final int shift = FastMath.min(aTwos, bTwos);
// "a" and "b" are positive.
// If a > b then "gdc(a, b)" is equal to "gcd(a - b, b)".

View File

@ -594,7 +594,7 @@ public class MathArrays {
double floatn = v.length;
double agiant = rgiant / floatn;
for (int i = 0; i < v.length; i++) {
double xabs = Math.abs(v[i]);
double xabs = FastMath.abs(v[i]);
if (xabs < rdwarf || xabs > agiant) {
if (xabs > rdwarf) {
if (xabs > x1max) {

View File

@ -30,7 +30,7 @@ public class SqrtTest {
final Sqrt s = new Sqrt();
final UnivariateFunction f = new UnivariateFunction() {
public double value(double x) {
return Math.sqrt(x);
return FastMath.sqrt(x);
}
};
@ -46,7 +46,7 @@ public class SqrtTest {
final UnivariateDifferentiableFunction sPrime = new Sqrt();
final UnivariateFunction f = new UnivariateFunction() {
public double value(double x) {
return 1 / (2 * Math.sqrt(x));
return 1 / (2 * FastMath.sqrt(x));
}
};

View File

@ -17,6 +17,7 @@
package org.apache.commons.math3.analysis.integration.gauss;
import org.apache.commons.math3.analysis.function.Power;
import org.apache.commons.math3.util.FastMath;
import org.junit.Test;
import org.junit.Assert;
@ -105,7 +106,7 @@ public abstract class GaussianQuadratureAbstractTest {
integrator.getNumberOfPoints() + "-point quadrature rule",
expected, actual, eps);
} else {
double err = Math.abs(actual - expected) / Math.ulp(expected);
double err = FastMath.abs(actual - expected) / Math.ulp(expected);
Assert.assertEquals("while integrating monomial x**" + n + " with a " +
+ integrator.getNumberOfPoints() + "-point quadrature rule, " +
" error was " + err + " ulps",

View File

@ -19,6 +19,7 @@ package org.apache.commons.math3.analysis.solvers;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.exception.ConvergenceException;
import org.apache.commons.math3.util.FastMath;
import org.junit.Test;
import org.junit.Assert;
@ -48,7 +49,7 @@ public final class RegulaFalsiSolverTest extends BaseSecantSolverAbstractTest {
final UnivariateFunction f = new UnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
return Math.exp(x) - Math.pow(Math.PI, 3.0);
return FastMath.exp(x) - FastMath.pow(Math.PI, 3.0);
}
};

View File

@ -291,10 +291,10 @@ public class QuaternionTest {
final double norm = q.getNorm();
Assert.assertEquals(Math.sqrt(30), norm, 0);
Assert.assertEquals(FastMath.sqrt(30), norm, 0);
final double normSquareRef = Quaternion.multiply(q, q.getConjugate()).getScalarPart();
Assert.assertEquals(Math.sqrt(normSquareRef), norm, 0);
Assert.assertEquals(FastMath.sqrt(normSquareRef), norm, 0);
}
@Test

View File

@ -369,8 +369,8 @@ public class LevenbergMarquardtOptimizerTest
for (int i = 0; i < values.length; ++i) {
final double t = time.get(i);
values[i] = params[0] +
params[1] * Math.exp(-t / params[3]) +
params[2] * Math.exp(-t / params[4]);
params[1] * FastMath.exp(-t / params[3]) +
params[2] * FastMath.exp(-t / params[4]);
}
return values;
}
@ -390,10 +390,10 @@ public class LevenbergMarquardtOptimizerTest
final double p4 = params[4];
final double tOp3 = t / p3;
final double tOp4 = t / p4;
jacobian[i][1] = Math.exp(-tOp3);
jacobian[i][2] = Math.exp(-tOp4);
jacobian[i][3] = params[1] * Math.exp(-tOp3) * tOp3 / p3;
jacobian[i][4] = params[2] * Math.exp(-tOp4) * tOp4 / p4;
jacobian[i][1] = FastMath.exp(-tOp3);
jacobian[i][2] = FastMath.exp(-tOp4);
jacobian[i][3] = params[1] * FastMath.exp(-tOp3) * tOp3 / p3;
jacobian[i][4] = params[2] * FastMath.exp(-tOp4) * tOp4 / p4;
}
return jacobian;
}

View File

@ -91,7 +91,7 @@ public class ConjugateGradientTest {
for (int i = 0; i < n; i++) {
final double actual = x.getEntry(i);
final double expected = ainv.getEntry(i, j);
final double delta = 1E-10 * Math.abs(expected);
final double delta = 1E-10 * FastMath.abs(expected);
final String msg = String.format("entry[%d][%d]", i, j);
Assert.assertEquals(msg, expected, actual, delta);
}
@ -117,7 +117,7 @@ public class ConjugateGradientTest {
for (int i = 0; i < n; i++) {
final double actual = x.getEntry(i);
final double expected = ainv.getEntry(i, j);
final double delta = 1E-10 * Math.abs(expected);
final double delta = 1E-10 * FastMath.abs(expected);
final String msg = String.format("entry[%d][%d)", i, j);
Assert.assertEquals(msg, expected, actual, delta);
}
@ -143,7 +143,7 @@ public class ConjugateGradientTest {
for (int i = 0; i < n; i++) {
final double actual = x.getEntry(i);
final double expected = ainv.getEntry(i, j);
final double delta = 1E-10 * Math.abs(expected);
final double delta = 1E-10 * FastMath.abs(expected);
final String msg = String.format("entry[%d][%d]", i, j);
Assert.assertEquals(msg, expected, actual, delta);
Assert.assertEquals(msg, x0.getEntry(i), 1., Math.ulp(1.));
@ -204,7 +204,7 @@ public class ConjugateGradientTest {
for (int i = 0; i < n; i++) {
final double actual = b.getEntry(i) - y.getEntry(i);
final double expected = r.getEntry(i);
final double delta = 1E-6 * Math.abs(expected);
final double delta = 1E-6 * FastMath.abs(expected);
final String msg = String
.format("column %d, residual %d", i, j);
Assert.assertEquals(msg, expected, actual, delta);
@ -320,7 +320,7 @@ public class ConjugateGradientTest {
for (int i = 0; i < n; i++) {
final double actual = x.getEntry(i);
final double expected = ainv.getEntry(i, j);
final double delta = 1E-6 * Math.abs(expected);
final double delta = 1E-6 * FastMath.abs(expected);
final String msg = String.format("coefficient (%d, %d)", i, j);
Assert.assertEquals(msg, expected, actual, delta);
}
@ -376,15 +376,12 @@ public class ConjugateGradientTest {
for (int i = 0; i < n; i++) {
final double actual = b.getEntry(i) - y.getEntry(i);
final double expected = r.getEntry(i);
final double delta = 1E-6 * Math.abs(expected);
final String msg = String
.format("column %d, residual %d", i, j);
final double delta = 1E-6 * FastMath.abs(expected);
final String msg = String.format("column %d, residual %d", i, j);
Assert.assertEquals(msg, expected, actual, delta);
}
}
Assert
.assertTrue("MaxCountExceededException should have been caught",
caught);
Assert.assertTrue("MaxCountExceededException should have been caught", caught);
}
}
@ -428,7 +425,7 @@ public class ConjugateGradientTest {
msg = String.format("row %d, column %d", i, j);
final double expected = x.getEntry(i);
final double actual = px.getEntry(i);
final double delta = 1E-6 * Math.abs(expected);
final double delta = 1E-6 * FastMath.abs(expected);
Assert.assertEquals(msg, expected, actual, delta);
}
}

View File

@ -257,7 +257,7 @@ public class SymmLQTest {
for (int i = 0; i < n; i++) {
final double actual = x.getEntry(i);
final double expected = ainv.getEntry(i, j);
final double delta = 1E-6 * Math.abs(expected);
final double delta = 1E-6 * FastMath.abs(expected);
final String msg = String.format("entry[%d][%d]", i, j);
Assert.assertEquals(msg, expected, actual, delta);
}
@ -283,7 +283,7 @@ public class SymmLQTest {
for (int i = 0; i < n; i++) {
final double actual = x.getEntry(i);
final double expected = ainv.getEntry(i, j);
final double delta = 1E-6 * Math.abs(expected);
final double delta = 1E-6 * FastMath.abs(expected);
final String msg = String.format("entry[%d][%d)", i, j);
Assert.assertEquals(msg, expected, actual, delta);
}
@ -309,7 +309,7 @@ public class SymmLQTest {
for (int i = 0; i < n; i++) {
final double actual = x.getEntry(i);
final double expected = ainv.getEntry(i, j);
final double delta = 1E-6 * Math.abs(expected);
final double delta = 1E-6 * FastMath.abs(expected);
final String msg = String.format("entry[%d][%d]", i, j);
Assert.assertEquals(msg, expected, actual, delta);
Assert.assertEquals(msg, x0.getEntry(i), 1., Math.ulp(1.));
@ -421,7 +421,7 @@ public class SymmLQTest {
for (int i = 0; i < n; i++) {
final double actual = x.getEntry(i);
final double expected = ainv.getEntry(i, j);
final double delta = 1E-6 * Math.abs(expected);
final double delta = 1E-6 * FastMath.abs(expected);
final String msg = String.format("coefficient (%d, %d)", i, j);
Assert.assertEquals(msg, expected, actual, delta);
}
@ -467,7 +467,7 @@ public class SymmLQTest {
msg = String.format("row %d, column %d", i, j);
final double expected = x.getEntry(i);
final double actual = px.getEntry(i);
final double delta = 5E-5 * Math.abs(expected);
final double delta = 5E-5 * FastMath.abs(expected);
Assert.assertEquals(msg, expected, actual, delta);
}
}

View File

@ -27,6 +27,7 @@ import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.function.Sin;
import org.apache.commons.math3.exception.MathUnsupportedOperationException;
import org.apache.commons.math3.linear.RealVector.Entry;
import org.apache.commons.math3.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
@ -85,12 +86,12 @@ public abstract class UnmodifiableRealVectorAbstractTest {
public static boolean equals(final double x, final double y) {
if (x == y) {
return true;
} else if (Math.abs(x) <= EPS) {
return Math.abs(y) <= EPS;
} else if (Math.abs(y) <= EPS) {
return Math.abs(x) <= EPS;
} else if (FastMath.abs(x) <= EPS) {
return FastMath.abs(y) <= EPS;
} else if (FastMath.abs(y) <= EPS) {
return FastMath.abs(x) <= EPS;
} else {
return Math.abs(x - y) <= EPS * Math.min(Math.abs(x), Math.abs(y));
return FastMath.abs(x - y) <= EPS * FastMath.min(FastMath.abs(x), FastMath.abs(y));
}
}

View File

@ -378,8 +378,8 @@ public class LevenbergMarquardtOptimizerTest
for (int i = 0; i < values.length; ++i) {
final double t = time.get(i);
values[i] = params[0] +
params[1] * Math.exp(-t / params[3]) +
params[2] * Math.exp(-t / params[4]);
params[1] * FastMath.exp(-t / params[3]) +
params[2] * FastMath.exp(-t / params[4]);
}
return values;
}
@ -399,10 +399,10 @@ public class LevenbergMarquardtOptimizerTest
final double p4 = params[4];
final double tOp3 = t / p3;
final double tOp4 = t / p4;
jacobian[i][1] = Math.exp(-tOp3);
jacobian[i][2] = Math.exp(-tOp4);
jacobian[i][3] = params[1] * Math.exp(-tOp3) * tOp3 / p3;
jacobian[i][4] = params[2] * Math.exp(-tOp4) * tOp4 / p4;
jacobian[i][1] = FastMath.exp(-tOp3);
jacobian[i][2] = FastMath.exp(-tOp4);
jacobian[i][3] = params[1] * FastMath.exp(-tOp3) * tOp3 / p3;
jacobian[i][4] = params[2] * FastMath.exp(-tOp4) * tOp4 / p4;
}
return jacobian;
}

View File

@ -364,8 +364,8 @@ public class LevenbergMarquardtOptimizerTest extends AbstractLeastSquaresOptimiz
for (int i = 0; i < values.length; ++i) {
final double t = time.get(i);
values[i] = params[0]
+ params[1] * Math.exp(-t / params[3])
+ params[2] * Math.exp(-t / params[4]);
+ params[1] * FastMath.exp(-t / params[3])
+ params[2] * FastMath.exp(-t / params[4]);
}
return values;
}

View File

@ -36,6 +36,7 @@ import org.apache.commons.math3.distribution.UniformRealDistribution;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
import org.apache.commons.math3.util.FastMath;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -413,10 +414,10 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes
*/
private int findBin(double x) {
// Number of bins below x should be trunc(x/10)
final double nMinus = Math.floor(x / 10);
final int bin = (int) Math.round(nMinus);
final double nMinus = FastMath.floor(x / 10);
final int bin = (int) FastMath.round(nMinus);
// If x falls on a bin boundary, it is in the lower bin
return Math.floor(x / 10) == x / 10 ? bin - 1 : bin;
return FastMath.floor(x / 10) == x / 10 ? bin - 1 : bin;
}
/**

View File

@ -820,8 +820,7 @@ public class RandomDataGeneratorTest {
randomData.reSeed(1000);
double v = randomData.nextUniform(0, 1);
randomData.reSeed();
Assert.assertTrue("different seeds", Math
.abs(v - randomData.nextUniform(0, 1)) > 10E-12);
Assert.assertTrue("different seeds", FastMath.abs(v - randomData.nextUniform(0, 1)) > 10E-12);
randomData.reSeed(1000);
Assert.assertEquals("same seeds", v, randomData.nextUniform(0, 1), 10E-12);
randomData.reSeedSecure(1000);

View File

@ -190,7 +190,7 @@ public class ErfTest {
{16.6, 3.48454651995041e-62}
};
for (int i = 0; i < 15; i++) {
final double result = 0.5*Erf.erfc(ref[i][0]/Math.sqrt(2));
final double result = 0.5*Erf.erfc(ref[i][0]/FastMath.sqrt(2));
Assert.assertEquals(ref[i][1], result, 1E-15);
TestUtils.assertRelativelyEquals(ref[i][1], result, 1E-13);
}

View File

@ -471,7 +471,7 @@ public final class StatUtilsTest {
@Test
public void testNormalize1() {
double sample[] = { 50, 100 };
double expectedSample[] = { -25 / Math.sqrt(1250), 25 / Math.sqrt(1250) };
double expectedSample[] = { -25 / FastMath.sqrt(1250), 25 / FastMath.sqrt(1250) };
double[] out = StatUtils.normalize(sample);
for (int i = 0; i < out.length; i++) {
Assert.assertTrue(Precision.equals(out[i], expectedSample[i], 1));
@ -490,7 +490,7 @@ public final class StatUtilsTest {
int length = 77;
double sample[] = new double[length];
for (int i = 0; i < length; i++) {
sample[i] = Math.random();
sample[i] = FastMath.random();
}
// normalize this sample
double standardizedSample[] = StatUtils.normalize(sample);

View File

@ -21,6 +21,7 @@ import org.apache.commons.math3.exception.NotPositiveException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.ZeroException;
import org.apache.commons.math3.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
@ -274,17 +275,17 @@ public class GTestTest {
// negative because k11 is lower than expected
Assert.assertTrue(testStatistic.rootLogLikelihoodRatio(36, 21928, 60280, 623876) < 0.0);
Assert.assertEquals(Math.sqrt(2.772589), testStatistic.rootLogLikelihoodRatio(1, 0, 0, 1), 0.000001);
Assert.assertEquals(-Math.sqrt(2.772589), testStatistic.rootLogLikelihoodRatio(0, 1, 1, 0), 0.000001);
Assert.assertEquals(Math.sqrt(27.72589), testStatistic.rootLogLikelihoodRatio(10, 0, 0, 10), 0.00001);
Assert.assertEquals(FastMath.sqrt(2.772589), testStatistic.rootLogLikelihoodRatio(1, 0, 0, 1), 0.000001);
Assert.assertEquals(-FastMath.sqrt(2.772589), testStatistic.rootLogLikelihoodRatio(0, 1, 1, 0), 0.000001);
Assert.assertEquals(FastMath.sqrt(27.72589), testStatistic.rootLogLikelihoodRatio(10, 0, 0, 10), 0.00001);
Assert.assertEquals(Math.sqrt(39.33052), testStatistic.rootLogLikelihoodRatio(5, 1995, 0, 100000), 0.00001);
Assert.assertEquals(-Math.sqrt(39.33052), testStatistic.rootLogLikelihoodRatio(0, 100000, 5, 1995), 0.00001);
Assert.assertEquals(FastMath.sqrt(39.33052), testStatistic.rootLogLikelihoodRatio(5, 1995, 0, 100000), 0.00001);
Assert.assertEquals(-FastMath.sqrt(39.33052), testStatistic.rootLogLikelihoodRatio(0, 100000, 5, 1995), 0.00001);
Assert.assertEquals(Math.sqrt(4730.737), testStatistic.rootLogLikelihoodRatio(1000, 1995, 1000, 100000), 0.001);
Assert.assertEquals(-Math.sqrt(4730.737), testStatistic.rootLogLikelihoodRatio(1000, 100000, 1000, 1995), 0.001);
Assert.assertEquals(FastMath.sqrt(4730.737), testStatistic.rootLogLikelihoodRatio(1000, 1995, 1000, 100000), 0.001);
Assert.assertEquals(-FastMath.sqrt(4730.737), testStatistic.rootLogLikelihoodRatio(1000, 100000, 1000, 1995), 0.001);
Assert.assertEquals(Math.sqrt(5734.343), testStatistic.rootLogLikelihoodRatio(1000, 1000, 1000, 100000), 0.001);
Assert.assertEquals(Math.sqrt(5714.932), testStatistic.rootLogLikelihoodRatio(1000, 1000, 1000, 99000), 0.001);
Assert.assertEquals(FastMath.sqrt(5734.343), testStatistic.rootLogLikelihoodRatio(1000, 1000, 1000, 100000), 0.001);
Assert.assertEquals(FastMath.sqrt(5714.932), testStatistic.rootLogLikelihoodRatio(1000, 1000, 1000, 99000), 0.001);
}
}

View File

@ -26,6 +26,7 @@ import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
import org.apache.commons.math3.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
@ -514,17 +515,17 @@ public class TestUtilsTest {
// negative because k11 is lower than expected
Assert.assertTrue(TestUtils.rootLogLikelihoodRatio(36, 21928, 60280, 623876) < 0.0);
Assert.assertEquals(Math.sqrt(2.772589), TestUtils.rootLogLikelihoodRatio(1, 0, 0, 1), 0.000001);
Assert.assertEquals(-Math.sqrt(2.772589), TestUtils.rootLogLikelihoodRatio(0, 1, 1, 0), 0.000001);
Assert.assertEquals(Math.sqrt(27.72589), TestUtils.rootLogLikelihoodRatio(10, 0, 0, 10), 0.00001);
Assert.assertEquals(FastMath.sqrt(2.772589), TestUtils.rootLogLikelihoodRatio(1, 0, 0, 1), 0.000001);
Assert.assertEquals(-FastMath.sqrt(2.772589), TestUtils.rootLogLikelihoodRatio(0, 1, 1, 0), 0.000001);
Assert.assertEquals(FastMath.sqrt(27.72589), TestUtils.rootLogLikelihoodRatio(10, 0, 0, 10), 0.00001);
Assert.assertEquals(Math.sqrt(39.33052), TestUtils.rootLogLikelihoodRatio(5, 1995, 0, 100000), 0.00001);
Assert.assertEquals(-Math.sqrt(39.33052), TestUtils.rootLogLikelihoodRatio(0, 100000, 5, 1995), 0.00001);
Assert.assertEquals(FastMath.sqrt(39.33052), TestUtils.rootLogLikelihoodRatio(5, 1995, 0, 100000), 0.00001);
Assert.assertEquals(-FastMath.sqrt(39.33052), TestUtils.rootLogLikelihoodRatio(0, 100000, 5, 1995), 0.00001);
Assert.assertEquals(Math.sqrt(4730.737), TestUtils.rootLogLikelihoodRatio(1000, 1995, 1000, 100000), 0.001);
Assert.assertEquals(-Math.sqrt(4730.737), TestUtils.rootLogLikelihoodRatio(1000, 100000, 1000, 1995), 0.001);
Assert.assertEquals(FastMath.sqrt(4730.737), TestUtils.rootLogLikelihoodRatio(1000, 1995, 1000, 100000), 0.001);
Assert.assertEquals(-FastMath.sqrt(4730.737), TestUtils.rootLogLikelihoodRatio(1000, 100000, 1000, 1995), 0.001);
Assert.assertEquals(Math.sqrt(5734.343), TestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 100000), 0.001);
Assert.assertEquals(Math.sqrt(5714.932), TestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 99000), 0.001);
Assert.assertEquals(FastMath.sqrt(5734.343), TestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 100000), 0.001);
Assert.assertEquals(FastMath.sqrt(5714.932), TestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 99000), 0.001);
}
}

View File

@ -21,6 +21,7 @@ import java.util.Random;
import org.apache.commons.math3.exception.MathIllegalArgumentException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.random.ISAACRandom;
import org.apache.commons.math3.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
@ -162,46 +163,46 @@ public final class SimpleRegressionTest {
if (model1.getN() != model2.getN()) {
return false;
}
if (Math.abs(model1.getIntercept() - model2.getIntercept()) > tol) {
if (FastMath.abs(model1.getIntercept() - model2.getIntercept()) > tol) {
return false;
}
if (Math.abs(model1.getInterceptStdErr() - model2.getInterceptStdErr()) > tol) {
if (FastMath.abs(model1.getInterceptStdErr() - model2.getInterceptStdErr()) > tol) {
return false;
}
if (Math.abs(model1.getMeanSquareError() - model2.getMeanSquareError()) > tol) {
if (FastMath.abs(model1.getMeanSquareError() - model2.getMeanSquareError()) > tol) {
return false;
}
if (Math.abs(model1.getR() - model2.getR()) > tol) {
if (FastMath.abs(model1.getR() - model2.getR()) > tol) {
return false;
}
if (Math.abs(model1.getRegressionSumSquares() - model2.getRegressionSumSquares()) > tol) {
if (FastMath.abs(model1.getRegressionSumSquares() - model2.getRegressionSumSquares()) > tol) {
return false;
}
if (Math.abs(model1.getRSquare() - model2.getRSquare()) > tol) {
if (FastMath.abs(model1.getRSquare() - model2.getRSquare()) > tol) {
return false;
}
if (Math.abs(model1.getSignificance() - model2.getSignificance()) > tol) {
if (FastMath.abs(model1.getSignificance() - model2.getSignificance()) > tol) {
return false;
}
if (Math.abs(model1.getSlope() - model2.getSlope()) > tol) {
if (FastMath.abs(model1.getSlope() - model2.getSlope()) > tol) {
return false;
}
if (Math.abs(model1.getSlopeConfidenceInterval() - model2.getSlopeConfidenceInterval()) > tol) {
if (FastMath.abs(model1.getSlopeConfidenceInterval() - model2.getSlopeConfidenceInterval()) > tol) {
return false;
}
if (Math.abs(model1.getSlopeStdErr() - model2.getSlopeStdErr()) > tol) {
if (FastMath.abs(model1.getSlopeStdErr() - model2.getSlopeStdErr()) > tol) {
return false;
}
if (Math.abs(model1.getSumOfCrossProducts() - model2.getSumOfCrossProducts()) > tol) {
if (FastMath.abs(model1.getSumOfCrossProducts() - model2.getSumOfCrossProducts()) > tol) {
return false;
}
if (Math.abs(model1.getSumSquaredErrors() - model2.getSumSquaredErrors()) > tol) {
if (FastMath.abs(model1.getSumSquaredErrors() - model2.getSumSquaredErrors()) > tol) {
return false;
}
if (Math.abs(model1.getTotalSumSquares() - model2.getTotalSumSquares()) > tol) {
if (FastMath.abs(model1.getTotalSumSquares() - model2.getTotalSumSquares()) > tol) {
return false;
}
if (Math.abs(model1.getXSumSquares() - model2.getXSumSquares()) > tol) {
if (FastMath.abs(model1.getXSumSquares() - model2.getXSumSquares()) > tol) {
return false;
}
return true;

View File

@ -160,7 +160,7 @@ public final class MathUtilsTest {
1e-7);
orig = offset - 123356789 * period - delta;
expected = Math.abs(period) - delta;
expected = FastMath.abs(period) - delta;
Assert.assertEquals(expected,
MathUtils.reduce(orig, period, offset),
1e-6);

View File

@ -506,7 +506,7 @@ public class PrecisionTest {
final double x = 100;
final int numTrials = 10000;
for (int i = 0; i < numTrials; i++) {
final double originalDelta = Math.random();
final double originalDelta = FastMath.random();
final double delta = Precision.representableDelta(x, originalDelta);
if (delta != originalDelta) {
++nonRepresentableCount;