1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.apache.commons.math4.legacy.analysis.integration;
18  
19  import org.apache.commons.math4.legacy.analysis.QuinticFunction;
20  import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
21  import org.apache.commons.math4.legacy.analysis.function.Sin;
22  import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
23  import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
24  import org.apache.commons.math4.core.jdkmath.JdkMath;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  public final class RombergIntegratorTest {
38  
39      
40  
41  
42      @Test
43      public void testSinFunction() {
44          UnivariateFunction f = new Sin();
45          UnivariateIntegrator integrator = new RombergIntegrator();
46          double min;
47          double max;
48          double expected;
49          double result;
50          double tolerance;
51  
52          min = 0; max = JdkMath.PI; expected = 2;
53          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
54          result = integrator.integrate(100, f, min, max);
55          Assert.assertTrue(integrator.getEvaluations() < 50);
56          Assert.assertTrue(integrator.getIterations()  < 10);
57          Assert.assertEquals(expected, result, tolerance);
58  
59          min = -JdkMath.PI/3; max = 0; expected = -0.5;
60          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
61          result = integrator.integrate(100, f, min, max);
62          Assert.assertTrue(integrator.getEvaluations() < 50);
63          Assert.assertTrue(integrator.getIterations()  < 10);
64          Assert.assertEquals(expected, result, tolerance);
65      }
66  
67      
68  
69  
70      @Test
71      public void testQuinticFunction() {
72          UnivariateFunction f = new QuinticFunction();
73          UnivariateIntegrator integrator = new RombergIntegrator();
74          double min;
75          double max;
76          double expected;
77          double result;
78          double tolerance;
79  
80          min = 0; max = 1; expected = -1.0/48;
81          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
82          result = integrator.integrate(100, f, min, max);
83          Assert.assertTrue(integrator.getEvaluations() < 10);
84          Assert.assertTrue(integrator.getIterations()  < 5);
85          Assert.assertEquals(expected, result, tolerance);
86  
87          min = 0; max = 0.5; expected = 11.0/768;
88          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
89          result = integrator.integrate(100, f, min, max);
90          Assert.assertTrue(integrator.getEvaluations() < 10);
91          Assert.assertTrue(integrator.getIterations()  < 5);
92          Assert.assertEquals(expected, result, tolerance);
93  
94          min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
95          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
96          result = integrator.integrate(100, f, min, max);
97          Assert.assertTrue(integrator.getEvaluations() < 10);
98          Assert.assertTrue(integrator.getIterations()  < 5);
99          Assert.assertEquals(expected, result, tolerance);
100     }
101 
102     
103 
104 
105     @Test
106     public void testParameters() {
107         UnivariateFunction f = new Sin();
108 
109         try {
110             
111             new RombergIntegrator().integrate(1000, f, 1, -1);
112             Assert.fail("Expecting NumberIsTooLargeException - bad interval");
113         } catch (NumberIsTooLargeException ex) {
114             
115         }
116         try {
117             
118             new RombergIntegrator(5, 4);
119             Assert.fail("Expecting NumberIsTooSmallException - bad iteration limits");
120         } catch (NumberIsTooSmallException ex) {
121             
122         }
123         try {
124             
125             new RombergIntegrator(10, 50);
126             Assert.fail("Expecting NumberIsTooLargeException - bad iteration limits");
127         } catch (NumberIsTooLargeException ex) {
128             
129         }
130     }
131 }