SplineInterpolator.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.exception.DimensionMismatchException;
  21. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
  22. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
  23. import org.apache.commons.math4.legacy.core.MathArrays;

  24. /**
  25.  * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set.
  26.  * <p>
  27.  * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction}
  28.  * consisting of n cubic polynomials, defined over the subintervals determined by the x values,
  29.  * {@code x[0] < x[i] ... < x[n].}  The x values are referred to as "knot points."</p>
  30.  * <p>
  31.  * The value of the PolynomialSplineFunction at a point x that is greater than or equal to the smallest
  32.  * knot point and strictly less than the largest knot point is computed by finding the subinterval to which
  33.  * x belongs and computing the value of the corresponding polynomial at <code>x - x[i] </code> where
  34.  * <code>i</code> is the index of the subinterval.  See {@link PolynomialSplineFunction} for more details.
  35.  * </p>
  36.  * <p>
  37.  * The interpolating polynomials satisfy: <ol>
  38.  * <li>The value of the PolynomialSplineFunction at each of the input x values equals the
  39.  *  corresponding y value.</li>
  40.  * <li>Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials
  41.  *  "match up" at the knot points, as do their first and second derivatives).</li>
  42.  * </ol>
  43.  * <p>
  44.  * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires,
  45.  * <u>Numerical Analysis</u>, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131.
  46.  * </p>
  47.  *
  48.  */
  49. public class SplineInterpolator implements UnivariateInterpolator {
  50.     /**
  51.      * Computes an interpolating function for the data set.
  52.      * @param x the arguments for the interpolation points
  53.      * @param y the values for the interpolation points
  54.      * @return a function which interpolates the data set
  55.      * @throws DimensionMismatchException if {@code x} and {@code y}
  56.      * have different sizes.
  57.      * @throws NumberIsTooSmallException if the size of {@code x < 3}.
  58.      * @throws org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException
  59.      * if {@code x} is not sorted in strict increasing order.
  60.      */
  61.     @Override
  62.     public PolynomialSplineFunction interpolate(double[] x, double[] y) {
  63.         if (x.length != y.length) {
  64.             throw new DimensionMismatchException(x.length, y.length);
  65.         }

  66.         if (x.length < 3) {
  67.             throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
  68.                                                 x.length, 3, true);
  69.         }

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

  72.         MathArrays.checkOrder(x);

  73.         // Differences between knot points
  74.         final double[] h = new double[n];
  75.         for (int i = 0; i < n; i++) {
  76.             h[i] = x[i + 1] - x[i];
  77.         }

  78.         final double[] mu = new double[n];
  79.         final double[] z = new double[n + 1];
  80.         double g = 0;
  81.         int indexM1 = 0;
  82.         int index = 1;
  83.         int indexP1 = 2;
  84.         while (index < n) {
  85.             final double xIp1 = x[indexP1];
  86.             final double xIm1 = x[indexM1];
  87.             final double hIm1 = h[indexM1];
  88.             final double hI = h[index];
  89.             g = 2d * (xIp1 - xIm1) - hIm1 * mu[indexM1];
  90.             mu[index] = hI / g;
  91.             z[index] = (3d * (y[indexP1] * hIm1 - y[index] * (xIp1 - xIm1)+ y[indexM1] * hI) /
  92.                         (hIm1 * hI) - hIm1 * z[indexM1]) / g;

  93.             indexM1 = index;
  94.             index = indexP1;
  95.             indexP1 = indexP1 + 1;
  96.         }

  97.         // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
  98.         final double[] b = new double[n];
  99.         final double[] c = new double[n + 1];
  100.         final double[] d = new double[n];

  101.         for (int j = n - 1; j >= 0; j--) {
  102.             final double cJp1 = c[j + 1];
  103.             final double cJ = z[j] - mu[j] * cJp1;
  104.             final double hJ = h[j];
  105.             b[j] = (y[j + 1] - y[j]) / hJ - hJ * (cJp1 + 2d * cJ) / 3d;
  106.             c[j] = cJ;
  107.             d[j] = (cJp1 - cJ) / (3d * hJ);
  108.         }

  109.         final PolynomialFunction[] polynomials = new PolynomialFunction[n];
  110.         final double[] coefficients = new double[4];
  111.         for (int i = 0; i < n; i++) {
  112.             coefficients[0] = y[i];
  113.             coefficients[1] = b[i];
  114.             coefficients[2] = c[i];
  115.             coefficients[3] = d[i];
  116.             polynomials[i] = new PolynomialFunction(coefficients);
  117.         }

  118.         return new PolynomialSplineFunction(x, polynomials);
  119.     }
  120. }