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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.analysis.integration;
23  
24  import org.hipparchus.analysis.QuinticFunction;
25  import org.hipparchus.analysis.UnivariateFunction;
26  import org.hipparchus.analysis.function.Sin;
27  import org.hipparchus.exception.MathIllegalArgumentException;
28  import org.hipparchus.util.FastMath;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  
33  /**
34   * Test case for Romberg integrator.
35   * <p>
36   * Romberg algorithm is very fast for good behavior integrand. Test runs
37   * show that for a default relative accuracy of 1E-6, it generally takes
38   * takes less than 5 iterations for the integral to converge.
39   *
40   */
41  public final class RombergIntegratorTest {
42  
43      /**
44       * Test of integrator for the sine function.
45       */
46      @Test
47      public void testSinFunction() {
48          UnivariateFunction f = new Sin();
49          UnivariateIntegrator integrator = new RombergIntegrator();
50          double min, max, expected, result, tolerance;
51  
52          min = 0; max = FastMath.PI; expected = 2;
53          tolerance = FastMath.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 = -FastMath.PI/3; max = 0; expected = -0.5;
60          tolerance = FastMath.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, max, expected, result, tolerance;
75  
76          min = 0; max = 1; expected = -1.0/48;
77          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
78          result = integrator.integrate(100, f, min, max);
79          Assert.assertTrue(integrator.getEvaluations() < 10);
80          Assert.assertTrue(integrator.getIterations()  < 5);
81          Assert.assertEquals(expected, result, tolerance);
82  
83          min = 0; max = 0.5; expected = 11.0/768;
84          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
85          result = integrator.integrate(100, f, min, max);
86          Assert.assertTrue(integrator.getEvaluations() < 10);
87          Assert.assertTrue(integrator.getIterations()  < 5);
88          Assert.assertEquals(expected, result, tolerance);
89  
90          min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
91          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
92          result = integrator.integrate(100, f, min, max);
93          Assert.assertTrue(integrator.getEvaluations() < 10);
94          Assert.assertTrue(integrator.getIterations()  < 5);
95          Assert.assertEquals(expected, result, tolerance);
96      }
97  
98      /**
99       * Test of parameters for the integrator.
100      */
101     @Test
102     public void testParameters() {
103         UnivariateFunction f = new Sin();
104 
105         try {
106             // bad interval
107             new RombergIntegrator().integrate(1000, f, 1, -1);
108             Assert.fail("Expecting MathIllegalArgumentException - bad interval");
109         } catch (MathIllegalArgumentException ex) {
110             // expected
111         }
112         try {
113             // bad iteration limits
114             new RombergIntegrator(5, 4);
115             Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
116         } catch (MathIllegalArgumentException ex) {
117             // expected
118         }
119         try {
120             // bad iteration limits
121             new RombergIntegrator(10, 50);
122             Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
123         } catch (MathIllegalArgumentException ex) {
124             // expected
125         }
126     }
127 }