Added new tests.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1424107 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a5f02be035
commit
3c60c05fd4
|
@ -21,6 +21,7 @@ import java.lang.reflect.Field;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.math3.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math3.util.ArithmeticUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
@ -128,6 +129,16 @@ public class DSCompilerTest {
|
|||
|
||||
}
|
||||
|
||||
@Test(expected=DimensionMismatchException.class)
|
||||
public void testIncompatbileParams() {
|
||||
DSCompiler.getCompiler(3, 2).checkCompatibility(DSCompiler.getCompiler(4, 2));
|
||||
}
|
||||
|
||||
@Test(expected=DimensionMismatchException.class)
|
||||
public void testIncompatbileOrder() {
|
||||
DSCompiler.getCompiler(3, 3).checkCompatibility(DSCompiler.getCompiler(3, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSymmetry() {
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
|
|
|
@ -225,24 +225,37 @@ public class DerivativeStructureTest {
|
|||
-2, dsZ,
|
||||
1, new DerivativeStructure(8, dsZ.multiply(dsX),
|
||||
-1, dsY).pow(3));
|
||||
DerivativeStructure dsOther =
|
||||
new DerivativeStructure(1, dsX,
|
||||
5, dsX.multiply(dsY),
|
||||
-2, dsZ).add(new DerivativeStructure(8, dsZ.multiply(dsX),
|
||||
-1, dsY).pow(3));
|
||||
double f = x + 5 * x * y - 2 * z + FastMath.pow(8 * z * x - y, 3);
|
||||
Assert.assertEquals(f, ds.getValue(),
|
||||
FastMath.abs(epsilon * f));
|
||||
Assert.assertEquals(f, dsOther.getValue(),
|
||||
FastMath.abs(epsilon * f));
|
||||
|
||||
// df/dx = 1 + 5 y + 24 (8 z x - y)^2 z
|
||||
double dfdx = 1 + 5 * y + 24 * z * FastMath.pow(8 * z * x - y, 2);
|
||||
Assert.assertEquals(dfdx, ds.getPartialDerivative(1, 0, 0),
|
||||
FastMath.abs(epsilon * dfdx));
|
||||
Assert.assertEquals(dfdx, dsOther.getPartialDerivative(1, 0, 0),
|
||||
FastMath.abs(epsilon * dfdx));
|
||||
|
||||
// df/dxdy = 5 + 48 z*(y - 8 z x)
|
||||
double dfdxdy = 5 + 48 * z * (y - 8 * z * x);
|
||||
Assert.assertEquals(dfdxdy, ds.getPartialDerivative(1, 1, 0),
|
||||
FastMath.abs(epsilon * dfdxdy));
|
||||
Assert.assertEquals(dfdxdy, dsOther.getPartialDerivative(1, 1, 0),
|
||||
FastMath.abs(epsilon * dfdxdy));
|
||||
|
||||
// df/dxdydz = 48 (y - 16 z x)
|
||||
double dfdxdydz = 48 * (y - 16 * z * x);
|
||||
Assert.assertEquals(dfdxdydz, ds.getPartialDerivative(1, 1, 1),
|
||||
FastMath.abs(epsilon * dfdxdydz));
|
||||
Assert.assertEquals(dfdxdydz, dsOther.getPartialDerivative(1, 1, 1),
|
||||
FastMath.abs(epsilon * dfdxdydz));
|
||||
|
||||
}
|
||||
|
||||
|
@ -506,6 +519,51 @@ public class DerivativeStructureTest {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveRemainder() {
|
||||
double epsilon = 1.0e-15;
|
||||
for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
|
||||
for (double x = -1.7; x < 2; x += 0.2) {
|
||||
DerivativeStructure dsX = new DerivativeStructure(2, maxOrder, 0, x);
|
||||
for (double y = -1.7; y < 2; y += 0.2) {
|
||||
DerivativeStructure remainder = dsX.remainder(y);
|
||||
DerivativeStructure ref = dsX.subtract(x - (x % y));
|
||||
DerivativeStructure zero = remainder.subtract(ref);
|
||||
for (int n = 0; n <= maxOrder; ++n) {
|
||||
for (int m = 0; m <= maxOrder; ++m) {
|
||||
if (n + m <= maxOrder) {
|
||||
Assert.assertEquals(0, zero.getPartialDerivative(n, m), epsilon);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemainder() {
|
||||
double epsilon = 1.0e-15;
|
||||
for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
|
||||
for (double x = -1.7; x < 2; x += 0.2) {
|
||||
DerivativeStructure dsX = new DerivativeStructure(2, maxOrder, 0, x);
|
||||
for (double y = -1.7; y < 2; y += 0.2) {
|
||||
DerivativeStructure dsY = new DerivativeStructure(2, maxOrder, 1, y);
|
||||
DerivativeStructure remainder = dsX.remainder(dsY);
|
||||
DerivativeStructure ref = dsX.subtract(dsY.multiply((x - (x % y)) / y));
|
||||
DerivativeStructure zero = remainder.subtract(ref);
|
||||
for (int n = 0; n <= maxOrder; ++n) {
|
||||
for (int m = 0; m <= maxOrder; ++m) {
|
||||
if (n + m <= maxOrder) {
|
||||
Assert.assertEquals(0, zero.getPartialDerivative(n, m), epsilon);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExp() {
|
||||
double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16 };
|
||||
|
|
|
@ -108,6 +108,7 @@ public class FiniteDifferencesDifferentiatorTest {
|
|||
DerivativeStructure dsX = new DerivativeStructure(1, maxError.length - 1, 0, x);
|
||||
DerivativeStructure yRef = gaussian.value(dsX);
|
||||
DerivativeStructure y = f.value(dsX);
|
||||
Assert.assertEquals(f.value(dsX.getValue()), f.value(dsX).getValue(), 1.0e-15);
|
||||
for (int order = 0; order <= yRef.getOrder(); ++order) {
|
||||
maxError[order] = FastMath.max(maxError[order],
|
||||
FastMath.abs(yRef.getPartialDerivative(order) -
|
||||
|
@ -297,9 +298,16 @@ public class FiniteDifferencesDifferentiatorTest {
|
|||
});
|
||||
|
||||
for (double x = -10; x < 10; x += 0.1) {
|
||||
DerivativeStructure[] y = f.value(new DerivativeStructure(1, 2, 0, x));
|
||||
DerivativeStructure dsX = new DerivativeStructure(1, 2, 0, x);
|
||||
DerivativeStructure[] y = f.value(dsX);
|
||||
double cos = FastMath.cos(x);
|
||||
double sin = FastMath.sin(x);
|
||||
double[] f1 = f.value(dsX.getValue());
|
||||
DerivativeStructure[] f2 = f.value(dsX);
|
||||
Assert.assertEquals(f1.length, f2.length);
|
||||
for (int i = 0; i < f1.length; ++i) {
|
||||
Assert.assertEquals(f1[i], f2[i].getValue(), 1.0e-15);
|
||||
}
|
||||
Assert.assertEquals( cos, y[0].getValue(), 7.0e-16);
|
||||
Assert.assertEquals( sin, y[1].getValue(), 7.0e-16);
|
||||
Assert.assertEquals(-sin, y[0].getPartialDerivative(1), 6.0e-14);
|
||||
|
@ -328,11 +336,21 @@ public class FiniteDifferencesDifferentiatorTest {
|
|||
});
|
||||
|
||||
for (double x = -1; x < 1; x += 0.02) {
|
||||
DerivativeStructure[][] y = f.value(new DerivativeStructure(1, 2, 0, x));
|
||||
DerivativeStructure dsX = new DerivativeStructure(1, 2, 0, x);
|
||||
DerivativeStructure[][] y = f.value(dsX);
|
||||
double cos = FastMath.cos(x);
|
||||
double sin = FastMath.sin(x);
|
||||
double cosh = FastMath.cosh(x);
|
||||
double sinh = FastMath.sinh(x);
|
||||
double[][] f1 = f.value(dsX.getValue());
|
||||
DerivativeStructure[][] f2 = f.value(dsX);
|
||||
Assert.assertEquals(f1.length, f2.length);
|
||||
for (int i = 0; i < f1.length; ++i) {
|
||||
Assert.assertEquals(f1[i].length, f2[i].length);
|
||||
for (int j = 0; j < f1[i].length; ++j) {
|
||||
Assert.assertEquals(f1[i][j], f2[i][j].getValue(), 1.0e-15);
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(cos, y[0][0].getValue(), 7.0e-18);
|
||||
Assert.assertEquals(sin, y[0][1].getValue(), 6.0e-17);
|
||||
Assert.assertEquals(cosh, y[1][0].getValue(), 3.0e-16);
|
||||
|
|
Loading…
Reference in New Issue