001: package JSci.tests;
002:
003: import JSci.maths.*;
004: import JSci.maths.polynomials.*;
005:
006: /**
007: * Testcase for numerical integration methods.
008: * @author Mark Hale
009: */
010: public class NumericalTest extends junit.framework.TestCase {
011: private final Mapping testFunc = new Mapping() {
012: public double map(double x) {
013: return 1.0 / x;
014: }
015: };
016: private final double testFunc_a = 1.0;
017: private final double testFunc_b = Math.E;
018: private final double testFuncExpected = 1.0;
019:
020: /** dy/dx = y */
021: private final RealPolynomial testDeriv = new RealPolynomial(
022: new double[] { 0.0, 1.0 });
023: private final double testDerivInitial = 1.0;
024: private final double testDerivExpected = Math.E;
025:
026: public static void main(String arg[]) {
027: junit.textui.TestRunner.run(NumericalTest.class);
028: }
029:
030: public NumericalTest(String name) {
031: super (name);
032: }
033:
034: protected void setUp() {
035: JSci.GlobalSettings.ZERO_TOL = 1.0e-6;
036: }
037:
038: public void testSolveQuadratic() {
039: double x1 = ExtraMath.random(-1.0, 1.0);
040: double x2 = ExtraMath.random(-1.0, 1.0);
041: double a = ExtraMath.random(-1.0, 1.0);
042: double b = -a * (x1 + x2);
043: double c = a * x1 * x2;
044: double[] roots = NumericalMath.solveQuadratic(a, b, c);
045: assertEquals(Math.min(x1, x2), Math.min(roots[0], roots[1]),
046: JSci.GlobalSettings.ZERO_TOL);
047: assertEquals(Math.max(x1, x2), Math.max(roots[0], roots[1]),
048: JSci.GlobalSettings.ZERO_TOL);
049: }
050:
051: public void testTrapezium() {
052: double ans = NumericalMath.trapezium(500, testFunc, testFunc_a,
053: testFunc_b);
054: assertEquals(testFuncExpected, ans,
055: JSci.GlobalSettings.ZERO_TOL);
056: }
057:
058: public void testSimpson() {
059: double ans = NumericalMath.simpson(20, testFunc, testFunc_a,
060: testFunc_b);
061: assertEquals(testFuncExpected, ans,
062: JSci.GlobalSettings.ZERO_TOL);
063: }
064:
065: public void testRichardson() {
066: double ans = NumericalMath.richardson(5, testFunc, testFunc_a,
067: testFunc_b);
068: assertEquals(testFuncExpected, ans,
069: JSci.GlobalSettings.ZERO_TOL);
070: }
071:
072: public void testGaussian4() {
073: double ans = NumericalMath.gaussian4(2, testFunc, testFunc_a,
074: testFunc_b);
075: assertEquals(testFuncExpected, ans,
076: JSci.GlobalSettings.ZERO_TOL);
077: }
078:
079: public void testGaussian8() {
080: double ans = NumericalMath.gaussian8(1, testFunc, testFunc_a,
081: testFunc_b);
082: assertEquals(testFuncExpected, ans,
083: JSci.GlobalSettings.ZERO_TOL);
084: }
085:
086: public void testSimpleRungeKutta2() {
087: double[] y = new double[5001];
088: y[0] = testDerivInitial;
089: NumericalMath.rungeKutta2(y, testDeriv, 1.0 / (y.length - 1));
090: assertEquals(testDerivExpected, y[y.length - 1],
091: JSci.GlobalSettings.ZERO_TOL);
092: }
093:
094: public void testSimpleRungeKutta4() {
095: double[] y = new double[51];
096: y[0] = testDerivInitial;
097: NumericalMath.rungeKutta4(y, testDeriv, 1.0 / (y.length - 1));
098: assertEquals(testDerivExpected, y[y.length - 1],
099: JSci.GlobalSettings.ZERO_TOL);
100: }
101:
102: public void testRungeKutta2() {
103: double[] y = new double[5001];
104: y[0] = testDerivInitial;
105: NumericalMath.rungeKutta2(y, testDeriv.tensor(testDeriv), 0.0,
106: Math.sqrt(2.0) / (y.length - 1));
107: assertEquals(testDerivExpected, y[y.length - 1],
108: JSci.GlobalSettings.ZERO_TOL);
109: }
110:
111: public void testRungeKutta4() {
112: double[] y = new double[51];
113: y[0] = testDerivInitial;
114: NumericalMath.rungeKutta4(y, testDeriv.tensor(testDeriv), 0.0,
115: Math.sqrt(2.0) / (y.length - 1));
116: assertEquals(testDerivExpected, y[y.length - 1],
117: JSci.GlobalSettings.ZERO_TOL);
118: }
119: }
|