LinearInterpolator.java

  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.math4.legacy.analysis.interpolation;

  18. import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
  19. import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialSplineFunction;
  20. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  21. import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
  22. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
  23. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
  24. import org.apache.commons.math4.legacy.core.MathArrays;

  25. /**
  26.  * Implements a linear function for interpolation of real univariate functions.
  27.  *
  28.  */
  29. public class LinearInterpolator implements UnivariateInterpolator {
  30.     /**
  31.      * Computes a linear interpolating function for the data set.
  32.      *
  33.      * @param x the arguments for the interpolation points
  34.      * @param y the values for the interpolation points
  35.      * @return a function which interpolates the data set
  36.      * @throws DimensionMismatchException if {@code x} and {@code y}
  37.      * have different sizes.
  38.      * @throws NonMonotonicSequenceException if {@code x} is not sorted in
  39.      * strict increasing order.
  40.      * @throws NumberIsTooSmallException if the size of {@code x} is smaller
  41.      * than 2.
  42.      */
  43.     @Override
  44.     public PolynomialSplineFunction interpolate(double[] x, double[] y)
  45.         throws DimensionMismatchException,
  46.                NumberIsTooSmallException,
  47.                NonMonotonicSequenceException {
  48.         if (x.length != y.length) {
  49.             throw new DimensionMismatchException(x.length, y.length);
  50.         }

  51.         if (x.length < 2) {
  52.             throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
  53.                                                 x.length, 2, true);
  54.         }

  55.         // Number of intervals.  The number of data points is n + 1.
  56.         int n = x.length - 1;

  57.         MathArrays.checkOrder(x);

  58.         // Slope of the lines between the datapoints.
  59.         final double[] m = new double[n];
  60.         for (int i = 0; i < n; i++) {
  61.             m[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
  62.         }

  63.         final PolynomialFunction[] polynomials = new PolynomialFunction[n];
  64.         final double[] coefficients = new double[2];
  65.         for (int i = 0; i < n; i++) {
  66.             coefficients[0] = y[i];
  67.             coefficients[1] = m[i];
  68.             polynomials[i] = new PolynomialFunction(coefficients);
  69.         }

  70.         return new PolynomialSplineFunction(x, polynomials);
  71.     }
  72. }