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.math.analysis; 18 19 /** 20 * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set. 21 * <p> 22 * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction} 23 * consisting of n cubic polynomials, defined over the subintervals determined by the x values, 24 * x[0] < x[i] ... < x[n]. The x values are referred to as "knot points."</p> 25 * <p> 26 * The value of the PolynomialSplineFunction at a point x that is greater than or equal to the smallest 27 * knot point and strictly less than the largest knot point is computed by finding the subinterval to which 28 * x belongs and computing the value of the corresponding polynomial at <code>x - x[i] </code> where 29 * <code>i</code> is the index of the subinterval. See {@link PolynomialSplineFunction} for more details. 30 * </p> 31 * <p> 32 * The interpolating polynomials satisfy: <ol> 33 * <li>The value of the PolynomialSplineFunction at each of the input x values equals the 34 * corresponding y value.</li> 35 * <li>Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials 36 * "match up" at the knot points, as do their first and second derivatives).</li> 37 * </ol></p> 38 * <p> 39 * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires, 40 * <u>Numerical Analysis</u>, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131. 41 * </p> 42 * 43 * @version $Revision: 615734 $ $Date: 2008-01-27 23:10:03 -0700 (Sun, 27 Jan 2008) $ 44 * 45 */ 46 public class SplineInterpolator implements UnivariateRealInterpolator { 47 48 /** 49 * Computes an interpolating function for the data set. 50 * @param x the arguments for the interpolation points 51 * @param y the values for the interpolation points 52 * @return a function which interpolates the data set 53 */ 54 public UnivariateRealFunction interpolate(double x[], double y[]) { 55 if (x.length != y.length) { 56 throw new IllegalArgumentException("Dataset arrays must have same length."); 57 } 58 59 if (x.length < 3) { 60 throw new IllegalArgumentException 61 ("At least 3 datapoints are required to compute a spline interpolant"); 62 } 63 64 // Number of intervals. The number of data points is n + 1. 65 int n = x.length - 1; 66 67 for (int i = 0; i < n; i++) { 68 if (x[i] >= x[i + 1]) { 69 throw new IllegalArgumentException("Dataset x values must be strictly increasing."); 70 } 71 } 72 73 // Differences between knot points 74 double h[] = new double[n]; 75 for (int i = 0; i < n; i++) { 76 h[i] = x[i + 1] - x[i]; 77 } 78 79 double mu[] = new double[n]; 80 double z[] = new double[n + 1]; 81 mu[0] = 0d; 82 z[0] = 0d; 83 double g = 0; 84 for (int i = 1; i < n; i++) { 85 g = 2d * (x[i+1] - x[i - 1]) - h[i - 1] * mu[i -1]; 86 mu[i] = h[i] / g; 87 z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) / 88 (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g; 89 } 90 91 // cubic spline coefficients -- b is linear, c quadratic, d is cubic (original y's are constants) 92 double b[] = new double[n]; 93 double c[] = new double[n + 1]; 94 double d[] = new double[n]; 95 96 z[n] = 0d; 97 c[n] = 0d; 98 99 for (int j = n -1; j >=0; j--) { 100 c[j] = z[j] - mu[j] * c[j + 1]; 101 b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d; 102 d[j] = (c[j + 1] - c[j]) / (3d * h[j]); 103 } 104 105 PolynomialFunction polynomials[] = new PolynomialFunction[n]; 106 double coefficients[] = new double[4]; 107 for (int i = 0; i < n; i++) { 108 coefficients[0] = y[i]; 109 coefficients[1] = b[i]; 110 coefficients[2] = c[i]; 111 coefficients[3] = d[i]; 112 polynomials[i] = new PolynomialFunction(coefficients); 113 } 114 115 return new PolynomialSplineFunction(x, polynomials); 116 } 117 118 }