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   * 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 MidPointIntegratorTest {
40  
41      /**
42       * Test of integrator for the sine function.
43       */
44      @Test
45      public void testLowAccuracy() {
46          UnivariateFunction f = new QuinticFunction();
47          UnivariateIntegrator integrator = new MidPointIntegrator(0.01, 1.0e-10, 2, 4);
48  
49          double min = -10;
50          double max =  -9;
51          double expected = -3697001.0 / 48.0;
52          double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
53          double result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
54          Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
55          Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
56          Assert.assertEquals(expected, result, tolerance);
57  
58      }
59  
60      /**
61       * Test of integrator for the sine function.
62       */
63      @Test
64      public void testSinFunction() {
65          UnivariateFunction f = new Sin();
66          UnivariateIntegrator integrator = new MidPointIntegrator();
67  
68          double min = 0;
69          double max = FastMath.PI;
70          double expected = 2;
71          double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
72          double result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
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 = -FastMath.PI/3;
78          max = 0;
79          expected = -0.5;
80          tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
81          result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
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          UnivariateFunction f = new QuinticFunction();
94          UnivariateIntegrator integrator = new MidPointIntegrator();
95  
96          double min = 0;
97          double max = 1;
98          double expected = -1.0 / 48;
99          double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
100         double result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
101         Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
102         Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
103         Assert.assertEquals(expected, result, tolerance);
104 
105         min = 0;
106         max = 0.5;
107         expected = 11.0 / 768;
108         tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
109         result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
110         Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
111         Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
112         Assert.assertEquals(expected, result, tolerance);
113 
114         min = -1;
115         max = 4;
116         expected = 2048 / 3.0 - 78 + 1.0 / 48;
117         tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
118         result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
119         Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
120         Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
121         Assert.assertEquals(expected, result, tolerance);
122 
123     }
124 
125     /**
126      * Test of parameters for the integrator.
127      */
128     @Test
129     public void testParameters() {
130         UnivariateFunction f = new Sin();
131 
132         try {
133             // bad interval
134             new MidPointIntegrator().integrate(1000, f, 1, -1);
135             Assert.fail("Expecting MathIllegalArgumentException - bad interval");
136         } catch (MathIllegalArgumentException ex) {
137             // expected
138         }
139         try {
140             // bad iteration limits
141             new MidPointIntegrator(5, 4);
142             Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
143         } catch (MathIllegalArgumentException ex) {
144             // expected
145         }
146         try {
147             // bad iteration limits
148             new MidPointIntegrator(10, 99);
149             Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
150         } catch (MathIllegalArgumentException ex) {
151             // expected
152         }
153     }
154 }