View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test case for Romberg integrator.
31   * <p>
32   * Romberg algorithm is very fast for good behavior integrand. Test runs
33   * show that for a default relative accuracy of 1E-6, it generally takes
34   * takes less than 5 iterations for the integral to converge.
35   *
36   */
37  public final class RombergIntegratorTest {
38  
39      /**
40       * Test of integrator for the sine function.
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       * Test of integrator for the quintic function.
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      * Test of parameters for the integrator.
104      */
105     @Test
106     public void testParameters() {
107         UnivariateFunction f = new Sin();
108 
109         try {
110             // bad interval
111             new RombergIntegrator().integrate(1000, f, 1, -1);
112             Assert.fail("Expecting NumberIsTooLargeException - bad interval");
113         } catch (NumberIsTooLargeException ex) {
114             // expected
115         }
116         try {
117             // bad iteration limits
118             new RombergIntegrator(5, 4);
119             Assert.fail("Expecting NumberIsTooSmallException - bad iteration limits");
120         } catch (NumberIsTooSmallException ex) {
121             // expected
122         }
123         try {
124             // bad iteration limits
125             new RombergIntegrator(10, 50);
126             Assert.fail("Expecting NumberIsTooLargeException - bad iteration limits");
127         } catch (NumberIsTooLargeException ex) {
128             // expected
129         }
130     }
131 }