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.math4.legacy.analysis.interpolation;
018
019import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
020import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialSplineFunction;
021import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
022import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
023import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
024import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
025import org.apache.commons.math4.legacy.core.MathArrays;
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    @Override
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}