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.  *      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.PolynomialFunctionLagrangeForm;
  19. import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunctionNewtonForm;
  20. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  21. import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
  22. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;

  23. /**
  24.  * Implements the <a href=
  25.  * "http://mathworld.wolfram.com/NewtonsDividedDifferenceInterpolationFormula.html">
  26.  * Divided Difference Algorithm</a> for interpolation of real univariate
  27.  * functions. For reference, see <b>Introduction to Numerical Analysis</b>,
  28.  * ISBN 038795452X, chapter 2.
  29.  * <p>
  30.  * The actual code of Neville's evaluation is in PolynomialFunctionLagrangeForm,
  31.  * this class provides an easy-to-use interface to it.</p>
  32.  *
  33.  * @since 1.2
  34.  */
  35. public class DividedDifferenceInterpolator
  36.     implements UnivariateInterpolator {
  37.     /**
  38.      * Compute an interpolating function for the dataset.
  39.      *
  40.      * @param x Interpolating points array.
  41.      * @param y Interpolating values array.
  42.      * @return a function which interpolates the dataset.
  43.      * @throws DimensionMismatchException if the array lengths are different.
  44.      * @throws NumberIsTooSmallException if the number of points is less than 2.
  45.      * @throws NonMonotonicSequenceException if {@code x} is not sorted in
  46.      * strictly increasing order.
  47.      */
  48.     @Override
  49.     public PolynomialFunctionNewtonForm interpolate(double[] x, double[] y)
  50.         throws DimensionMismatchException,
  51.                NumberIsTooSmallException,
  52.                NonMonotonicSequenceException {
  53.         /*
  54.          * a[] and c[] are defined in the general formula of Newton form:
  55.          * p(x) = a[0] + a[1](x-c[0]) + a[2](x-c[0])(x-c[1]) + ... +
  56.          *        a[n](x-c[0])(x-c[1])...(x-c[n-1])
  57.          */
  58.         PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);

  59.         /*
  60.          * When used for interpolation, the Newton form formula becomes
  61.          * p(x) = f[x0] + f[x0,x1](x-x0) + f[x0,x1,x2](x-x0)(x-x1) + ... +
  62.          *        f[x0,x1,...,x[n-1]](x-x0)(x-x1)...(x-x[n-2])
  63.          * Therefore, a[k] = f[x0,x1,...,xk], c[k] = x[k].
  64.          * <p>
  65.          * Note x[], y[], a[] have the same length but c[]'s size is one less.</p>
  66.          */
  67.         final double[] c = new double[x.length-1];
  68.         System.arraycopy(x, 0, c, 0, c.length);

  69.         final double[] a = computeDividedDifference(x, y);
  70.         return new PolynomialFunctionNewtonForm(a, c);
  71.     }

  72.     /**
  73.      * Return a copy of the divided difference array.
  74.      * <p>
  75.      * The divided difference array is defined recursively by <pre>
  76.      * f[x0] = f(x0)
  77.      * f[x0,x1,...,xk] = (f[x1,...,xk] - f[x0,...,x[k-1]]) / (xk - x0)
  78.      * </pre>
  79.      * <p>
  80.      * The computational complexity is \(O(n^2)\) where \(n\) is the common
  81.      * length of {@code x} and {@code y}.</p>
  82.      *
  83.      * @param x Interpolating points array.
  84.      * @param y Interpolating values array.
  85.      * @return a fresh copy of the divided difference array.
  86.      * @throws DimensionMismatchException if the array lengths are different.
  87.      * @throws NumberIsTooSmallException if the number of points is less than 2.
  88.      * @throws NonMonotonicSequenceException
  89.      * if {@code x} is not sorted in strictly increasing order.
  90.      */
  91.     protected static double[] computeDividedDifference(final double[] x, final double[] y)
  92.         throws DimensionMismatchException,
  93.                NumberIsTooSmallException,
  94.                NonMonotonicSequenceException {
  95.         PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);

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

  97.         final int n = x.length;
  98.         final double[] a = new double [n];
  99.         a[0] = divdiff[0];
  100.         for (int i = 1; i < n; i++) {
  101.             for (int j = 0; j < n-i; j++) {
  102.                 final double denominator = x[j+i] - x[j];
  103.                 divdiff[j] = (divdiff[j+1] - divdiff[j]) / denominator;
  104.             }
  105.             a[i] = divdiff[0];
  106.         }

  107.         return a;
  108.     }
  109. }