    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.exception.DimensionMismatchException;
    020    import org.apache.commons.math.exception.util.LocalizedFormats;
    021    import org.apache.commons.math.exception.NumberIsTooSmallException;
    022    import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
    023    import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
    024    import org.apache.commons.math.util.MathUtils;
    025
    026    /**
    027     * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set.
    028     * <p>
    029     * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction}
    030     * consisting of n cubic polynomials, defined over the subintervals determined by the x values,
    031     * x[0] < x[i] ... < x[n].  The x values are referred to as "knot points."</p>
    032     * <p>
    033     * The value of the PolynomialSplineFunction at a point x that is greater than or equal to the smallest
    034     * knot point and strictly less than the largest knot point is computed by finding the subinterval to which
    035     * x belongs and computing the value of the corresponding polynomial at <code>x - x[i] </code> where
    036     * <code>i</code> is the index of the subinterval.  See {@link PolynomialSplineFunction} for more details.
    037     * </p>
    038     * <p>
    039     * The interpolating polynomials satisfy: <ol>
    040     * <li>The value of the PolynomialSplineFunction at each of the input x values equals the
    041     *  corresponding y value.</li>
    042     * <li>Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials
    043     *  "match up" at the knot points, as do their first and second derivatives).</li>
    044     * </ol></p>
    045     * <p>
    046     * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires,
    047     * <u>Numerical Analysis</u>, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131.
    048     * </p>
    049     *
    050     * @version $Revision: 983921 $ $Date: 2010-08-10 12:46:06 +0200 (mar. 10 ao??t 2010) $
    051     *
    052     */
    053    public class SplineInterpolator implements UnivariateRealInterpolator {
    054
    055        /**
    056         * Computes an interpolating function for the data set.
    057         * @param x the arguments for the interpolation points
    058         * @param y the values for the interpolation points
    059         * @return a function which interpolates the data set
    060         * @throws DimensionMismatchException if {@code x} and {@code y}
    061         * have different sizes.
    062         * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
    063         * if {@code x} is not sorted in strict increasing order.
    064         * @throws NumberIsTooSmallException if the size of {@code x} is smaller
    065         * than 3.
    066         */
    067        public PolynomialSplineFunction interpolate(double x[], double y[]) {
    068            if (x.length != y.length) {
    069                throw new DimensionMismatchException(x.length, y.length);
    070            }
    071
    072            if (x.length < 3) {
    073                throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
    074                                                    x.length, 3, true);
    075            }
    076
    077            // Number of intervals.  The number of data points is n + 1.
    078            int n = x.length - 1;
    079
    080            MathUtils.checkOrder(x);
    081
    082            // Differences between knot points
    083            double h[] = new double[n];
    084            for (int i = 0; i < n; i++) {
    085                h[i] = x[i + 1] - x[i];
    086            }
    087
    088            double mu[] = new double[n];
    089            double z[] = new double[n + 1];
    090            mu[0] = 0d;
    091            z[0] = 0d;
    092            double g = 0;
    093            for (int i = 1; i < n; i++) {
    094                g = 2d * (x[i+1]  - x[i - 1]) - h[i - 1] * mu[i -1];
    095                mu[i] = h[i] / g;
    096                z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) /
    097                        (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g;
    098            }
    099
    100            // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
    101            double b[] = new double[n];
    102            double c[] = new double[n + 1];
    103            double d[] = new double[n];
    104
    105            z[n] = 0d;
    106            c[n] = 0d;
    107
    108            for (int j = n -1; j >=0; j--) {
    109                c[j] = z[j] - mu[j] * c[j + 1];
    110                b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
    111                d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
    112            }
    113
    114            PolynomialFunction polynomials[] = new PolynomialFunction[n];
    115            double coefficients[] = new double[4];
    116            for (int i = 0; i < n; i++) {
    117                coefficients[0] = y[i];
    118                coefficients[1] = b[i];
    119                coefficients[2] = c[i];
    120                coefficients[3] = d[i];
    121                polynomials[i] = new PolynomialFunction(coefficients);
    122            }
    123
    124            return new PolynomialSplineFunction(x, polynomials);
    125        }
    126
    127    }