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