/**********************************************************
*
* Class LinearInterpolation
*
* Class for performing a linear interpolation
*
*
* WRITTEN BY: Dr Michael Thomas Flanagan
*
* DATE: February 2011
* UPDATE:
*
* DOCUMENTATION:
* See Michael Thomas Flanagan's Java library on-line web page:
* http://www.ee.ucl.ac.uk/~mflanaga/java/LinearInterpolation.html
* http://www.ee.ucl.ac.uk/~mflanaga/java/
*
* Copyright (c) 2011 Michael Thomas Flanagan
*
* PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice,
* this list of conditions and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission
* from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/
package flanagan.interpolation;
import flanagan.math.Fmath;
import flanagan.math.Conv;
public class LinearInterpolation{
private int nPoints = 0; // no. of tabulated points
private int nPointsOriginal = 0; // no. of tabulated points after any deletions of identical points
private double[] y = null; // y=f(x) tabulated function
private double[] x = null; // x in tabulated function f(x)
private double yy = Double.NaN; // interpolated value of y
private double dydx = Double.NaN; // interpolated value of the first derivative, dy/dx
private int[]newAndOldIndices; // record of indices on ordering x into ascending order
private double xMin = Double.NaN; // minimum x value
private double xMax = Double.NaN; // maximum x value
private double range = Double.NaN; // xMax - xMin
private boolean checkPoints = false; // = true when points checked for identical values
private static boolean supress = false; // if true: warning messages supressed
private static boolean averageIdenticalAbscissae = false; // if true: the the ordinate values for identical abscissae are averaged
// if false: the abscissae values are separated by 0.001 of the total abscissae range;
private static double potentialRoundingError = 5e-15; // potential rounding error used in checking wheter a value lies within the interpolation bounds
private static boolean roundingCheck = true; // = true: points outside the interpolation bounds by less than the potential rounding error rounded to the bounds limit
// Constructor with data arrays initialised to arrays x and y
public LinearInterpolation(double[] x, double[] y){
this.nPoints=x.length;
this.nPointsOriginal = this.nPoints;
if(this.nPoints!=y.length)throw new IllegalArgumentException("Arrays x and y are of different length "+ this.nPoints + " " + y.length);
if(this.nPoints<3)throw new IllegalArgumentException("A minimum of three data points is needed");
this.x = new double[nPoints];
this.y = new double[nPoints];
for(int i=0; ithis.y[1]){
if(this.y[1]>this.y[2]){
check = stay(ii, jj, sepn);
}
else{
check = swap(ii, jj, sepn);
}
}
else{
if(this.y[2]<=this.y[1]){
check = swap(ii, jj, sepn);
}
else{
check = stay(ii, jj, sepn);
}
}
}
if(jj==this.nPoints-1){
if(x[nP-2]-x[nP-3]<=sepn)sepn = (x[nP-2]-x[nP-3])/2.0D;
if(this.y[ii]<=this.y[jj]){
if(this.y[ii-1]<=this.y[ii]){
check = stay(ii, jj, sepn);
}
else{
check = swap(ii, jj, sepn);
}
}
else{
if(this.y[ii-1]<=this.y[ii]){
check = swap(ii, jj, sepn);
}
else{
check = stay(ii, jj, sepn);
}
}
}
if(ii!=0 && jj!=this.nPoints-1){
if(x[ii]-x[ii-1]<=sepn)sepn = (x[ii]-x[ii-1])/2;
if(x[jj+1]-x[jj]<=sepn)sepn = (x[jj+1]-x[jj])/2;
if(this.y[ii]>this.y[ii-1]){
if(this.y[jj]>this.y[ii]){
if(this.y[jj]>this.y[jj+1]){
if(this.y[ii-1]<=this.y[jj+1]){
check = stay(ii, jj, sepn);
}
else{
check = swap(ii, jj, sepn);
}
}
else{
check = stay(ii, jj, sepn);
}
}
else{
if(this.y[jj+1]>this.y[jj]){
if(this.y[jj+1]>this.y[ii-1] && this.y[jj+1]>this.y[ii-1]){
check = stay(ii, jj, sepn);
}
}
else{
check = swap(ii, jj, sepn);
}
}
}
else{
if(this.y[jj]>this.y[ii]){
if(this.y[jj+1]>this.y[jj]){
check = stay(ii, jj, sepn);
}
}
else{
if(this.y[jj+1]>this.y[ii-1]){
check = stay(ii, jj, sepn);
}
else{
check = swap(ii, jj, sepn);
}
}
}
}
if(check==false){
check = stay(ii, jj, sepn);
}
if(!LinearInterpolation.supress)System.out.println(", the two abscissae have been separated by a distance " + sepn);
jj++;
}
}
if((this.nPoints-1)==ii)test2 = false;
}
else{
jj++;
}
if(jj>=this.nPoints)test2 = false;
}
ii++;
if(ii>=this.nPoints-1)test1 = false;
}
if(this.nPoints<3)throw new IllegalArgumentException("Removal of duplicate points has reduced the number of points to less than the required minimum of three data points");
this.checkPoints = true;
}
// Swap method for checkForIdenticalPoints procedure
private boolean swap(int ii, int jj, double sepn){
this.x[ii] += sepn;
this.x[jj] -= sepn;
double hold = this.x[ii];
this.x[ii] = this.x[jj];
this.x[jj] = hold;
hold = this.y[ii];
this.y[ii] = this.y[jj];
this.y[jj] = hold;
return true;
}
// Stay method for checkForIdenticalPoints procedure
private boolean stay(int ii, int jj, double sepn){
this.x[ii] -= sepn;
this.x[jj] += sepn;
return true;
}
// Supress warning messages in the identifiaction of duplicate points
public static void supress(){
LinearInterpolation.supress = true;
}
// Unsupress warning messages in the identifiaction of duplicate points
public static void unsupress(){
LinearInterpolation.supress = false;
}
// INTERPOLATE
// Returns an interpolated value of y for a value of x from a tabulated function y=f(x)
// after the data has been entered via a constructor.
public double interpolate(double xx){
// Check for violation of interpolation bounds
if (xxthis.x[this.nPoints-1]){
if(LinearInterpolation.roundingCheck && Math.abs(xx-this.x[this.nPoints-1])<=Math.pow(10, Math.floor(Math.log10(Math.abs(this.x[this.nPoints-1]))))*LinearInterpolation.potentialRoundingError){
xx = this.x[this.nPoints-1];
}
else{
throw new IllegalArgumentException("x ("+xx+") is outside the range of data points ("+this.x[0]+" to "+this.x[this.nPoints-1] + ")");
}
}
boolean flag = true;
for(int i=0; i
Ads help maintain this website.