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