[MATH-1059] Replace Math with FastMath.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1540167 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-11-08 20:00:08 +00:00
parent f2a833757b
commit 5ee76f095e
4 changed files with 72 additions and 69 deletions

View File

@ -143,7 +143,7 @@ public class KalmanFilterTest {
filter.correct(z);
// state estimate shouldn't be larger than measurement noise
double diff = Math.abs(constantValue - filter.getStateEstimation()[0]);
double diff = FastMath.abs(constantValue - filter.getStateEstimation()[0]);
// System.out.println(diff);
Assert.assertTrue(Precision.compareTo(diff, measurementNoise, 1e-6) < 0);
}
@ -171,7 +171,7 @@ public class KalmanFilterTest {
// B = [ dt^2/2 ]
// [ dt ]
RealMatrix B = new Array2DRowRealMatrix(
new double[][] { { Math.pow(dt, 2d) / 2d }, { dt } });
new double[][] { { FastMath.pow(dt, 2d) / 2d }, { dt } });
// H = [ 1 0 ]
RealMatrix H = new Array2DRowRealMatrix(new double[][] { { 1d, 0d } });
@ -180,12 +180,12 @@ public class KalmanFilterTest {
RealVector x = new ArrayRealVector(new double[] { 0, 0 });
RealMatrix tmp = new Array2DRowRealMatrix(
new double[][] { { Math.pow(dt, 4d) / 4d, Math.pow(dt, 3d) / 2d },
{ Math.pow(dt, 3d) / 2d, Math.pow(dt, 2d) } });
new double[][] { { FastMath.pow(dt, 4d) / 4d, FastMath.pow(dt, 3d) / 2d },
{ FastMath.pow(dt, 3d) / 2d, FastMath.pow(dt, 2d) } });
// Q = [ dt^4/4 dt^3/2 ]
// [ dt^3/2 dt^2 ]
RealMatrix Q = tmp.scalarMultiply(Math.pow(accelNoise, 2));
RealMatrix Q = tmp.scalarMultiply(FastMath.pow(accelNoise, 2));
// P0 = [ 1 1 ]
// [ 1 1 ]
@ -193,7 +193,7 @@ public class KalmanFilterTest {
// R = [ measurementNoise^2 ]
RealMatrix R = new Array2DRowRealMatrix(
new double[] { Math.pow(measurementNoise, 2) });
new double[] { FastMath.pow(measurementNoise, 2) });
// constant control input, increase velocity by 0.1 m/s per cycle
RealVector u = new ArrayRealVector(new double[] { 0.1d });
@ -214,7 +214,7 @@ public class KalmanFilterTest {
RandomGenerator rand = new JDKRandomGenerator();
RealVector tmpPNoise = new ArrayRealVector(
new double[] { Math.pow(dt, 2d) / 2d, dt });
new double[] { FastMath.pow(dt, 2d) / 2d, dt });
// iterate 60 steps
for (int i = 0; i < 60; i++) {
@ -235,7 +235,7 @@ public class KalmanFilterTest {
filter.correct(z);
// state estimate shouldn't be larger than the measurement noise
double diff = Math.abs(x.getEntry(0) - filter.getStateEstimation()[0]);
double diff = FastMath.abs(x.getEntry(0) - filter.getStateEstimation()[0]);
Assert.assertTrue(Precision.compareTo(diff, measurementNoise, 1e-6) < 0);
}

View File

@ -18,6 +18,7 @@ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv;
import java.util.Arrays;
import java.util.Random;
import org.apache.commons.math3.Retry;
import org.apache.commons.math3.RetryRunner;
import org.apache.commons.math3.analysis.MultivariateFunction;
@ -33,6 +34,7 @@ import org.apache.commons.math3.optim.SimpleBounds;
import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction;
import org.apache.commons.math3.optim.MaxEval;
import org.apache.commons.math3.random.MersenneTwister;
import org.apache.commons.math3.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -44,7 +46,7 @@ import org.junit.runner.RunWith;
public class CMAESOptimizerTest {
static final int DIM = 13;
static final int LAMBDA = 4 + (int)(3.*Math.log(DIM));
static final int LAMBDA = 4 + (int)(3.*FastMath.log(DIM));
@Test(expected = NumberIsTooLargeException.class)
public void testInitOutofbounds1() {
@ -327,10 +329,10 @@ public class CMAESOptimizerTest {
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new Rastrigin(), startPoint, insigma, boundaries,
GoalType.MINIMIZE, (int)(200*Math.sqrt(DIM)), true, 0, 1e-13,
GoalType.MINIMIZE, (int)(200*FastMath.sqrt(DIM)), true, 0, 1e-13,
1e-13, 1e-6, 200000, expected);
doTest(new Rastrigin(), startPoint, insigma, boundaries,
GoalType.MINIMIZE, (int)(200*Math.sqrt(DIM)), false, 0, 1e-13,
GoalType.MINIMIZE, (int)(200*FastMath.sqrt(DIM)), false, 0, 1e-13,
1e-13, 1e-6, 200000, expected);
}
@ -630,7 +632,7 @@ public class CMAESOptimizerTest {
double f = 0;
x = B.Rotate(x);
for (int i = 0; i < x.length; ++i)
f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
return f;
}
}
@ -650,7 +652,7 @@ public class CMAESOptimizerTest {
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i)
f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
return f;
}
}
@ -667,7 +669,7 @@ public class CMAESOptimizerTest {
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i)
f += Math.pow(Math.abs(x[i]), 2. + 10 * (double) i
f += FastMath.pow(FastMath.abs(x[i]), 2. + 10 * (double) i
/ (x.length - 1.));
return f;
}
@ -676,7 +678,7 @@ public class CMAESOptimizerTest {
private static class SsDiffPow implements MultivariateFunction {
public double value(double[] x) {
double f = Math.pow(new DiffPow().value(x), 0.25);
double f = FastMath.pow(new DiffPow().value(x), 0.25);
return f;
}
}
@ -708,12 +710,12 @@ public class CMAESOptimizerTest {
double res2 = 0;
double fac = 0;
for (int i = 0; i < x.length; ++i) {
fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.));
fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.));
f += fac * fac * x[i] * x[i];
res2 += Math.cos(2. * Math.PI * fac * x[i]);
res2 += FastMath.cos(2. * FastMath.PI * fac * x[i]);
}
f = (20. - 20. * Math.exp(-0.2 * Math.sqrt(f / x.length))
+ Math.exp(1.) - Math.exp(res2 / x.length));
f = (20. - 20. * FastMath.exp(-0.2 * FastMath.sqrt(f / x.length))
+ FastMath.exp(1.) - FastMath.exp(res2 / x.length));
return f;
}
}
@ -736,11 +738,11 @@ public class CMAESOptimizerTest {
double f = 0;
double fac;
for (int i = 0; i < x.length; ++i) {
fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.));
fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.));
if (i == 0 && x[i] < 0)
fac *= 1.;
f += fac * fac * x[i] * x[i] + amplitude
* (1. - Math.cos(2. * Math.PI * fac * x[i]));
* (1. - FastMath.cos(2. * FastMath.PI * fac * x[i]));
}
return f;
}
@ -785,7 +787,7 @@ public class CMAESOptimizerTest {
for (sp = 0., k = 0; k < DIM; ++k)
sp += basis[i][k] * basis[i][k]; /* squared norm */
for (k = 0; k < DIM; ++k)
basis[i][k] /= Math.sqrt(sp);
basis[i][k] /= FastMath.sqrt(sp);
}
}
}

View File

@ -32,6 +32,7 @@ import org.apache.commons.math3.optimization.PointValuePair;
import org.apache.commons.math3.optimization.InitialGuess;
import org.apache.commons.math3.optimization.SimpleBounds;
import org.apache.commons.math3.random.MersenneTwister;
import org.apache.commons.math3.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -43,7 +44,7 @@ import org.junit.runner.RunWith;
public class CMAESOptimizerTest {
static final int DIM = 13;
static final int LAMBDA = 4 + (int)(3.*Math.log(DIM));
static final int LAMBDA = 4 + (int)(3.*FastMath.log(DIM));
@Test(expected = NumberIsTooLargeException.class)
public void testInitOutofbounds1() {
@ -326,10 +327,10 @@ public class CMAESOptimizerTest {
PointValuePair expected =
new PointValuePair(point(DIM,0.0),0.0);
doTest(new Rastrigin(), startPoint, insigma, boundaries,
GoalType.MINIMIZE, (int)(200*Math.sqrt(DIM)), true, 0, 1e-13,
GoalType.MINIMIZE, (int)(200*FastMath.sqrt(DIM)), true, 0, 1e-13,
1e-13, 1e-6, 200000, expected);
doTest(new Rastrigin(), startPoint, insigma, boundaries,
GoalType.MINIMIZE, (int)(200*Math.sqrt(DIM)), false, 0, 1e-13,
GoalType.MINIMIZE, (int)(200*FastMath.sqrt(DIM)), false, 0, 1e-13,
1e-13, 1e-6, 200000, expected);
}
@ -596,7 +597,7 @@ public class CMAESOptimizerTest {
double f = 0;
x = B.Rotate(x);
for (int i = 0; i < x.length; ++i)
f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
return f;
}
}
@ -616,7 +617,7 @@ public class CMAESOptimizerTest {
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i)
f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i];
return f;
}
}
@ -633,7 +634,7 @@ public class CMAESOptimizerTest {
public double value(double[] x) {
double f = 0;
for (int i = 0; i < x.length; ++i)
f += Math.pow(Math.abs(x[i]), 2. + 10 * (double) i
f += FastMath.pow(FastMath.abs(x[i]), 2. + 10 * (double) i
/ (x.length - 1.));
return f;
}
@ -642,7 +643,7 @@ public class CMAESOptimizerTest {
private static class SsDiffPow implements MultivariateFunction {
public double value(double[] x) {
double f = Math.pow(new DiffPow().value(x), 0.25);
double f = FastMath.pow(new DiffPow().value(x), 0.25);
return f;
}
}
@ -674,12 +675,12 @@ public class CMAESOptimizerTest {
double res2 = 0;
double fac = 0;
for (int i = 0; i < x.length; ++i) {
fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.));
fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.));
f += fac * fac * x[i] * x[i];
res2 += Math.cos(2. * Math.PI * fac * x[i]);
res2 += FastMath.cos(2. * FastMath.PI * fac * x[i]);
}
f = (20. - 20. * Math.exp(-0.2 * Math.sqrt(f / x.length))
+ Math.exp(1.) - Math.exp(res2 / x.length));
f = (20. - 20. * FastMath.exp(-0.2 * FastMath.sqrt(f / x.length))
+ FastMath.exp(1.) - FastMath.exp(res2 / x.length));
return f;
}
}
@ -702,11 +703,11 @@ public class CMAESOptimizerTest {
double f = 0;
double fac;
for (int i = 0; i < x.length; ++i) {
fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.));
fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.));
if (i == 0 && x[i] < 0)
fac *= 1.;
f += fac * fac * x[i] * x[i] + amplitude
* (1. - Math.cos(2. * Math.PI * fac * x[i]));
* (1. - FastMath.cos(2. * FastMath.PI * fac * x[i]));
}
return f;
}
@ -751,7 +752,7 @@ public class CMAESOptimizerTest {
for (sp = 0., k = 0; k < DIM; ++k)
sp += basis[i][k] * basis[i][k]; /* squared norm */
for (k = 0; k < DIM; ++k)
basis[i][k] /= Math.sqrt(sp);
basis[i][k] /= FastMath.sqrt(sp);
}
}
}

View File

@ -392,25 +392,25 @@ public class MathArraysTest {
MathArrays.sortInPlace(x1, x2, x3);
Assert.assertEquals(-3, x1[0], Math.ulp(1d));
Assert.assertEquals(9, x2[0], Math.ulp(1d));
Assert.assertEquals(-27, x3[0], Math.ulp(1d));
Assert.assertEquals(-3, x1[0], FastMath.ulp(1d));
Assert.assertEquals(9, x2[0], FastMath.ulp(1d));
Assert.assertEquals(-27, x3[0], FastMath.ulp(1d));
Assert.assertEquals(1, x1[1], Math.ulp(1d));
Assert.assertEquals(1, x2[1], Math.ulp(1d));
Assert.assertEquals(1, x3[1], Math.ulp(1d));
Assert.assertEquals(1, x1[1], FastMath.ulp(1d));
Assert.assertEquals(1, x2[1], FastMath.ulp(1d));
Assert.assertEquals(1, x3[1], FastMath.ulp(1d));
Assert.assertEquals(2, x1[2], Math.ulp(1d));
Assert.assertEquals(4, x2[2], Math.ulp(1d));
Assert.assertEquals(8, x3[2], Math.ulp(1d));
Assert.assertEquals(2, x1[2], FastMath.ulp(1d));
Assert.assertEquals(4, x2[2], FastMath.ulp(1d));
Assert.assertEquals(8, x3[2], FastMath.ulp(1d));
Assert.assertEquals(4, x1[3], Math.ulp(1d));
Assert.assertEquals(16, x2[3], Math.ulp(1d));
Assert.assertEquals(64, x3[3], Math.ulp(1d));
Assert.assertEquals(4, x1[3], FastMath.ulp(1d));
Assert.assertEquals(16, x2[3], FastMath.ulp(1d));
Assert.assertEquals(64, x3[3], FastMath.ulp(1d));
Assert.assertEquals(5, x1[4], Math.ulp(1d));
Assert.assertEquals(25, x2[4], Math.ulp(1d));
Assert.assertEquals(125, x3[4], Math.ulp(1d));
Assert.assertEquals(5, x1[4], FastMath.ulp(1d));
Assert.assertEquals(25, x2[4], FastMath.ulp(1d));
Assert.assertEquals(125, x3[4], FastMath.ulp(1d));
}
@Test
@ -423,25 +423,25 @@ public class MathArraysTest {
MathArrays.OrderDirection.DECREASING,
x2, x3);
Assert.assertEquals(-3, x1[4], Math.ulp(1d));
Assert.assertEquals(9, x2[4], Math.ulp(1d));
Assert.assertEquals(-27, x3[4], Math.ulp(1d));
Assert.assertEquals(-3, x1[4], FastMath.ulp(1d));
Assert.assertEquals(9, x2[4], FastMath.ulp(1d));
Assert.assertEquals(-27, x3[4], FastMath.ulp(1d));
Assert.assertEquals(1, x1[3], Math.ulp(1d));
Assert.assertEquals(1, x2[3], Math.ulp(1d));
Assert.assertEquals(1, x3[3], Math.ulp(1d));
Assert.assertEquals(1, x1[3], FastMath.ulp(1d));
Assert.assertEquals(1, x2[3], FastMath.ulp(1d));
Assert.assertEquals(1, x3[3], FastMath.ulp(1d));
Assert.assertEquals(2, x1[2], Math.ulp(1d));
Assert.assertEquals(4, x2[2], Math.ulp(1d));
Assert.assertEquals(8, x3[2], Math.ulp(1d));
Assert.assertEquals(2, x1[2], FastMath.ulp(1d));
Assert.assertEquals(4, x2[2], FastMath.ulp(1d));
Assert.assertEquals(8, x3[2], FastMath.ulp(1d));
Assert.assertEquals(4, x1[1], Math.ulp(1d));
Assert.assertEquals(16, x2[1], Math.ulp(1d));
Assert.assertEquals(64, x3[1], Math.ulp(1d));
Assert.assertEquals(4, x1[1], FastMath.ulp(1d));
Assert.assertEquals(16, x2[1], FastMath.ulp(1d));
Assert.assertEquals(64, x3[1], FastMath.ulp(1d));
Assert.assertEquals(5, x1[0], Math.ulp(1d));
Assert.assertEquals(25, x2[0], Math.ulp(1d));
Assert.assertEquals(125, x3[0], Math.ulp(1d));
Assert.assertEquals(5, x1[0], FastMath.ulp(1d));
Assert.assertEquals(25, x2[0], FastMath.ulp(1d));
Assert.assertEquals(125, x3[0], FastMath.ulp(1d));
}
@Test
@ -535,7 +535,7 @@ public class MathArraysTest {
-Double.MAX_VALUE,
-1, 0,
Double.MIN_VALUE,
Math.ulp(1d),
FastMath.ulp(1d),
1, 3, 113, 4769,
Double.MAX_VALUE,
Double.POSITIVE_INFINITY };
@ -553,7 +553,7 @@ public class MathArraysTest {
-Double.MAX_VALUE,
-1, 0,
Double.MIN_VALUE,
Math.ulp(1d),
FastMath.ulp(1d),
1, 3, 113, 4769,
Double.MAX_VALUE,
Double.POSITIVE_INFINITY };
@ -572,7 +572,7 @@ public class MathArraysTest {
-Double.MAX_VALUE,
-1, 0,
Double.MIN_VALUE,
Math.ulp(1d),
FastMath.ulp(1d),
1, 3, 113, 4769,
Double.MAX_VALUE,
Double.POSITIVE_INFINITY };