1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.analysis.interpolation;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.DimensionMismatchException;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.NumberIsTooSmallException;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.polynomials.PolynomialFunction;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.MathUtils;
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set.
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction}
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * consisting of n cubic polynomials, defined over the subintervals determined by the x values,
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * x[0] < x[i] ... < x[n].  The x values are referred to as "knot points."</p>
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The value of the PolynomialSplineFunction at a point x that is greater than or equal to the smallest
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * knot point and strictly less than the largest knot point is computed by finding the subinterval to which
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * x belongs and computing the value of the corresponding polynomial at <code>x - x[i] </code> where
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>i</code> is the index of the subinterval.  See {@link PolynomialSplineFunction} for more details.
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p>
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The interpolating polynomials satisfy: <ol>
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>The value of the PolynomialSplineFunction at each of the input x values equals the
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  corresponding y value.</li>
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  "match up" at the knot points, as do their first and second derivatives).</li>
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ol></p>
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires,
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <u>Numerical Analysis</u>, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131.
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p>
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 983921 $ $Date: 2010-08-10 12:46:06 +0200 (mar. 10 août 2010) $
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class SplineInterpolator implements UnivariateRealInterpolator {
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Computes an interpolating function for the data set.
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param x the arguments for the interpolation points
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param y the values for the interpolation points
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return a function which interpolates the data set
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws DimensionMismatchException if {@code x} and {@code y}
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * have different sizes.
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * if {@code x} is not sorted in strict increasing order.
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws NumberIsTooSmallException if the size of {@code x} is smaller
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * than 3.
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public PolynomialSplineFunction interpolate(double x[], double y[]) {
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (x.length != y.length) {
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw new DimensionMismatchException(x.length, y.length);
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (x.length < 3) {
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                                x.length, 3, true);
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Number of intervals.  The number of data points is n + 1.
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int n = x.length - 1;
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        MathUtils.checkOrder(x);
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Differences between knot points
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double h[] = new double[n];
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < n; i++) {
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            h[i] = x[i + 1] - x[i];
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double mu[] = new double[n];
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double z[] = new double[n + 1];
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        mu[0] = 0d;
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        z[0] = 0d;
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double g = 0;
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 1; i < n; i++) {
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            g = 2d * (x[i+1]  - x[i - 1]) - h[i - 1] * mu[i -1];
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            mu[i] = h[i] / g;
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) /
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g;
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double b[] = new double[n];
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double c[] = new double[n + 1];
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double d[] = new double[n];
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        z[n] = 0d;
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        c[n] = 0d;
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int j = n -1; j >=0; j--) {
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            c[j] = z[j] - mu[j] * c[j + 1];
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        PolynomialFunction polynomials[] = new PolynomialFunction[n];
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double coefficients[] = new double[4];
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < n; i++) {
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            coefficients[0] = y[i];
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            coefficients[1] = b[i];
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            coefficients[2] = c[i];
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            coefficients[3] = d[i];
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            polynomials[i] = new PolynomialFunction(coefficients);
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return new PolynomialSplineFunction(x, polynomials);
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
128