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.jupiter.api.Test;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  import static org.junit.jupiter.api.Assertions.fail;
34  
35  
36  /**
37   * Test case for Romberg integrator.
38   * <p>
39   * Romberg algorithm is very fast for good behavior integrand. Test runs
40   * show that for a default relative accuracy of 1E-6, it generally takes
41   * takes less than 5 iterations for the integral to converge.
42   *
43   */
44  final class RombergIntegratorTest {
45  
46      /**
47       * Test of integrator for the sine function.
48       */
49      @Test
50      void testSinFunction() {
51          UnivariateFunction f = new Sin();
52          UnivariateIntegrator integrator = new RombergIntegrator();
53          double min, max, expected, result, tolerance;
54  
55          min = 0; max = FastMath.PI; expected = 2;
56          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
57          result = integrator.integrate(100, f, min, max);
58          assertTrue(integrator.getEvaluations() < 50);
59          assertTrue(integrator.getIterations()  < 10);
60          assertEquals(expected, result, tolerance);
61  
62          min = -FastMath.PI/3; max = 0; expected = -0.5;
63          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
64          result = integrator.integrate(100, f, min, max);
65          assertTrue(integrator.getEvaluations() < 50);
66          assertTrue(integrator.getIterations()  < 10);
67          assertEquals(expected, result, tolerance);
68      }
69  
70      /**
71       * Test of integrator for the quintic function.
72       */
73      @Test
74      void testQuinticFunction() {
75          UnivariateFunction f = new QuinticFunction();
76          UnivariateIntegrator integrator = new RombergIntegrator();
77          double min, max, expected, result, tolerance;
78  
79          min = 0; max = 1; expected = -1.0/48;
80          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
81          result = integrator.integrate(100, f, min, max);
82          assertTrue(integrator.getEvaluations() < 10);
83          assertTrue(integrator.getIterations()  < 5);
84          assertEquals(expected, result, tolerance);
85  
86          min = 0; max = 0.5; expected = 11.0/768;
87          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
88          result = integrator.integrate(100, f, min, max);
89          assertTrue(integrator.getEvaluations() < 10);
90          assertTrue(integrator.getIterations()  < 5);
91          assertEquals(expected, result, tolerance);
92  
93          min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
94          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
95          result = integrator.integrate(100, f, min, max);
96          assertTrue(integrator.getEvaluations() < 10);
97          assertTrue(integrator.getIterations()  < 5);
98          assertEquals(expected, result, tolerance);
99      }
100 
101     /**
102      * Test of parameters for the integrator.
103      */
104     @Test
105     void testParameters() {
106         UnivariateFunction f = new Sin();
107 
108         try {
109             // bad interval
110             new RombergIntegrator().integrate(1000, f, 1, -1);
111             fail("Expecting MathIllegalArgumentException - bad interval");
112         } catch (MathIllegalArgumentException ex) {
113             // expected
114         }
115         try {
116             // bad iteration limits
117             new RombergIntegrator(5, 4);
118             fail("Expecting MathIllegalArgumentException - bad iteration limits");
119         } catch (MathIllegalArgumentException ex) {
120             // expected
121         }
122         try {
123             // bad iteration limits
124             new RombergIntegrator(10, 50);
125             fail("Expecting MathIllegalArgumentException - bad iteration limits");
126         } catch (MathIllegalArgumentException ex) {
127             // expected
128         }
129     }
130 }