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