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