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  public final class TrapezoidIntegratorTest {
37  
38      
39  
40  
41      @Test
42      public void testSinFunction() {
43          UnivariateFunction f = new Sin();
44          UnivariateIntegrator integrator = new TrapezoidIntegrator();
45          double min;
46          double max;
47          double expected;
48          double result;
49          double tolerance;
50  
51          min = 0; max = JdkMath.PI; expected = 2;
52          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
53          result = integrator.integrate(10000, f, min, max);
54          Assert.assertTrue(integrator.getEvaluations() < 2500);
55          Assert.assertTrue(integrator.getIterations()  < 15);
56          Assert.assertEquals(expected, result, tolerance);
57  
58          min = -JdkMath.PI/3; max = 0; expected = -0.5;
59          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
60          result = integrator.integrate(10000, f, min, max);
61          Assert.assertTrue(integrator.getEvaluations() < 2500);
62          Assert.assertTrue(integrator.getIterations()  < 15);
63          Assert.assertEquals(expected, result, tolerance);
64      }
65  
66      
67  
68  
69      @Test
70      public void testQuinticFunction() {
71          UnivariateFunction f = new QuinticFunction();
72          UnivariateIntegrator integrator = new TrapezoidIntegrator();
73          double min;
74          double max;
75          double expected;
76          double result;
77          double tolerance;
78  
79          min = 0; max = 1; expected = -1.0/48;
80          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
81          result = integrator.integrate(10000, f, min, max);
82          Assert.assertTrue(integrator.getEvaluations() < 5000);
83          Assert.assertTrue(integrator.getIterations()  < 15);
84          Assert.assertEquals(expected, result, tolerance);
85  
86          min = 0; max = 0.5; expected = 11.0/768;
87          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
88          result = integrator.integrate(10000, f, min, max);
89          Assert.assertTrue(integrator.getEvaluations() < 2500);
90          Assert.assertTrue(integrator.getIterations()  < 15);
91          Assert.assertEquals(expected, result, tolerance);
92  
93          min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
94          tolerance = JdkMath.abs(expected * integrator.getRelativeAccuracy());
95          result = integrator.integrate(10000, f, min, max);
96          Assert.assertTrue(integrator.getEvaluations() < 5000);
97          Assert.assertTrue(integrator.getIterations()  < 15);
98          Assert.assertEquals(expected, result, tolerance);
99      }
100 
101     
102 
103 
104     @Test
105     public void testParameters() {
106         UnivariateFunction f = new Sin();
107 
108         try {
109             
110             new TrapezoidIntegrator().integrate(1000, f, 1, -1);
111             Assert.fail("Expecting NumberIsTooLargeException - bad interval");
112         } catch (NumberIsTooLargeException ex) {
113             
114         }
115         try {
116             
117             new TrapezoidIntegrator(5, 4);
118             Assert.fail("Expecting NumberIsTooSmallException - bad iteration limits");
119         } catch (NumberIsTooSmallException ex) {
120             
121         }
122         try {
123             
124             new TrapezoidIntegrator(10,99);
125             Assert.fail("Expecting NumberIsTooLargeException - bad iteration limits");
126         } catch (NumberIsTooLargeException ex) {
127             
128         }
129     }
130 }