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.math3.analysis.interpolation;

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

  25. /**
  26.  * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set.
  27.  * <p>
  28.  * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction}
  29.  * consisting of n cubic polynomials, defined over the subintervals determined by the x values,
  30.  * 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.  * </ol></p>
  44.  * <p>
  45.  * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires,
  46.  * <u>Numerical Analysis</u>, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131.
  47.  * </p>
  48.  *
  49.  */
  50. public class SplineInterpolator implements UnivariateInterpolator {
  51.     /**
  52.      * Computes an interpolating function for the data set.
  53.      * @param x the arguments for the interpolation points
  54.      * @param y the values for the interpolation points
  55.      * @return a function which interpolates the data set
  56.      * @throws DimensionMismatchException if {@code x} and {@code y}
  57.      * have different sizes.
  58.      * @throws NonMonotonicSequenceException if {@code x} is not sorted in
  59.      * strict increasing order.
  60.      * @throws NumberIsTooSmallException if the size of {@code x} is smaller
  61.      * than 3.
  62.      */
  63.     public PolynomialSplineFunction interpolate(double x[], double y[])
  64.         throws DimensionMismatchException,
  65.                NumberIsTooSmallException,
  66.                NonMonotonicSequenceException {
  67.         if (x.length != y.length) {
  68.             throw new DimensionMismatchException(x.length, y.length);
  69.         }

  70.         if (x.length < 3) {
  71.             throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
  72.                                                 x.length, 3, true);
  73.         }

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

  76.         MathArrays.checkOrder(x);

  77.         // Differences between knot points
  78.         final double h[] = new double[n];
  79.         for (int i = 0; i < n; i++) {
  80.             h[i] = x[i + 1] - x[i];
  81.         }

  82.         final double mu[] = new double[n];
  83.         final double z[] = new double[n + 1];
  84.         mu[0] = 0d;
  85.         z[0] = 0d;
  86.         double g = 0;
  87.         for (int i = 1; i < n; i++) {
  88.             g = 2d * (x[i+1]  - x[i - 1]) - h[i - 1] * mu[i -1];
  89.             mu[i] = h[i] / g;
  90.             z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) /
  91.                     (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g;
  92.         }

  93.         // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
  94.         final double b[] = new double[n];
  95.         final double c[] = new double[n + 1];
  96.         final double d[] = new double[n];

  97.         z[n] = 0d;
  98.         c[n] = 0d;

  99.         for (int j = n -1; j >=0; j--) {
  100.             c[j] = z[j] - mu[j] * c[j + 1];
  101.             b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
  102.             d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
  103.         }

  104.         final PolynomialFunction polynomials[] = new PolynomialFunction[n];
  105.         final double coefficients[] = new double[4];
  106.         for (int i = 0; i < n; i++) {
  107.             coefficients[0] = y[i];
  108.             coefficients[1] = b[i];
  109.             coefficients[2] = c[i];
  110.             coefficients[3] = d[i];
  111.             polynomials[i] = new PolynomialFunction(coefficients);
  112.         }

  113.         return new PolynomialSplineFunction(x, polynomials);
  114.     }
  115. }