001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.analysis.interpolation;
018
019import java.io.Serializable;
020
021import org.apache.commons.math4.analysis.polynomials.PolynomialFunctionLagrangeForm;
022import org.apache.commons.math4.analysis.polynomials.PolynomialFunctionNewtonForm;
023import org.apache.commons.math4.exception.DimensionMismatchException;
024import org.apache.commons.math4.exception.NonMonotonicSequenceException;
025import org.apache.commons.math4.exception.NumberIsTooSmallException;
026
027/**
028 * Implements the <a href=
029 * "http://mathworld.wolfram.com/NewtonsDividedDifferenceInterpolationFormula.html">
030 * Divided Difference Algorithm</a> for interpolation of real univariate
031 * functions. For reference, see <b>Introduction to Numerical Analysis</b>,
032 * ISBN 038795452X, chapter 2.
033 * <p>
034 * The actual code of Neville's evaluation is in PolynomialFunctionLagrangeForm,
035 * this class provides an easy-to-use interface to it.</p>
036 *
037 * @since 1.2
038 */
039public class DividedDifferenceInterpolator
040    implements UnivariateInterpolator, Serializable {
041    /** serializable version identifier */
042    private static final long serialVersionUID = 107049519551235069L;
043
044    /**
045     * Compute an interpolating function for the dataset.
046     *
047     * @param x Interpolating points array.
048     * @param y Interpolating values array.
049     * @return a function which interpolates the dataset.
050     * @throws DimensionMismatchException if the array lengths are different.
051     * @throws NumberIsTooSmallException if the number of points is less than 2.
052     * @throws NonMonotonicSequenceException if {@code x} is not sorted in
053     * strictly increasing order.
054     */
055    @Override
056    public PolynomialFunctionNewtonForm interpolate(double x[], double y[])
057        throws DimensionMismatchException,
058               NumberIsTooSmallException,
059               NonMonotonicSequenceException {
060        /*
061         * a[] and c[] are defined in the general formula of Newton form:
062         * p(x) = a[0] + a[1](x-c[0]) + a[2](x-c[0])(x-c[1]) + ... +
063         *        a[n](x-c[0])(x-c[1])...(x-c[n-1])
064         */
065        PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);
066
067        /*
068         * When used for interpolation, the Newton form formula becomes
069         * p(x) = f[x0] + f[x0,x1](x-x0) + f[x0,x1,x2](x-x0)(x-x1) + ... +
070         *        f[x0,x1,...,x[n-1]](x-x0)(x-x1)...(x-x[n-2])
071         * Therefore, a[k] = f[x0,x1,...,xk], c[k] = x[k].
072         * <p>
073         * Note x[], y[], a[] have the same length but c[]'s size is one less.</p>
074         */
075        final double[] c = new double[x.length-1];
076        System.arraycopy(x, 0, c, 0, c.length);
077
078        final double[] a = computeDividedDifference(x, y);
079        return new PolynomialFunctionNewtonForm(a, c);
080    }
081
082    /**
083     * Return a copy of the divided difference array.
084     * <p>
085     * The divided difference array is defined recursively by <pre>
086     * f[x0] = f(x0)
087     * f[x0,x1,...,xk] = (f[x1,...,xk] - f[x0,...,x[k-1]]) / (xk - x0)
088     * </pre>
089     * <p>
090     * The computational complexity is \(O(n^2)\) where \(n\) is the common
091     * length of {@code x} and {@code y}.</p>
092     *
093     * @param x Interpolating points array.
094     * @param y Interpolating values array.
095     * @return a fresh copy of the divided difference array.
096     * @throws DimensionMismatchException if the array lengths are different.
097     * @throws NumberIsTooSmallException if the number of points is less than 2.
098     * @throws NonMonotonicSequenceException
099     * if {@code x} is not sorted in strictly increasing order.
100     */
101    protected static double[] computeDividedDifference(final double x[], final double y[])
102        throws DimensionMismatchException,
103               NumberIsTooSmallException,
104               NonMonotonicSequenceException {
105        PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);
106
107        final double[] divdiff = y.clone(); // initialization
108
109        final int n = x.length;
110        final double[] a = new double [n];
111        a[0] = divdiff[0];
112        for (int i = 1; i < n; i++) {
113            for (int j = 0; j < n-i; j++) {
114                final double denominator = x[j+i] - x[j];
115                divdiff[j] = (divdiff[j+1] - divdiff[j]) / denominator;
116            }
117            a[i] = divdiff[0];
118        }
119
120        return a;
121    }
122}