1   /*
2    * 
3    * Copyright (c) 2004-2005 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: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $ 
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.0,f.value(0.0), interpolationTolerance);
71          assertEquals(0.4,f.value(0.4), interpolationTolerance);
72          assertEquals(1.0,f.value(1.0), interpolationTolerance);
73      }
74  
75      public void testInterpolateLinearDegenerateThreeSegment()
76          throws Exception {
77          double x[] = { 0.0, 0.5, 1.0, 1.5 };
78          double y[] = { 0.0, 0.5, 1.0, 1.5 };
79          UnivariateRealInterpolator i = new SplineInterpolator();
80          UnivariateRealFunction f = i.interpolate(x, y);
81          verifyInterpolation(f, x, y);
82          
83          // Verify coefficients using analytical values
84          PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
85          double target[] = {y[0], 1d, 0d, 0d};
86          TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
87          target = new double[]{y[1], 1d, 0d, 0d};
88          TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
89          target = new double[]{y[2], 1d, 0d, 0d};
90          TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance);
91          
92          // Check interpolation
93          assertEquals(0,f.value(0), interpolationTolerance);
94          assertEquals(1.4,f.value(1.4), interpolationTolerance);
95          assertEquals(1.5,f.value(1.5), interpolationTolerance);
96      }
97  
98      public void testInterpolateLinear() throws Exception {
99          double x[] = { 0.0, 0.5, 1.0 };
100         double y[] = { 0.0, 0.5, 0.0 };
101         UnivariateRealInterpolator i = new SplineInterpolator();
102         UnivariateRealFunction f = i.interpolate(x, y);
103         verifyInterpolation(f, x, y);
104         verifyConsistency((PolynomialSplineFunction) f, x);
105         
106         // Verify coefficients using analytical values
107         PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
108         double target[] = {y[0], 1.5d, 0d, -2d};
109         TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
110         target = new double[]{y[1], 0d, -3d, 2d};
111         TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);    
112     }
113     
114     public void testInterpolateSin() throws Exception {
115         double x[] =
116             {
117                 0.0,
118                 Math.PI / 6d,
119                 Math.PI / 2d,
120                 5d * Math.PI / 6d,
121                 Math.PI,
122                 7d * Math.PI / 6d,
123                 3d * Math.PI / 2d,
124                 11d * Math.PI / 6d,
125                 2.d * Math.PI };
126         double y[] = { 0d, 0.5d, 1d, 0.5d, 0d, -0.5d, -1d, -0.5d, 0d };
127         UnivariateRealInterpolator i = new SplineInterpolator();
128         UnivariateRealFunction f = i.interpolate(x, y);
129         verifyInterpolation(f, x, y);
130         verifyConsistency((PolynomialSplineFunction) f, x);
131         
132         /* Check coefficients against values computed using R (version 1.8.1, Red Hat Linux 9)
133          * 
134          * To replicate in R:
135          *     x[1] <- 0
136          *     x[2] <- pi / 6, etc, same for y[] (could use y <- scan() for y values)
137          *     g <- splinefun(x, y, "natural")
138          *     splinecoef <- eval(expression(z), envir = environment(g))
139          *     print(splinecoef) 
140          */
141         PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
142         double target[] = {y[0], 1.002676d, 0d, -0.17415829d};
143         TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
144         target = new double[]{y[1], 8.594367e-01, -2.735672e-01, -0.08707914};
145         TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
146         target = new double[]{y[2], 1.471804e-17,-5.471344e-01, 0.08707914};
147         TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance);
148         target = new double[]{y[3], -8.594367e-01, -2.735672e-01, 0.17415829};
149         TestUtils.assertEquals(polynomials[3].getCoefficients(), target, coefficientTolerance);
150         target = new double[]{y[4], -1.002676, 6.548562e-17, 0.17415829};
151         TestUtils.assertEquals(polynomials[4].getCoefficients(), target, coefficientTolerance);
152         target = new double[]{y[5], -8.594367e-01, 2.735672e-01, 0.08707914};
153         TestUtils.assertEquals(polynomials[5].getCoefficients(), target, coefficientTolerance);
154         target = new double[]{y[6], 3.466465e-16, 5.471344e-01, -0.08707914};
155         TestUtils.assertEquals(polynomials[6].getCoefficients(), target, coefficientTolerance);
156         target = new double[]{y[7], 8.594367e-01, 2.735672e-01, -0.17415829};
157         TestUtils.assertEquals(polynomials[7].getCoefficients(), target, coefficientTolerance); 
158         
159         //Check interpolation
160         assertEquals(Math.sqrt(2d) / 2d,f.value(Math.PI/4d),interpolationTolerance);
161         assertEquals(Math.sqrt(2d) / 2d,f.value(3d*Math.PI/4d),interpolationTolerance);     
162     }
163     
164 
165     public void testIllegalArguments() throws MathException {
166         // Data set arrays of different size.
167         UnivariateRealInterpolator i = new SplineInterpolator();
168         try {
169             double xval[] = { 0.0, 1.0 };
170             double yval[] = { 0.0, 1.0, 2.0 };
171             i.interpolate(xval, yval);
172             fail("Failed to detect data set array with different sizes.");
173         } catch (IllegalArgumentException iae) {
174         }
175         // X values not sorted.
176         try {
177             double xval[] = { 0.0, 1.0, 0.5 };
178             double yval[] = { 0.0, 1.0, 2.0 };
179             i.interpolate(xval, yval);
180             fail("Failed to detect unsorted arguments.");
181         } catch (IllegalArgumentException iae) {
182         }
183     }
184     
185     /**
186      * verifies that f(x[i]) = y[i] for i = 0..n-1 where n is common length.
187      */
188     protected void verifyInterpolation(UnivariateRealFunction f, double x[], double y[])  
189         throws Exception{
190         for (int i = 0; i < x.length; i++) {
191             assertEquals(f.value(x[i]), y[i], knotTolerance);
192         }     
193     }
194     
195     /**
196      * Verifies that interpolating polynomials satisfy consistency requirement:
197      *    adjacent polynomials must agree through two derivatives at knot points
198      */
199     protected void verifyConsistency(PolynomialSplineFunction f, double x[]) 
200         throws Exception {
201         PolynomialFunction polynomials[] = f.getPolynomials();
202         for (int i = 1; i < x.length - 2; i++) {
203             // evaluate polynomials and derivatives at x[i + 1]  
204             assertEquals(polynomials[i].value(x[i +1] - x[i]), polynomials[i + 1].value(0), 0.1); 
205             assertEquals(polynomials[i].derivative().value(x[i +1] - x[i]), 
206                     polynomials[i + 1].derivative().value(0), 0.5); 
207             assertEquals(polynomials[i].polynomialDerivative().derivative().value(x[i +1] - x[i]), 
208                     polynomials[i + 1].polynomialDerivative().derivative().value(0), 0.5); 
209         }
210     }
211     
212 }