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