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