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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.analysis.interpolation;

  22. import java.lang.reflect.Array;

  23. import org.hipparchus.Field;
  24. import org.hipparchus.CalculusFieldElement;
  25. import org.hipparchus.analysis.polynomials.FieldPolynomialFunction;
  26. import org.hipparchus.analysis.polynomials.FieldPolynomialSplineFunction;
  27. import org.hipparchus.analysis.polynomials.PolynomialFunction;
  28. import org.hipparchus.analysis.polynomials.PolynomialSplineFunction;
  29. import org.hipparchus.exception.LocalizedCoreFormats;
  30. import org.hipparchus.exception.MathIllegalArgumentException;
  31. import org.hipparchus.util.MathArrays;
  32. import org.hipparchus.util.MathUtils;

  33. /**
  34.  * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set.
  35.  * <p>
  36.  * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction}
  37.  * consisting of n cubic polynomials, defined over the subintervals determined by the x values,
  38.  * {@code x[0] < x[i] ... < x[n].}  The x values are referred to as "knot points."</p>
  39.  * <p>
  40.  * The value of the PolynomialSplineFunction at a point x that is greater than or equal to the smallest
  41.  * knot point and strictly less than the largest knot point is computed by finding the subinterval to which
  42.  * x belongs and computing the value of the corresponding polynomial at <code>x - x[i] </code> where
  43.  * <code>i</code> is the index of the subinterval.  See {@link PolynomialSplineFunction} for more details.
  44.  * </p>
  45.  * <p>
  46.  * The interpolating polynomials satisfy:
  47.  * </p>
  48.  * <ol>
  49.  * <li>The value of the PolynomialSplineFunction at each of the input x values equals the
  50.  *  corresponding y value.</li>
  51.  * <li>Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials
  52.  *  "match up" at the knot points, as do their first and second derivatives).</li>
  53.  * </ol>
  54.  * <p>
  55.  * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires,
  56.  * <u>Numerical Analysis</u>, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131.
  57.  * </p>
  58.  *
  59.  */
  60. public class SplineInterpolator implements UnivariateInterpolator, FieldUnivariateInterpolator {

  61.     /** Empty constructor.
  62.      * <p>
  63.      * This constructor is not strictly necessary, but it prevents spurious
  64.      * javadoc warnings with JDK 18 and later.
  65.      * </p>
  66.      * @since 3.0
  67.      */
  68.     public SplineInterpolator() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  69.         // nothing to do
  70.     }

  71.     /**
  72.      * Computes an interpolating function for the data set.
  73.      * @param x the arguments for the interpolation points
  74.      * @param y the values for the interpolation points
  75.      * @return a function which interpolates the data set
  76.      * @throws MathIllegalArgumentException if {@code x} and {@code y}
  77.      * have different sizes.
  78.      * @throws MathIllegalArgumentException if {@code x} is not sorted in
  79.      * strict increasing order.
  80.      * @throws MathIllegalArgumentException if the size of {@code x} is smaller
  81.      * than 3.
  82.      */
  83.     @Override
  84.     public PolynomialSplineFunction interpolate(double[] x, double[] y)
  85.         throws MathIllegalArgumentException {

  86.         MathUtils.checkNotNull(x);
  87.         MathUtils.checkNotNull(y);
  88.         MathArrays.checkEqualLength(x, y);
  89.         if (x.length < 3) {
  90.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_POINTS,
  91.                                                    x.length, 3, true);
  92.         }

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

  95.         MathArrays.checkOrder(x);

  96.         // Differences between knot points
  97.         final double[] h = new double[n];
  98.         for (int i = 0; i < n; i++) {
  99.             h[i] = x[i + 1] - x[i];
  100.         }

  101.         final double[] mu = new double[n];
  102.         final double[] z = new double[n + 1];
  103.         mu[0] = 0d;
  104.         z[0] = 0d;
  105.         double g;
  106.         for (int i = 1; i < n; i++) {
  107.             g = 2d * (x[i+1]  - x[i - 1]) - h[i - 1] * mu[i -1];
  108.             mu[i] = h[i] / g;
  109.             z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) /
  110.                     (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g;
  111.         }

  112.         // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
  113.         final double[] b = new double[n];
  114.         final double[] c = new double[n + 1];
  115.         final double[] d = new double[n];

  116.         z[n] = 0d;
  117.         c[n] = 0d;

  118.         for (int j = n -1; j >=0; j--) {
  119.             c[j] = z[j] - mu[j] * c[j + 1];
  120.             b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
  121.             d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
  122.         }

  123.         final PolynomialFunction[] polynomials = new PolynomialFunction[n];
  124.         final double[] coefficients = new double[4];
  125.         for (int i = 0; i < n; i++) {
  126.             coefficients[0] = y[i];
  127.             coefficients[1] = b[i];
  128.             coefficients[2] = c[i];
  129.             coefficients[3] = d[i];
  130.             polynomials[i] = new PolynomialFunction(coefficients);
  131.         }

  132.         return new PolynomialSplineFunction(x, polynomials);
  133.     }

  134.     /**
  135.      * Computes an interpolating function for the data set.
  136.      * @param x the arguments for the interpolation points
  137.      * @param y the values for the interpolation points
  138.      * @param <T> the type of the field elements
  139.      * @return a function which interpolates the data set
  140.      * @throws MathIllegalArgumentException if {@code x} and {@code y}
  141.      * have different sizes.
  142.      * @throws MathIllegalArgumentException if {@code x} is not sorted in
  143.      * strict increasing order.
  144.      * @throws MathIllegalArgumentException if the size of {@code x} is smaller
  145.      * than 3.
  146.      * @since 1.5
  147.      */
  148.     @Override
  149.     public <T extends CalculusFieldElement<T>> FieldPolynomialSplineFunction<T> interpolate(
  150.                     T[] x, T[] y)
  151.         throws MathIllegalArgumentException {

  152.         MathUtils.checkNotNull(x);
  153.         MathUtils.checkNotNull(y);
  154.         MathArrays.checkEqualLength(x, y);
  155.         if (x.length < 3) {
  156.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_POINTS,
  157.                                                    x.length, 3, true);
  158.         }

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

  161.         MathArrays.checkOrder(x);

  162.         // Differences between knot points
  163.         final Field<T> field = x[0].getField();
  164.         final T[] h = MathArrays.buildArray(field, n);
  165.         for (int i = 0; i < n; i++) {
  166.             h[i] = x[i + 1].subtract(x[i]);
  167.         }

  168.         final T[] mu = MathArrays.buildArray(field, n);
  169.         final T[] z = MathArrays.buildArray(field, n + 1);
  170.         mu[0] = field.getZero();
  171.         z[0]  = field.getZero();
  172.         for (int i = 1; i < n; i++) {
  173.             final T g = x[i+1].subtract(x[i - 1]).multiply(2).subtract(h[i - 1].multiply(mu[i -1]));
  174.             mu[i] = h[i].divide(g);
  175.             z[i] =          y[i + 1].multiply(h[i - 1]).
  176.                    subtract(y[i].multiply(x[i + 1].subtract(x[i - 1]))).
  177.                         add(y[i - 1].multiply(h[i])).
  178.                    multiply(3).
  179.                    divide(h[i - 1].multiply(h[i])).
  180.                    subtract(h[i - 1].multiply(z[i - 1])).
  181.                    divide(g);
  182.         }

  183.         // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
  184.         final T[] b = MathArrays.buildArray(field, n);
  185.         final T[] c = MathArrays.buildArray(field, n + 1);
  186.         final T[] d = MathArrays.buildArray(field, n);

  187.         z[n] = field.getZero();
  188.         c[n] = field.getZero();

  189.         for (int j = n -1; j >=0; j--) {
  190.             c[j] = z[j].subtract(mu[j].multiply(c[j + 1]));
  191.             b[j] = y[j + 1].subtract(y[j]).divide(h[j]).
  192.                    subtract(h[j].multiply(c[j + 1].add(c[j]).add(c[j])).divide(3));
  193.             d[j] = c[j + 1].subtract(c[j]).divide(h[j].multiply(3));
  194.         }

  195.         @SuppressWarnings("unchecked")
  196.         final FieldPolynomialFunction<T>[] polynomials =
  197.                         (FieldPolynomialFunction<T>[]) Array.newInstance(FieldPolynomialFunction.class, n);
  198.         final T[] coefficients = MathArrays.buildArray(field, 4);
  199.         for (int i = 0; i < n; i++) {
  200.             coefficients[0] = y[i];
  201.             coefficients[1] = b[i];
  202.             coefficients[2] = c[i];
  203.             coefficients[3] = d[i];
  204.             polynomials[i] = new FieldPolynomialFunction<>(coefficients);
  205.         }

  206.         return new FieldPolynomialSplineFunction<>(x, polynomials);
  207.     }

  208. }