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