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.CalculusFieldUnivariateFunction;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.util.Binary64;
27  import org.hipparchus.util.Binary64Field;
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 FieldRombergIntegratorTest {
45  
46      /**
47       * Test of integrator for the sine function.
48       */
49      @Test
50      void testSinFunction() {
51          FieldUnivariateIntegrator<Binary64> integrator = new FieldRombergIntegrator<>(Binary64Field.getInstance());
52  
53          Binary64 min = new Binary64(0);
54          Binary64 max = new Binary64(FastMath.PI);
55          double expected = 2;
56          double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
57          double result = integrator.integrate(100, x -> x.sin(), min, max).getReal();
58          assertTrue(integrator.getEvaluations() < 50);
59          assertTrue(integrator.getIterations()  < 10);
60          assertEquals(expected, result, tolerance);
61  
62          min = new Binary64(-FastMath.PI/3);
63          max = new Binary64(0);
64          expected = -0.5;
65          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
66          result = integrator.integrate(100, x -> x.sin(), min, max).getReal();
67          assertTrue(integrator.getEvaluations() < 50);
68          assertTrue(integrator.getIterations()  < 10);
69          assertEquals(expected, result, tolerance);
70      }
71  
72      /**
73       * Test of integrator for the quintic function.
74       */
75      @Test
76      void testQuinticFunction() {
77          CalculusFieldUnivariateFunction<Binary64> f =
78                          t -> t.subtract(1).multiply(t.subtract(0.5)).multiply(t).multiply(t.add(0.5)).multiply(t.add(1));
79          FieldUnivariateIntegrator<Binary64> integrator = new FieldRombergIntegrator<>(Binary64Field.getInstance());
80  
81          Binary64 min = new Binary64(0);
82          Binary64 max = new Binary64(1);
83          double expected = -1.0 / 48;
84          double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
85          double result = integrator.integrate(100, f, min, max).getReal();
86          assertTrue(integrator.getEvaluations() < 10);
87          assertTrue(integrator.getIterations()  < 5);
88          assertEquals(expected, result, tolerance);
89  
90          min = new Binary64(0);
91          max = new Binary64(0.5);
92          expected = 11.0 / 768;
93          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
94          result = integrator.integrate(100, f, min, max).getReal();
95          assertTrue(integrator.getEvaluations() < 10);
96          assertTrue(integrator.getIterations()  < 5);
97          assertEquals(expected, result, tolerance);
98  
99          min = new Binary64(-1);
100         max = new Binary64(4);
101         expected = 2048 / 3.0 - 78 + 1.0 / 48;
102         tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
103         result = integrator.integrate(100, f, min, max).getReal();
104         assertTrue(integrator.getEvaluations() < 10);
105         assertTrue(integrator.getIterations()  < 5);
106         assertEquals(expected, result, tolerance);
107     }
108 
109     /**
110      * Test of parameters for the integrator.
111      */
112     @Test
113     void testParameters() {
114 
115         try {
116             // bad interval
117             new FieldRombergIntegrator<>(Binary64Field.getInstance()).integrate(1000, x -> x.sin(),
118                                                                                  new Binary64(1), new Binary64(-1));
119             fail("Expecting MathIllegalArgumentException - bad interval");
120         } catch (MathIllegalArgumentException ex) {
121             // expected
122         }
123         try {
124             // bad iteration limits
125             new FieldRombergIntegrator<>(Binary64Field.getInstance(), 5, 4);
126             fail("Expecting MathIllegalArgumentException - bad iteration limits");
127         } catch (MathIllegalArgumentException ex) {
128             // expected
129         }
130         try {
131             // bad iteration limits
132             new FieldRombergIntegrator<>(Binary64Field.getInstance(), 10, 50);
133             fail("Expecting MathIllegalArgumentException - bad iteration limits");
134         } catch (MathIllegalArgumentException ex) {
135             // expected
136         }
137     }
138 }