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