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.interpolation;
23  
24  import org.hipparchus.analysis.UnivariateFunction;
25  import org.hipparchus.analysis.function.Expm1;
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.fail;
33  
34  
35  /**
36   * Test case for Divided Difference interpolator.
37   * <p>
38   * The error of polynomial interpolation is
39   *     f(z) - p(z) = f^(n)(zeta) * (z-x[0])(z-x[1])...(z-x[n-1]) / n!
40   * where f^(n) is the n-th derivative of the approximated function and
41   * zeta is some point in the interval determined by x[] and z.
42   * <p>
43   * Since zeta is unknown, f^(n)(zeta) cannot be calculated. But we can bound
44   * it and use the absolute value upper bound for estimates. For reference,
45   * see <b>Introduction to Numerical Analysis</b>, ISBN 038795452X, chapter 2.
46   *
47   */
48  final class DividedDifferenceInterpolatorTest {
49  
50      /**
51       * Test of interpolator for the sine function.
52       * <p>
53       * |sin^(n)(zeta)| &lt;= 1.0, zeta in [0, 2*PI]
54       */
55      @Test
56      void testSinFunction() {
57          UnivariateFunction f = new Sin();
58          UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();
59          double[] x;
60          double[] y;
61          double z;
62          double expected;
63          double result;
64          double tolerance;
65  
66          // 6 interpolating points on interval [0, 2*PI]
67          int n = 6;
68          double min = 0.0, max = 2 * FastMath.PI;
69          x = new double[n];
70          y = new double[n];
71          for (int i = 0; i < n; i++) {
72              x[i] = min + i * (max - min) / n;
73              y[i] = f.value(x[i]);
74          }
75          double derivativebound = 1.0;
76          UnivariateFunction p = interpolator.interpolate(x, y);
77  
78          z = FastMath.PI / 4; expected = f.value(z); result = p.value(z);
79          tolerance = FastMath.abs(derivativebound * partialerror(x, z));
80          assertEquals(expected, result, tolerance);
81  
82          z = FastMath.PI * 1.5; expected = f.value(z); result = p.value(z);
83          tolerance = FastMath.abs(derivativebound * partialerror(x, z));
84          assertEquals(expected, result, tolerance);
85      }
86  
87      /**
88       * Test of interpolator for the exponential function.
89       * <p>
90       * |expm1^(n)(zeta)| &lt;= e, zeta in [-1, 1]
91       */
92      @Test
93      void testExpm1Function() {
94          UnivariateFunction f = new Expm1();
95          UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();
96          double[] x;
97          double[] y;
98          double z;
99          double expected;
100         double result;
101         double tolerance;
102 
103         // 5 interpolating points on interval [-1, 1]
104         int n = 5;
105         double min = -1.0, max = 1.0;
106         x = new double[n];
107         y = new double[n];
108         for (int i = 0; i < n; i++) {
109             x[i] = min + i * (max - min) / n;
110             y[i] = f.value(x[i]);
111         }
112         double derivativebound = FastMath.E;
113         UnivariateFunction p = interpolator.interpolate(x, y);
114 
115         z = 0.0; expected = f.value(z); result = p.value(z);
116         tolerance = FastMath.abs(derivativebound * partialerror(x, z));
117         assertEquals(expected, result, tolerance);
118 
119         z = 0.5; expected = f.value(z); result = p.value(z);
120         tolerance = FastMath.abs(derivativebound * partialerror(x, z));
121         assertEquals(expected, result, tolerance);
122 
123         z = -0.5; expected = f.value(z); result = p.value(z);
124         tolerance = FastMath.abs(derivativebound * partialerror(x, z));
125         assertEquals(expected, result, tolerance);
126     }
127 
128     /**
129      * Test of parameters for the interpolator.
130      */
131     @Test
132     void testParameters() {
133         UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();
134 
135         try {
136             // bad abscissas array
137             double[] x = { 1.0, 2.0, 2.0, 4.0 };
138             double[] y = { 0.0, 4.0, 4.0, 2.5 };
139             UnivariateFunction p = interpolator.interpolate(x, y);
140             p.value(0.0);
141             fail("Expecting MathIllegalArgumentException - bad abscissas array");
142         } catch (MathIllegalArgumentException ex) {
143             // expected
144         }
145     }
146 
147     /**
148      * Returns the partial error term (z-x[0])(z-x[1])...(z-x[n-1])/n!
149      */
150     protected double partialerror(double[] x, double z) throws
151         IllegalArgumentException {
152 
153         if (x.length < 1) {
154             throw new IllegalArgumentException
155                 ("Interpolation array cannot be empty.");
156         }
157         double out = 1;
158         for (int i = 0; i < x.length; i++) {
159             out *= (z - x[i]) / (i + 1);
160         }
161         return out;
162     }
163 }