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.math.analysis.integration;
18  
19  import org.apache.commons.math.MathException;
20  import org.apache.commons.math.analysis.QuinticFunction;
21  import org.apache.commons.math.analysis.SinFunction;
22  import org.apache.commons.math.analysis.UnivariateRealFunction;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * Testcase for Romberg integrator.
28   * <p>
29   * Romberg algorithm is very fast for good behavior integrand. Test runs
30   * show that for a default relative accuracy of 1E-6, it generally takes
31   * takes less than 5 iterations for the integral to converge.
32   * 
33   * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $ 
34   */
35  public final class RombergIntegratorTest extends TestCase {
36  
37      /**
38       * Test of integrator for the sine function.
39       */
40      public void testSinFunction() throws MathException {
41          UnivariateRealFunction f = new SinFunction();
42          UnivariateRealIntegrator integrator = new RombergIntegrator();
43          double min, max, expected, result, tolerance;
44  
45          min = 0; max = Math.PI; expected = 2;
46          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
47          result = integrator.integrate(f, min, max);
48          assertEquals(expected, result, tolerance);
49  
50          min = -Math.PI/3; max = 0; expected = -0.5;
51          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
52          result = integrator.integrate(f, min, max);
53          assertEquals(expected, result, tolerance);
54      }
55  
56      /**
57       * Test of integrator for the quintic function.
58       */
59      public void testQuinticFunction() throws MathException {
60          UnivariateRealFunction f = new QuinticFunction();
61          UnivariateRealIntegrator integrator = new RombergIntegrator();
62          double min, max, expected, result, tolerance;
63  
64          min = 0; max = 1; expected = -1.0/48;
65          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
66          result = integrator.integrate(f, min, max);
67          assertEquals(expected, result, tolerance);
68  
69          min = 0; max = 0.5; expected = 11.0/768;
70          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
71          result = integrator.integrate(f, min, max);
72          assertEquals(expected, result, tolerance);
73  
74          min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
75          tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
76          result = integrator.integrate(f, min, max);
77          assertEquals(expected, result, tolerance);
78      }
79  
80      /**
81       * Test of parameters for the integrator.
82       */
83      public void testParameters() throws Exception {
84          UnivariateRealFunction f = new SinFunction();
85          UnivariateRealIntegrator integrator = new RombergIntegrator();
86  
87          try {
88              // bad interval
89              integrator.integrate(f, 1, -1);
90              fail("Expecting IllegalArgumentException - bad interval");
91          } catch (IllegalArgumentException ex) {
92              // expected
93          }
94          try {
95              // bad iteration limits
96              integrator.setMinimalIterationCount(5);
97              integrator.setMaximalIterationCount(4);
98              integrator.integrate(f, -1, 1);
99              fail("Expecting IllegalArgumentException - bad iteration limits");
100         } catch (IllegalArgumentException ex) {
101             // expected
102         }
103         try {
104             // bad iteration limits
105             integrator.setMinimalIterationCount(10);
106             integrator.setMaximalIterationCount(50);
107             integrator.integrate(f, -1, 1);
108             fail("Expecting IllegalArgumentException - bad iteration limits");
109         } catch (IllegalArgumentException ex) {
110             // expected
111         }
112     }
113 }