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     */
017    package org.apache.commons.math3.analysis.interpolation;
018    
019    import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
020    import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
021    import org.apache.commons.math3.util.MathArrays;
022    import org.apache.commons.math3.exception.DimensionMismatchException;
023    import org.apache.commons.math3.exception.NumberIsTooSmallException;
024    import org.apache.commons.math3.exception.NonMonotonicSequenceException;
025    import org.apache.commons.math3.exception.util.LocalizedFormats;
026    
027    /**
028     * Implements a linear function for interpolation of real univariate functions.
029     *
030     * @version $Id: LinearInterpolator.java 1379904 2012-09-01 23:54:52Z erans $
031     */
032    public class LinearInterpolator implements UnivariateInterpolator {
033        /**
034         * Computes a linear interpolating function for the data set.
035         *
036         * @param x the arguments for the interpolation points
037         * @param y the values for the interpolation points
038         * @return a function which interpolates the data set
039         * @throws DimensionMismatchException if {@code x} and {@code y}
040         * have different sizes.
041         * @throws NonMonotonicSequenceException if {@code x} is not sorted in
042         * strict increasing order.
043         * @throws NumberIsTooSmallException if the size of {@code x} is smaller
044         * than 2.
045         */
046        public PolynomialSplineFunction interpolate(double x[], double y[])
047            throws DimensionMismatchException,
048                   NumberIsTooSmallException,
049                   NonMonotonicSequenceException {
050            if (x.length != y.length) {
051                throw new DimensionMismatchException(x.length, y.length);
052            }
053    
054            if (x.length < 2) {
055                throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
056                                                    x.length, 2, true);
057            }
058    
059            // Number of intervals.  The number of data points is n + 1.
060            int n = x.length - 1;
061    
062            MathArrays.checkOrder(x);
063    
064            // Slope of the lines between the datapoints.
065            final double m[] = new double[n];
066            for (int i = 0; i < n; i++) {
067                m[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
068            }
069    
070            final PolynomialFunction polynomials[] = new PolynomialFunction[n];
071            final double coefficients[] = new double[2];
072            for (int i = 0; i < n; i++) {
073                coefficients[0] = y[i];
074                coefficients[1] = m[i];
075                polynomials[i] = new PolynomialFunction(coefficients);
076            }
077    
078            return new PolynomialSplineFunction(x, polynomials);
079        }
080    }