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   * Test case for midpoint integrator.
34   * <p>
35   * Test runs show that for a default relative accuracy of 1E-6, it generally
36   * takes 10 to 15 iterations for the integral to converge.
37   *
38   */
39  public final class FieldMidPointIntegratorTest {
40  
41      /**
42       * Test of integrator for the sine function.
43       */
44      @Test
45      public void testLowAccuracy() {
46          CalculusFieldUnivariateFunction<Binary64> f =
47                          t -> t.subtract(1).multiply(t.subtract(0.5)).multiply(t).multiply(t.add(0.5)).multiply(t.add(1));
48          FieldUnivariateIntegrator<Binary64> integrator = new FieldMidPointIntegrator<>(Binary64Field.getInstance(), 0.01, 1.0e-10, 2, 4);
49  
50          Binary64 min = new Binary64(-10);
51          Binary64 max =  new Binary64(-9);
52          double expected = -3697001.0 / 48.0;
53          double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
54          double result = integrator.integrate(Integer.MAX_VALUE, f, min, max).getReal();
55          Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
56          Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
57          Assert.assertEquals(expected, result, tolerance);
58  
59      }
60  
61      /**
62       * Test of integrator for the sine function.
63       */
64      @Test
65      public void testSinFunction() {
66          FieldUnivariateIntegrator<Binary64> integrator = new FieldMidPointIntegrator<>(Binary64Field.getInstance());
67  
68          Binary64 min = new Binary64(0);
69          Binary64 max = new Binary64(FastMath.PI);
70          double expected = 2;
71          double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
72          double result = integrator.integrate(Integer.MAX_VALUE, x -> x.sin(), min, max).getReal();
73          Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
74          Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
75          Assert.assertEquals(expected, result, tolerance);
76  
77          min = new Binary64(-FastMath.PI/3);
78          max = new Binary64(0);
79          expected = -0.5;
80          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
81          result = integrator.integrate(Integer.MAX_VALUE, x -> x.sin(), min, max).getReal();
82          Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
83          Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
84          Assert.assertEquals(expected, result, tolerance);
85  
86      }
87  
88      /**
89       * Test of integrator for the quintic function.
90       */
91      @Test
92      public void testQuinticFunction() {
93          CalculusFieldUnivariateFunction<Binary64> f =
94                          t -> t.subtract(1).multiply(t.subtract(0.5)).multiply(t).multiply(t.add(0.5)).multiply(t.add(1));
95          FieldUnivariateIntegrator<Binary64> integrator = new FieldMidPointIntegrator<>(Binary64Field.getInstance());
96  
97          Binary64 min = new Binary64(0);
98          Binary64 max = new Binary64(1);
99          double expected = -1.0 / 48;
100         double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
101         double result = integrator.integrate(Integer.MAX_VALUE, f, min, max).getReal();
102         Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
103         Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
104         Assert.assertEquals(expected, result, tolerance);
105 
106         min = new Binary64(0);
107         max = new Binary64(0.5);
108         expected = 11.0 / 768;
109         tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
110         result = integrator.integrate(Integer.MAX_VALUE, f, min, max).getReal();
111         Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
112         Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
113         Assert.assertEquals(expected, result, tolerance);
114 
115         min = new Binary64(-1);
116         max = new Binary64(4);
117         expected = 2048 / 3.0 - 78 + 1.0 / 48;
118         tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
119         result = integrator.integrate(Integer.MAX_VALUE, f, min, max).getReal();
120         Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
121         Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
122         Assert.assertEquals(expected, result, tolerance);
123 
124     }
125 
126     /**
127      * Test of parameters for the integrator.
128      */
129     @Test
130     public void testParameters() {
131 
132         try {
133             // bad interval
134             new FieldMidPointIntegrator<>(Binary64Field.getInstance()).integrate(1000, x -> x.sin(),
135                                                                                   new Binary64(1), new Binary64(-1));
136             Assert.fail("Expecting MathIllegalArgumentException - bad interval");
137         } catch (MathIllegalArgumentException ex) {
138             // expected
139         }
140         try {
141             // bad iteration limits
142             new FieldMidPointIntegrator<>(Binary64Field.getInstance(), 5, 4);
143             Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
144         } catch (MathIllegalArgumentException ex) {
145             // expected
146         }
147         try {
148             // bad iteration limits
149             new FieldMidPointIntegrator<>(Binary64Field.getInstance(), 10, 99);
150             Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
151         } catch (MathIllegalArgumentException ex) {
152             // expected
153         }
154     }
155 }