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 * Implements a linear function for interpolation of real univariate functions. 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision$ $Date$ 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.2 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class LinearInterpolator implements UnivariateRealInterpolator { 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Computes a linear interpolating function for the data set. 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param x the arguments for the interpolation points 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param y the values for the interpolation points 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a function which interpolates the data set 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws DimensionMismatchException if {@code x} and {@code y} 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * have different sizes. 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws org.apache.commons.math.exception.NonMonotonousSequenceException 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * if {@code x} is not sorted in strict increasing order. 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @throws NumberIsTooSmallException if the size of {@code x} is smaller 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * than 2. 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public PolynomialSplineFunction interpolate(double x[], double y[]) { 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (x.length != y.length) { 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new DimensionMismatchException(x.length, y.length); 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (x.length < 2) { 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS, 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond x.length, 2, true); 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Number of intervals. The number of data points is n + 1. 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int n = x.length - 1; 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond MathUtils.checkOrder(x); 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Slope of the lines between the datapoints. 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double m[] = new double[n]; 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < n; i++) { 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond m[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]); 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond PolynomialFunction polynomials[] = new PolynomialFunction[n]; 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double coefficients[] = new double[2]; 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < n; i++) { 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond coefficients[0] = y[i]; 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond coefficients[1] = m[i]; 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond polynomials[i] = new PolynomialFunction(coefficients); 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new PolynomialSplineFunction(x, polynomials); 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 76