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

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

  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.     public PolynomialSplineFunction interpolate(double x[], double y[])
  44.         throws DimensionMismatchException,
  45.                NumberIsTooSmallException,
  46.                NonMonotonicSequenceException {
  47.         if (x.length != y.length) {
  48.             throw new DimensionMismatchException(x.length, y.length);
  49.         }

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

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

  56.         MathArrays.checkOrder(x);

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

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

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