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
19 import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
20 import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialSplineFunction;
21 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
22 import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
23 import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
24 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
25 import org.apache.commons.math4.legacy.core.MathArrays;
26
27 /**
28 * Implements a linear function for interpolation of real univariate functions.
29 *
30 */
31 public class LinearInterpolator implements UnivariateInterpolator {
32 /**
33 * Computes a linear interpolating function for the data set.
34 *
35 * @param x the arguments for the interpolation points
36 * @param y the values for the interpolation points
37 * @return a function which interpolates the data set
38 * @throws DimensionMismatchException if {@code x} and {@code y}
39 * have different sizes.
40 * @throws NonMonotonicSequenceException if {@code x} is not sorted in
41 * strict increasing order.
42 * @throws NumberIsTooSmallException if the size of {@code x} is smaller
43 * than 2.
44 */
45 @Override
46 public PolynomialSplineFunction interpolate(double[] x, double[] y)
47 throws DimensionMismatchException,
48 NumberIsTooSmallException,
49 NonMonotonicSequenceException {
50 if (x.length != y.length) {
51 throw new DimensionMismatchException(x.length, y.length);
52 }
53
54 if (x.length < 2) {
55 throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
56 x.length, 2, true);
57 }
58
59 // Number of intervals. The number of data points is n + 1.
60 int n = x.length - 1;
61
62 MathArrays.checkOrder(x);
63
64 // Slope of the lines between the datapoints.
65 final double[] m = new double[n];
66 for (int i = 0; i < n; i++) {
67 m[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
68 }
69
70 final PolynomialFunction[] polynomials = new PolynomialFunction[n];
71 final double[] coefficients = new double[2];
72 for (int i = 0; i < n; i++) {
73 coefficients[0] = y[i];
74 coefficients[1] = m[i];
75 polynomials[i] = new PolynomialFunction(coefficients);
76 }
77
78 return new PolynomialSplineFunction(x, polynomials);
79 }
80 }