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.core.MathArrays;
022import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
023import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
024import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
025import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
026
027/**
028 * Computes a clamped cubic spline interpolation for the data set.
029 * <p>
030 * The {@link #interpolate(double[], double[], double, double)} method returns a {@link PolynomialSplineFunction}
031 * consisting of n cubic polynomials, defined over the subintervals determined by the x values,
032 * {@code 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 * <li>The <i>clamped boundary condition</i>, i.e., the PolynomialSplineFunction takes "a specific direction" at both
046 * its start point and its end point by providing the desired first derivative values (slopes) as function parameters to
047 * {@link #interpolate(double[], double[], double, double)}.</li>
048 * </ol>
049 * <p>
050 * The clamped cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires,
051 * <u>Numerical Analysis</u>, 9th Ed., 2010, Cengage Learning, ISBN 0-538-73351-9, pp 153-156.
052 * </p>
053 *
054 */
055public class ClampedSplineInterpolator extends SplineInterpolator {
056    /**
057     * Computes an interpolating function for the data set.
058     * @param x the arguments for the interpolation points
059     * @param y the values for the interpolation points
060     * @param fpo first derivative at the starting point of the returned spline function (starting slope), satisfying
061     *            clamped boundary condition S′(x0) = f′(x0)
062     * @param fpn first derivative at the ending point of the returned spline function (ending slope), satisfying
063     *            clamped boundary condition S′(xn) = f′(xn)
064     * @return a function which interpolates the data set
065     * @throws DimensionMismatchException if {@code x} and {@code y}
066     * have different sizes.
067     * @throws NumberIsTooSmallException if the size of {@code x < 3}.
068     * @throws org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException
069     * if {@code x} is not sorted in strict increasing order.
070     */
071    public PolynomialSplineFunction interpolate(final double[] x, final double[] y,
072                                                final double fpo, final double fpn)
073            throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException {
074        if (x.length != y.length) {
075            throw new DimensionMismatchException(x.length, y.length);
076        }
077
078        if (x.length < 3) {
079            throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
080                                                x.length, 3, true);
081        }
082
083        // Number of intervals.  The number of data points is n + 1.
084        final int n = x.length - 1;
085
086        MathArrays.checkOrder(x);
087
088        // Differences between knot points
089        final double h[] = new double[n];
090        for (int i = 0; i < n; i++) {
091            h[i] = x[i + 1] - x[i];
092        }
093
094        final double mu[] = new double[n];
095        final double z[] = new double[n + 1];
096        final double alpha[] = new double[n + 1];
097        final double l[] = new double[n + 1];
098
099        alpha[0] = 3d * (y[1] - y[0]) / h[0] - 3d * fpo;
100        alpha[n] = 3d * fpn - 3d * (y[n] - y[n - 1]) / h[n - 1];
101
102        mu[0] = 0.5d;
103        l[0] = 2d * h[0];
104        z[0] = alpha[0] / l[0];
105
106        for (int i = 1; i < n; i++) {
107
108            alpha[i] = (3d / h[i]) * (y[i + 1] - y[i]) - (3d / h[i - 1]) * (y[i] - y[i - 1]);
109            l[i] = 2d * (x[i + 1] - x[i - 1]) - h[i - 1] * mu[i - 1];
110            mu[i] = h[i] / l[i];
111            z[i] = (alpha[i] - h[i - 1] * z[i - 1]) / l[i];
112        }
113        // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
114        final double b[] = new double[n];
115        final double c[] = new double[n + 1];
116        final double d[] = new double[n];
117        l[n] = h[n - 1] * (2d - mu[n - 1]);
118        z[n] = (alpha[n] - h[n - 1] * z[n - 1]) / l[n];
119        c[n] = z[n];
120
121        for (int j = n - 1; j >= 0; j--) {
122            c[j] = z[j] - mu[j] * c[j + 1];
123            b[j] = ((y[j + 1] - y[j]) / h[j]) - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
124            d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
125        }
126
127        final PolynomialFunction polynomials[] = new PolynomialFunction[n];
128        final double coefficients[] = new double[4];
129        for (int i = 0; i < n; i++) {
130            coefficients[0] = y[i];
131            coefficients[1] = b[i];
132            coefficients[2] = c[i];
133            coefficients[3] = d[i];
134            polynomials[i] = new PolynomialFunction(coefficients);
135        }
136        return new PolynomialSplineFunction(x, polynomials);
137    }
138}