ClampedSplineInterpolator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.analysis.interpolation;

  18. import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
  19. import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialSplineFunction;
  20. import org.apache.commons.math4.legacy.core.MathArrays;
  21. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  22. import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
  23. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
  24. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;

  25. /**
  26.  * Computes a clamped cubic spline interpolation for the data set.
  27.  * <p>
  28.  * The {@link #interpolate(double[], double[], double, double)} method returns a {@link PolynomialSplineFunction}
  29.  * consisting of n cubic polynomials, defined over the subintervals determined by the x values,
  30.  * {@code x[0] < x[i] ... < x[n]}.  The x values are referred to as "knot points."</p>
  31.  * <p>
  32.  * The value of the PolynomialSplineFunction at a point x that is greater than or equal to the smallest
  33.  * knot point and strictly less than the largest knot point is computed by finding the subinterval to which
  34.  * x belongs and computing the value of the corresponding polynomial at <code>x - x[i]</code> where
  35.  * <code>i</code> is the index of the subinterval. See {@link PolynomialSplineFunction} for more details.
  36.  * </p>
  37.  * <p>
  38.  * The interpolating polynomials satisfy: <ol>
  39.  * <li>The value of the PolynomialSplineFunction at each of the input x values equals the
  40.  *  corresponding y value.</li>
  41.  * <li>Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials
  42.  *  "match up" at the knot points, as do their first and second derivatives).</li>
  43.  * <li>The <i>clamped boundary condition</i>, i.e., the PolynomialSplineFunction takes "a specific direction" at both
  44.  * its start point and its end point by providing the desired first derivative values (slopes) as function parameters to
  45.  * {@link #interpolate(double[], double[], double, double)}.</li>
  46.  * </ol>
  47.  * <p>
  48.  * The clamped cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires,
  49.  * <u>Numerical Analysis</u>, 9th Ed., 2010, Cengage Learning, ISBN 0-538-73351-9, pp 153-156.
  50.  * </p>
  51.  *
  52.  */
  53. public class ClampedSplineInterpolator extends SplineInterpolator {
  54.     /**
  55.      * Computes an interpolating function for the data set.
  56.      * @param x the arguments for the interpolation points
  57.      * @param y the values for the interpolation points
  58.      * @param fpo first derivative at the starting point of the returned spline function (starting slope), satisfying
  59.      *            clamped boundary condition S′(x0) = f′(x0)
  60.      * @param fpn first derivative at the ending point of the returned spline function (ending slope), satisfying
  61.      *            clamped boundary condition S′(xn) = f′(xn)
  62.      * @return a function which interpolates the data set
  63.      * @throws DimensionMismatchException if {@code x} and {@code y}
  64.      * have different sizes.
  65.      * @throws NumberIsTooSmallException if the size of {@code x < 3}.
  66.      * @throws org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException
  67.      * if {@code x} is not sorted in strict increasing order.
  68.      */
  69.     public PolynomialSplineFunction interpolate(final double[] x, final double[] y,
  70.                                                 final double fpo, final double fpn)
  71.             throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException {
  72.         if (x.length != y.length) {
  73.             throw new DimensionMismatchException(x.length, y.length);
  74.         }

  75.         if (x.length < 3) {
  76.             throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
  77.                                                 x.length, 3, true);
  78.         }

  79.         // Number of intervals.  The number of data points is n + 1.
  80.         final int n = x.length - 1;

  81.         MathArrays.checkOrder(x);

  82.         // Differences between knot points
  83.         final double h[] = new double[n];
  84.         for (int i = 0; i < n; i++) {
  85.             h[i] = x[i + 1] - x[i];
  86.         }

  87.         final double mu[] = new double[n];
  88.         final double z[] = new double[n + 1];
  89.         final double alpha[] = new double[n + 1];
  90.         final double l[] = new double[n + 1];

  91.         alpha[0] = 3d * (y[1] - y[0]) / h[0] - 3d * fpo;
  92.         alpha[n] = 3d * fpn - 3d * (y[n] - y[n - 1]) / h[n - 1];

  93.         mu[0] = 0.5d;
  94.         l[0] = 2d * h[0];
  95.         z[0] = alpha[0] / l[0];

  96.         for (int i = 1; i < n; i++) {

  97.             alpha[i] = (3d / h[i]) * (y[i + 1] - y[i]) - (3d / h[i - 1]) * (y[i] - y[i - 1]);
  98.             l[i] = 2d * (x[i + 1] - x[i - 1]) - h[i - 1] * mu[i - 1];
  99.             mu[i] = h[i] / l[i];
  100.             z[i] = (alpha[i] - h[i - 1] * z[i - 1]) / l[i];
  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.         l[n] = h[n - 1] * (2d - mu[n - 1]);
  107.         z[n] = (alpha[n] - h[n - 1] * z[n - 1]) / l[n];
  108.         c[n] = z[n];

  109.         for (int j = n - 1; j >= 0; j--) {
  110.             c[j] = z[j] - mu[j] * c[j + 1];
  111.             b[j] = ((y[j + 1] - y[j]) / h[j]) - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
  112.             d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
  113.         }

  114.         final PolynomialFunction polynomials[] = new PolynomialFunction[n];
  115.         final 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.         return new PolynomialSplineFunction(x, polynomials);
  124.     }
  125. }