001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.analysis.interpolation;
018    
019    import org.apache.commons.math.MathRuntimeException;
020    import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
021    import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
022    
023    /**
024     * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set.
025     * <p>
026     * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction}
027     * consisting of n cubic polynomials, defined over the subintervals determined by the x values,
028     * x[0] < x[i] ... < x[n].  The x values are referred to as "knot points."</p>
029     * <p>
030     * The value of the PolynomialSplineFunction at a point x that is greater than or equal to the smallest
031     * knot point and strictly less than the largest knot point is computed by finding the subinterval to which
032     * x belongs and computing the value of the corresponding polynomial at <code>x - x[i] </code> where
033     * <code>i</code> is the index of the subinterval.  See {@link PolynomialSplineFunction} for more details.
034     * </p>
035     * <p>
036     * The interpolating polynomials satisfy: <ol>
037     * <li>The value of the PolynomialSplineFunction at each of the input x values equals the
038     *  corresponding y value.</li>
039     * <li>Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials
040     *  "match up" at the knot points, as do their first and second derivatives).</li>
041     * </ol></p>
042     * <p>
043     * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires,
044     * <u>Numerical Analysis</u>, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131.
045     * </p>
046     *
047     * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $
048     *
049     */
050    public class SplineInterpolator implements UnivariateRealInterpolator {
051    
052        /**
053         * Computes an interpolating function for the data set.
054         * @param x the arguments for the interpolation points
055         * @param y the values for the interpolation points
056         * @return a function which interpolates the data set
057         */
058        public PolynomialSplineFunction interpolate(double x[], double y[]) {
059            if (x.length != y.length) {
060                throw MathRuntimeException.createIllegalArgumentException(
061                      "dimension mismatch {0} != {1}", x.length, y.length);
062            }
063    
064            if (x.length < 3) {
065                throw MathRuntimeException.createIllegalArgumentException(
066                      "{0} points are required, got only {1}", 3, x.length);
067            }
068    
069            // Number of intervals.  The number of data points is n + 1.
070            int n = x.length - 1;
071    
072            for (int i = 0; i < n; i++) {
073                if (x[i]  >= x[i + 1]) {
074                    throw MathRuntimeException.createIllegalArgumentException(
075                          "points {0} and {1} are not strictly increasing ({2} >= {3})",
076                          i, i+1, x[i], x[i+1]);
077                }
078            }
079    
080            // Differences between knot points
081            double h[] = new double[n];
082            for (int i = 0; i < n; i++) {
083                h[i] = x[i + 1] - x[i];
084            }
085    
086            double mu[] = new double[n];
087            double z[] = new double[n + 1];
088            mu[0] = 0d;
089            z[0] = 0d;
090            double g = 0;
091            for (int i = 1; i < n; i++) {
092                g = 2d * (x[i+1]  - x[i - 1]) - h[i - 1] * mu[i -1];
093                mu[i] = h[i] / g;
094                z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) /
095                        (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g;
096            }
097    
098            // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
099            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    }