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 */
017package org.apache.commons.math4.legacy.analysis.interpolation;
018
019import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
020import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialSplineFunction;
021import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
022import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
023import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
024import org.apache.commons.math4.legacy.core.MathArrays;
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 * {@code 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>
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 */
051public class SplineInterpolator implements UnivariateInterpolator {
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     * @throws DimensionMismatchException if {@code x} and {@code y}
058     * have different sizes.
059     * @throws NumberIsTooSmallException if the size of {@code x < 3}.
060     * @throws org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException
061     * if {@code x} is not sorted in strict increasing order.
062     */
063    @Override
064    public PolynomialSplineFunction interpolate(double[] x, double[] y) {
065        if (x.length != y.length) {
066            throw new DimensionMismatchException(x.length, y.length);
067        }
068
069        if (x.length < 3) {
070            throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
071                                                x.length, 3, true);
072        }
073
074        // Number of intervals.  The number of data points is n + 1.
075        final int n = x.length - 1;
076
077        MathArrays.checkOrder(x);
078
079        // Differences between knot points
080        final double[] h = new double[n];
081        for (int i = 0; i < n; i++) {
082            h[i] = x[i + 1] - x[i];
083        }
084
085        final double[] mu = new double[n];
086        final double[] z = new double[n + 1];
087        double g = 0;
088        int indexM1 = 0;
089        int index = 1;
090        int indexP1 = 2;
091        while (index < n) {
092            final double xIp1 = x[indexP1];
093            final double xIm1 = x[indexM1];
094            final double hIm1 = h[indexM1];
095            final double hI = h[index];
096            g = 2d * (xIp1 - xIm1) - hIm1 * mu[indexM1];
097            mu[index] = hI / g;
098            z[index] = (3d * (y[indexP1] * hIm1 - y[index] * (xIp1 - xIm1)+ y[indexM1] * hI) /
099                        (hIm1 * hI) - hIm1 * z[indexM1]) / g;
100
101            indexM1 = index;
102            index = indexP1;
103            indexP1 = indexP1 + 1;
104        }
105
106        // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
107        final double[] b = new double[n];
108        final double[] c = new double[n + 1];
109        final double[] d = new double[n];
110
111        for (int j = n - 1; j >= 0; j--) {
112            final double cJp1 = c[j + 1];
113            final double cJ = z[j] - mu[j] * cJp1;
114            final double hJ = h[j];
115            b[j] = (y[j + 1] - y[j]) / hJ - hJ * (cJp1 + 2d * cJ) / 3d;
116            c[j] = cJ;
117            d[j] = (cJp1 - cJ) / (3d * hJ);
118        }
119
120        final PolynomialFunction[] polynomials = new PolynomialFunction[n];
121        final double[] coefficients = new double[4];
122        for (int i = 0; i < n; i++) {
123            coefficients[0] = y[i];
124            coefficients[1] = b[i];
125            coefficients[2] = c[i];
126            coefficients[3] = d[i];
127            polynomials[i] = new PolynomialFunction(coefficients);
128        }
129
130        return new PolynomialSplineFunction(x, polynomials);
131    }
132}