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.math.analysis.interpolation;
18  
19  import org.apache.commons.math.MathRuntimeException;
20  import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
21  import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
22  
23  /**
24   * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set.
25   * <p>
26   * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction}
27   * consisting of n cubic polynomials, defined over the subintervals determined by the x values,  
28   * x[0] < x[i] ... < x[n].  The x values are referred to as "knot points."</p>
29   * <p>
30   * The value of the PolynomialSplineFunction at a point x that is greater than or equal to the smallest
31   * knot point and strictly less than the largest knot point is computed by finding the subinterval to which
32   * x belongs and computing the value of the corresponding polynomial at <code>x - x[i] </code> where
33   * <code>i</code> is the index of the subinterval.  See {@link PolynomialSplineFunction} for more details.
34   * </p>
35   * <p>
36   * The interpolating polynomials satisfy: <ol>
37   * <li>The value of the PolynomialSplineFunction at each of the input x values equals the 
38   *  corresponding y value.</li>
39   * <li>Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials 
40   *  "match up" at the knot points, as do their first and second derivatives).</li>
41   * </ol></p>
42   * <p>
43   * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires, 
44   * <u>Numerical Analysis</u>, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131.
45   * </p>
46   *
47   * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
48   *
49   */
50  public class SplineInterpolator implements UnivariateRealInterpolator {
51      
52      /**
53       * Computes an interpolating function for the data set.
54       * @param x the arguments for the interpolation points
55       * @param y the values for the interpolation points
56       * @return a function which interpolates the data set
57       */
58      public PolynomialSplineFunction interpolate(double x[], double y[]) {
59          if (x.length != y.length) {
60              throw MathRuntimeException.createIllegalArgumentException(
61                    "dimension mismatch {0} != {1}", x.length, y.length);
62          }
63          
64          if (x.length < 3) {
65              throw MathRuntimeException.createIllegalArgumentException(
66                    "{0} points are required, got only {1}", 3, x.length);
67          }
68          
69          // Number of intervals.  The number of data points is n + 1.
70          int n = x.length - 1;   
71          
72          for (int i = 0; i < n; i++) {
73              if (x[i]  >= x[i + 1]) {
74                  throw MathRuntimeException.createIllegalArgumentException(
75                        "points {0} and {1} are not strictly increasing ({2} >= {3})",
76                        i, i+1, x[i], x[i+1]);
77              }
78          }
79          
80          // Differences between knot points
81          double h[] = new double[n];
82          for (int i = 0; i < n; i++) {
83              h[i] = x[i + 1] - x[i];
84          }
85          
86          double mu[] = new double[n];
87          double z[] = new double[n + 1];
88          mu[0] = 0d;
89          z[0] = 0d;
90          double g = 0;
91          for (int i = 1; i < n; i++) {
92              g = 2d * (x[i+1]  - x[i - 1]) - h[i - 1] * mu[i -1];
93              mu[i] = h[i] / g;
94              z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) /
95                      (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g;
96          }
97         
98          // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
99          double b[] = new double[n];
100         double c[] = new double[n + 1];
101         double d[] = new double[n];
102         
103         z[n] = 0d;
104         c[n] = 0d;
105         
106         for (int j = n -1; j >=0; j--) {
107             c[j] = z[j] - mu[j] * c[j + 1];
108             b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
109             d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
110         }
111         
112         PolynomialFunction polynomials[] = new PolynomialFunction[n];
113         double coefficients[] = new double[4];
114         for (int i = 0; i < n; i++) {
115             coefficients[0] = y[i];
116             coefficients[1] = b[i];
117             coefficients[2] = c[i];
118             coefficients[3] = d[i];
119             polynomials[i] = new PolynomialFunction(coefficients);
120         }
121         
122         return new PolynomialSplineFunction(x, polynomials);
123     }
124 
125 }