1 /* 2 * Copyright (C) 2023 Alberto Irurueta Carro (alberto@irurueta.com) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.irurueta.numerical.interpolation; 17 18 /** 19 * Computes linear interpolation. 20 */ 21 public class LinearInterpolator extends BaseInterpolator { 22 23 /** 24 * Length of x's and y's to take into account. 25 */ 26 private static final int M = 2; 27 28 public LinearInterpolator(final double[] x, final double[] y) { 29 super(x, y, M); 30 } 31 32 /** 33 * Actual interpolation method to be implemented by subclasses. 34 * 35 * @param j index where value x to be interpolated in located in the array of xx. 36 * @param x value to obtain interpolation for. 37 * @return interpolated value. 38 */ 39 @Override 40 public double rawinterp(int j, double x) { 41 if (xx[j] == xx[j + 1]) { 42 return yy[j]; 43 } else { 44 return yy[j] + ((x - xx[j]) / (xx[j + 1] - xx[j])) * (yy[j + 1] - yy[j]); 45 } 46 } 47 }