KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > tests > NumericalTest


1 package JSci.tests;
2
3 import JSci.maths.*;
4 import JSci.maths.polynomials.*;
5
6 /**
7 * Testcase for numerical integration methods.
8 * @author Mark Hale
9 */

10 public class NumericalTest extends junit.framework.TestCase {
11         private final Mapping testFunc=new Mapping() {
12                 public double map(double x) {
13                         return 1.0/x;
14                 }
15         };
16         private final double testFunc_a=1.0;
17         private final double testFunc_b=Math.E;
18         private final double testFuncExpected=1.0;
19
20         /** dy/dx = y */
21         private final RealPolynomial testDeriv=new RealPolynomial(new double[] {0.0, 1.0});
22         private final double testDerivInitial=1.0;
23         private final double testDerivExpected=Math.E;
24
25         public static void main(String JavaDoc arg[]) {
26                 junit.textui.TestRunner.run(NumericalTest.class);
27         }
28         public NumericalTest(String JavaDoc name) {
29                 super(name);
30         }
31         protected void setUp() {
32                 JSci.GlobalSettings.ZERO_TOL=1.0e-6;
33         }
34         public void testSolveQuadratic() {
35                 double x1 = ExtraMath.random(-1.0, 1.0);
36                 double x2 = ExtraMath.random(-1.0, 1.0);
37                 double a = ExtraMath.random(-1.0, 1.0);
38                 double b = -a*(x1+x2);
39                 double c = a*x1*x2;
40                 double[] roots = NumericalMath.solveQuadratic(a, b, c);
41                 assertEquals(Math.min(x1,x2), Math.min(roots[0],roots[1]), JSci.GlobalSettings.ZERO_TOL);
42                 assertEquals(Math.max(x1,x2), Math.max(roots[0],roots[1]), JSci.GlobalSettings.ZERO_TOL);
43         }
44         public void testTrapezium() {
45                 double ans=NumericalMath.trapezium(500,testFunc,testFunc_a,testFunc_b);
46                 assertEquals(testFuncExpected, ans, JSci.GlobalSettings.ZERO_TOL);
47         }
48         public void testSimpson() {
49                 double ans=NumericalMath.simpson(20,testFunc,testFunc_a,testFunc_b);
50                 assertEquals(testFuncExpected, ans, JSci.GlobalSettings.ZERO_TOL);
51         }
52         public void testRichardson() {
53                 double ans=NumericalMath.richardson(5,testFunc,testFunc_a,testFunc_b);
54                 assertEquals(testFuncExpected, ans, JSci.GlobalSettings.ZERO_TOL);
55         }
56         public void testGaussian4() {
57                 double ans=NumericalMath.gaussian4(2,testFunc,testFunc_a,testFunc_b);
58                 assertEquals(testFuncExpected, ans, JSci.GlobalSettings.ZERO_TOL);
59         }
60         public void testGaussian8() {
61                 double ans=NumericalMath.gaussian8(1,testFunc,testFunc_a,testFunc_b);
62                 assertEquals(testFuncExpected, ans, JSci.GlobalSettings.ZERO_TOL);
63         }
64         public void testSimpleRungeKutta2() {
65                 double[] y = new double[5001];
66                 y[0] = testDerivInitial;
67                 NumericalMath.rungeKutta2(y, testDeriv, 1.0/(y.length-1));
68                 assertEquals(testDerivExpected, y[y.length-1], JSci.GlobalSettings.ZERO_TOL);
69         }
70         public void testSimpleRungeKutta4() {
71                 double[] y = new double[51];
72                 y[0] = testDerivInitial;
73                 NumericalMath.rungeKutta4(y, testDeriv, 1.0/(y.length-1));
74                 assertEquals(testDerivExpected, y[y.length-1], JSci.GlobalSettings.ZERO_TOL);
75         }
76         public void testRungeKutta2() {
77                 double[] y = new double[5001];
78                 y[0] = testDerivInitial;
79                 NumericalMath.rungeKutta2(y, testDeriv.tensor(testDeriv), 0.0, Math.sqrt(2.0)/(y.length-1));
80                 assertEquals(testDerivExpected, y[y.length-1], JSci.GlobalSettings.ZERO_TOL);
81         }
82         public void testRungeKutta4() {
83                 double[] y = new double[51];
84                 y[0] = testDerivInitial;
85                 NumericalMath.rungeKutta4(y, testDeriv.tensor(testDeriv), 0.0, Math.sqrt(2.0)/(y.length-1));
86                 assertEquals(testDerivExpected, y[y.length-1], JSci.GlobalSettings.ZERO_TOL);
87         }
88 }
89
90
Popular Tags