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