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