DividedDifferenceInterpolator.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.io.Serializable;

  23. import org.hipparchus.analysis.polynomials.PolynomialFunctionLagrangeForm;
  24. import org.hipparchus.analysis.polynomials.PolynomialFunctionNewtonForm;
  25. import org.hipparchus.exception.MathIllegalArgumentException;

  26. /**
  27.  * Implements the <a href=
  28.  * "http://mathworld.wolfram.com/NewtonsDividedDifferenceInterpolationFormula.html">
  29.  * Divided Difference Algorithm</a> for interpolation of real univariate
  30.  * functions. For reference, see <b>Introduction to Numerical Analysis</b>,
  31.  * ISBN 038795452X, chapter 2.
  32.  * <p>
  33.  * The actual code of Neville's evaluation is in PolynomialFunctionLagrangeForm,
  34.  * this class provides an easy-to-use interface to it.</p>
  35.  *
  36.  */
  37. public class DividedDifferenceInterpolator
  38.     implements UnivariateInterpolator, Serializable {
  39.     /** serializable version identifier */
  40.     private static final long serialVersionUID = 107049519551235069L;

  41.     /** Empty constructor.
  42.      * <p>
  43.      * This constructor is not strictly necessary, but it prevents spurious
  44.      * javadoc warnings with JDK 18 and later.
  45.      * </p>
  46.      * @since 3.0
  47.      */
  48.     public DividedDifferenceInterpolator() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  49.         // nothing to do
  50.     }

  51.     /**
  52.      * Compute an interpolating function for the dataset.
  53.      *
  54.      * @param x Interpolating points array.
  55.      * @param y Interpolating values array.
  56.      * @return a function which interpolates the dataset.
  57.      * @throws MathIllegalArgumentException if the array lengths are different.
  58.      * @throws MathIllegalArgumentException if the number of points is less than 2.
  59.      * @throws MathIllegalArgumentException if {@code x} is not sorted in
  60.      * strictly increasing order.
  61.      */
  62.     @Override
  63.     public PolynomialFunctionNewtonForm interpolate(double[] x, double[] y)
  64.         throws MathIllegalArgumentException {
  65.         /**
  66.          * a[] and c[] are defined in the general formula of Newton form:
  67.          * p(x) = a[0] + a[1](x-c[0]) + a[2](x-c[0])(x-c[1]) + ... +
  68.          *        a[n](x-c[0])(x-c[1])...(x-c[n-1])
  69.          */
  70.         PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);

  71.         /**
  72.          * When used for interpolation, the Newton form formula becomes
  73.          * p(x) = f[x0] + f[x0,x1](x-x0) + f[x0,x1,x2](x-x0)(x-x1) + ... +
  74.          *        f[x0,x1,...,x[n-1]](x-x0)(x-x1)...(x-x[n-2])
  75.          * Therefore, a[k] = f[x0,x1,...,xk], c[k] = x[k].
  76.          * <p>
  77.          * Note x[], y[], a[] have the same length but c[]'s size is one less.</p>
  78.          */
  79.         final double[] c = new double[x.length-1];
  80.         System.arraycopy(x, 0, c, 0, c.length);

  81.         final double[] a = computeDividedDifference(x, y);
  82.         return new PolynomialFunctionNewtonForm(a, c);
  83.     }

  84.     /**
  85.      * Return a copy of the divided difference array.
  86.      * <p>
  87.      * The divided difference array is defined recursively by <pre>
  88.      * f[x0] = f(x0)
  89.      * f[x0,x1,...,xk] = (f[x1,...,xk] - f[x0,...,x[k-1]]) / (xk - x0)
  90.      * </pre>
  91.      * <p>
  92.      * The computational complexity is \(O(n^2)\) where \(n\) is the common
  93.      * length of {@code x} and {@code y}.</p>
  94.      *
  95.      * @param x Interpolating points array.
  96.      * @param y Interpolating values array.
  97.      * @return a fresh copy of the divided difference array.
  98.      * @throws MathIllegalArgumentException if the array lengths are different.
  99.      * @throws MathIllegalArgumentException if the number of points is less than 2.
  100.      * @throws MathIllegalArgumentException
  101.      * if {@code x} is not sorted in strictly increasing order.
  102.      */
  103.     protected static double[] computeDividedDifference(final double[] x, final double[] y)
  104.         throws MathIllegalArgumentException {
  105.         PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);

  106.         final double[] divdiff = y.clone(); // initialization

  107.         final int n = x.length;
  108.         final double[] a = new double [n];
  109.         a[0] = divdiff[0];
  110.         for (int i = 1; i < n; i++) {
  111.             for (int j = 0; j < n-i; j++) {
  112.                 final double denominator = x[j+i] - x[j];
  113.                 divdiff[j] = (divdiff[j+1] - divdiff[j]) / denominator;
  114.             }
  115.             a[i] = divdiff[0];
  116.         }

  117.         return a;
  118.     }
  119. }