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.math.analysis;
18  
19  import org.apache.commons.math.MathException;
20  import org.apache.commons.math.TestUtils;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  /**
27   * Test the SplineInterpolator.
28   *
29   * @version $Revision: 480442 $ $Date: 2006-11-29 00:21:22 -0700 (Wed, 29 Nov 2006) $ 
30   */
31  public class SplineInterpolatorTest extends TestCase {
32      
33      /** error tolerance for spline interpolator value at knot points */
34      protected double knotTolerance = 1E-12;
35     
36      /** error tolerance for interpolating polynomial coefficients */
37      protected double coefficientTolerance = 1E-6;
38      
39      /** error tolerance for interpolated values -- high value is from sin test */
40      protected double interpolationTolerance = 1E-2;
41  
42      public SplineInterpolatorTest(String name) {
43          super(name);
44      }
45  
46      public static Test suite() {
47          TestSuite suite = new TestSuite(SplineInterpolatorTest.class);
48          suite.setName("UnivariateRealInterpolator Tests");
49          return suite;
50      }
51  
52      public void testInterpolateLinearDegenerateTwoSegment()
53          throws Exception {
54          double x[] = { 0.0, 0.5, 1.0 };
55          double y[] = { 0.0, 0.5, 1.0 };
56          UnivariateRealInterpolator i = new SplineInterpolator();
57          UnivariateRealFunction f = i.interpolate(x, y);
58          verifyInterpolation(f, x, y);
59          verifyConsistency((PolynomialSplineFunction) f, x);
60          
61          // Verify coefficients using analytical values
62          PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
63          double target[] = {y[0], 1d, 0d, 0d};
64          TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
65          target = new double[]{y[1], 1d, 0d, 0d};
66          TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
67          
68          // Check interpolation
69          assertEquals(0.0,f.value(0.0), interpolationTolerance);
70          assertEquals(0.4,f.value(0.4), interpolationTolerance);
71          assertEquals(1.0,f.value(1.0), interpolationTolerance);
72      }
73  
74      public void testInterpolateLinearDegenerateThreeSegment()
75          throws Exception {
76          double x[] = { 0.0, 0.5, 1.0, 1.5 };
77          double y[] = { 0.0, 0.5, 1.0, 1.5 };
78          UnivariateRealInterpolator i = new SplineInterpolator();
79          UnivariateRealFunction f = i.interpolate(x, y);
80          verifyInterpolation(f, x, y);
81          
82          // Verify coefficients using analytical values
83          PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
84          double target[] = {y[0], 1d, 0d, 0d};
85          TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
86          target = new double[]{y[1], 1d, 0d, 0d};
87          TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
88          target = new double[]{y[2], 1d, 0d, 0d};
89          TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance);
90          
91          // Check interpolation
92          assertEquals(0,f.value(0), interpolationTolerance);
93          assertEquals(1.4,f.value(1.4), interpolationTolerance);
94          assertEquals(1.5,f.value(1.5), interpolationTolerance);
95      }
96  
97      public void testInterpolateLinear() throws Exception {
98          double x[] = { 0.0, 0.5, 1.0 };
99          double y[] = { 0.0, 0.5, 0.0 };
100         UnivariateRealInterpolator i = new SplineInterpolator();
101         UnivariateRealFunction f = i.interpolate(x, y);
102         verifyInterpolation(f, x, y);
103         verifyConsistency((PolynomialSplineFunction) f, x);
104         
105         // Verify coefficients using analytical values
106         PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
107         double target[] = {y[0], 1.5d, 0d, -2d};
108         TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
109         target = new double[]{y[1], 0d, -3d, 2d};
110         TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);    
111     }
112     
113     public void testInterpolateSin() throws Exception {
114         double x[] =
115             {
116                 0.0,
117                 Math.PI / 6d,
118                 Math.PI / 2d,
119                 5d * Math.PI / 6d,
120                 Math.PI,
121                 7d * Math.PI / 6d,
122                 3d * Math.PI / 2d,
123                 11d * Math.PI / 6d,
124                 2.d * Math.PI };
125         double y[] = { 0d, 0.5d, 1d, 0.5d, 0d, -0.5d, -1d, -0.5d, 0d };
126         UnivariateRealInterpolator i = new SplineInterpolator();
127         UnivariateRealFunction f = i.interpolate(x, y);
128         verifyInterpolation(f, x, y);
129         verifyConsistency((PolynomialSplineFunction) f, x);
130         
131         /* Check coefficients against values computed using R (version 1.8.1, Red Hat Linux 9)
132          * 
133          * To replicate in R:
134          *     x[1] <- 0
135          *     x[2] <- pi / 6, etc, same for y[] (could use y <- scan() for y values)
136          *     g <- splinefun(x, y, "natural")
137          *     splinecoef <- eval(expression(z), envir = environment(g))
138          *     print(splinecoef) 
139          */
140         PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
141         double target[] = {y[0], 1.002676d, 0d, -0.17415829d};
142         TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
143         target = new double[]{y[1], 8.594367e-01, -2.735672e-01, -0.08707914};
144         TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
145         target = new double[]{y[2], 1.471804e-17,-5.471344e-01, 0.08707914};
146         TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance);
147         target = new double[]{y[3], -8.594367e-01, -2.735672e-01, 0.17415829};
148         TestUtils.assertEquals(polynomials[3].getCoefficients(), target, coefficientTolerance);
149         target = new double[]{y[4], -1.002676, 6.548562e-17, 0.17415829};
150         TestUtils.assertEquals(polynomials[4].getCoefficients(), target, coefficientTolerance);
151         target = new double[]{y[5], -8.594367e-01, 2.735672e-01, 0.08707914};
152         TestUtils.assertEquals(polynomials[5].getCoefficients(), target, coefficientTolerance);
153         target = new double[]{y[6], 3.466465e-16, 5.471344e-01, -0.08707914};
154         TestUtils.assertEquals(polynomials[6].getCoefficients(), target, coefficientTolerance);
155         target = new double[]{y[7], 8.594367e-01, 2.735672e-01, -0.17415829};
156         TestUtils.assertEquals(polynomials[7].getCoefficients(), target, coefficientTolerance); 
157         
158         //Check interpolation
159         assertEquals(Math.sqrt(2d) / 2d,f.value(Math.PI/4d),interpolationTolerance);
160         assertEquals(Math.sqrt(2d) / 2d,f.value(3d*Math.PI/4d),interpolationTolerance);     
161     }
162     
163 
164     public void testIllegalArguments() throws MathException {
165         // Data set arrays of different size.
166         UnivariateRealInterpolator i = new SplineInterpolator();
167         try {
168             double xval[] = { 0.0, 1.0 };
169             double yval[] = { 0.0, 1.0, 2.0 };
170             i.interpolate(xval, yval);
171             fail("Failed to detect data set array with different sizes.");
172         } catch (IllegalArgumentException iae) {
173         }
174         // X values not sorted.
175         try {
176             double xval[] = { 0.0, 1.0, 0.5 };
177             double yval[] = { 0.0, 1.0, 2.0 };
178             i.interpolate(xval, yval);
179             fail("Failed to detect unsorted arguments.");
180         } catch (IllegalArgumentException iae) {
181         }
182     }
183     
184     /**
185      * verifies that f(x[i]) = y[i] for i = 0..n-1 where n is common length.
186      */
187     protected void verifyInterpolation(UnivariateRealFunction f, double x[], double y[])  
188         throws Exception{
189         for (int i = 0; i < x.length; i++) {
190             assertEquals(f.value(x[i]), y[i], knotTolerance);
191         }     
192     }
193     
194     /**
195      * Verifies that interpolating polynomials satisfy consistency requirement:
196      *    adjacent polynomials must agree through two derivatives at knot points
197      */
198     protected void verifyConsistency(PolynomialSplineFunction f, double x[]) 
199         throws Exception {
200         PolynomialFunction polynomials[] = f.getPolynomials();
201         for (int i = 1; i < x.length - 2; i++) {
202             // evaluate polynomials and derivatives at x[i + 1]  
203             assertEquals(polynomials[i].value(x[i +1] - x[i]), polynomials[i + 1].value(0), 0.1); 
204             assertEquals(polynomials[i].derivative().value(x[i +1] - x[i]), 
205                     polynomials[i + 1].derivative().value(0), 0.5); 
206             assertEquals(polynomials[i].polynomialDerivative().derivative().value(x[i +1] - x[i]), 
207                     polynomials[i + 1].polynomialDerivative().derivative().value(0), 0.5); 
208         }
209     }
210     
211 }