Importing release version 322 from old repos

This commit is contained in:
Marcel Kronfeld
2007-12-11 16:38:11 +00:00
parent 8cecdb016d
commit 7ae15be788
668 changed files with 109288 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
///////////////////////////////////////////////////////////////////////////////
// Filename: $RCSfile: AbstractDataModifier.java,v $
// Purpose: Some interpolation stuff.
// Language: Java
// Compiler: JDK 1.4
// Authors: Joerg K. Wegner
// Version: $Revision: 1.1 $
// $Date: 2003/07/22 19:24:58 $
// $Author: wegnerj $
//
// Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
//
///////////////////////////////////////////////////////////////////////////////
package wsi.ra.math.interpolation;
/*==========================================================================*
* IMPORTS
*==========================================================================*/
/*==========================================================================*
* CLASS DECLARATION
*==========================================================================*/
/**
* The minimal set of functions which should implemented in a data modifier for
* <code>AbstractDataSet</code>.
*/
public abstract class AbstractDataModifier
{
/*-------------------------------------------------------------------------*
* abstract methods
*-------------------------------------------------------------------------*/
/**
* Modifies the X data.
*/
public abstract void modifyX(double[] setX);
/**
* Modifies the Y data.
*/
public abstract void modifyY(double[] setY);
/**
* Modifies the data.
*/
public abstract void modify(double[] setX, double[] setY);
}
/****************************************************************************
* END OF FILE
****************************************************************************/

View File

@@ -0,0 +1,116 @@
///////////////////////////////////////////////////////////////////////////////
// Filename: $RCSfile: AbstractDataSet.java,v $
// Purpose: Some interpolation stuff.
// Language: Java
// Compiler: JDK 1.4
// Authors: Joerg K. Wegner
// Version: $Revision: 1.1 $
// $Date: 2003/07/22 19:25:04 $
// $Author: wegnerj $
//
// Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
//
///////////////////////////////////////////////////////////////////////////////
package wsi.ra.math.interpolation;
/*==========================================================================*
* IMPORTS
*==========================================================================*/
/*==========================================================================*
* CLASS DECLARATION
*==========================================================================*/
public abstract class AbstractDataSet
{
/*-------------------------------------------------------------------------*
* public member variables
*-------------------------------------------------------------------------*/
/*--------------------------------------------------------------o-----------*
* protected member variables
*-------------------------------------------------------------------------*/
/**
* double array of X data.
*
* @see #yDoubleData
*/
protected double[] xDoubleData = { -1, 1 };
/**
* double array of Y data.
*
* @see #xDoubleData
*/
protected double[] yDoubleData = { 1, 1 };
/*-------------------------------------------------------------------------*
* abstract methods
*-------------------------------------------------------------------------*/
/**
* Returns the length of the data set
* @return the length of the data set
*/
public int getLength()
{
return xDoubleData.length;
}
/**
* Returns an array of the X data
* @return the array of the X data
*/
public double[] getXData()
{
return xDoubleData;
}
/**
* Returns an array of the Y data
* @return the array of the Y data
*/
public double[] getYData()
{
return yDoubleData;
}
/**
* Returns the X label of the data set
* @return the X label of the data set
*/
public abstract String getXLabel();
/**
* Returns the Y label of the data set
* @return the Y label of the data set
*/
public abstract String getYLabel();
/**
* Modifies the X data.
*
* @param the data modifier
*/
public void modifyXData(AbstractDataModifier modifier)
{
modifier.modifyX(xDoubleData);
}
/**
* Modifies the Y data.
*
* @param the data modifier
*/
public void modifyYData(AbstractDataModifier modifier)
{
modifier.modifyY(yDoubleData);
}
/**
* Modifies the data.
*
* @param the data modifier
*/
public void modifyData(AbstractDataModifier modifier)
{
modifier.modify(xDoubleData, yDoubleData);
}
}
/****************************************************************************
* END OF FILE
****************************************************************************/

View File

@@ -0,0 +1,100 @@
///////////////////////////////////////////////////////////////////////////////
// Filename: $RCSfile: BasicDataSet.java,v $
// Purpose: Some interpolation stuff.
// Language: Java
// Compiler: JDK 1.4
// Authors: Joerg K. Wegner
// Version: $Revision: 1.1 $
// $Date: 2003/07/22 19:25:11 $
// $Author: wegnerj $
//
// Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
//
///////////////////////////////////////////////////////////////////////////////
package wsi.ra.math.interpolation;
import wsi.ra.sort.XYDoubleArray;
/*==========================================================================*
* IMPORTS
*==========================================================================*/
/**
* The minimum wrapper class for an <code>AbstractDataSet</code>.
*/
public class BasicDataSet extends AbstractDataSet
{
/*-------------------------------------------------------------------------*
* protected member variables
*-------------------------------------------------------------------------*/
protected int dataType = -1;
protected String xLabel = null;
protected String yLabel = null;
/*------------------------------------------------------------------------*
* constructor
*------------------------------------------------------------------------*/
public BasicDataSet()
{
this(null, null, null, null);
}
public BasicDataSet(XYDoubleArray data)
{
this(data.x, data.y, null, null);
}
public BasicDataSet(XYDoubleArray data, String xLabel, String yLabel)
{
this(data.x, data.y, xLabel, yLabel);
}
public BasicDataSet(
double[] xDoubleData,
double[] yDoubleData,
int dataType)
{
this(xDoubleData, yDoubleData, null, null);
}
public BasicDataSet(
double[] xDoubleData,
double[] yDoubleData,
String xLabel,
String yLabel)
{
this.xDoubleData = xDoubleData;
this.yDoubleData = yDoubleData;
this.xLabel = xLabel;
this.yLabel = yLabel;
}
/*-------------------------------------------------------------------------*
* public methods
*-------------------------------------------------------------------------*/
public int getDataType()
{
return dataType;
}
public String getXLabel()
{
return xLabel;
}
public String getYLabel()
{
return yLabel;
}
public String getAdditionalInformation(String parm1)
{
return new String();
}
}
/****************************************************************************
* END OF FILE
****************************************************************************/

View File

@@ -0,0 +1,44 @@
///////////////////////////////////////////////////////////////////////////////
// Filename: $RCSfile: InterpolationException.java,v $
// Purpose: Some interpolation stuff.
// Language: Java
// Compiler: JDK 1.4
// Authors: Joerg K. Wegner
// Version: $Revision: 1.1 $
// $Date: 2003/07/22 19:25:17 $
// $Author: wegnerj $
//
// Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
//
///////////////////////////////////////////////////////////////////////////////
package wsi.ra.math.interpolation;
/*==========================================================================*
* IMPORTS
*==========================================================================*/
/*==========================================================================*
* CLASS DECLARATION
*==========================================================================*/
/**
* Exception for interpolation error.
*/
public class InterpolationException extends Exception
{
public InterpolationException()
{
super();
}
public InterpolationException(String s)
{
super(s);
}
}
/****************************************************************************
* END OF FILE
****************************************************************************/

View File

@@ -0,0 +1,573 @@
///////////////////////////////////////////////////////////////////////////////
// Filename: $RCSfile: LinearInterpolation.java,v $
// Purpose: Some interpolation stuff.
// Language: Java
// Compiler: JDK 1.4
// Authors: Joerg K. Wegner
// Original author: Charles S. Stanton
// Version: $Revision: 1.1 $
// $Date: 2003/07/22 19:25:23 $
// $Author: wegnerj $
//
// Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
//
///////////////////////////////////////////////////////////////////////////////
package wsi.ra.math.interpolation;
/*==========================================================================*
* IMPORTS
*==========================================================================*/
//import cern.jet.stat.*;
/**
* Defines the routines for the spline interpolation of data.
*/
public class LinearInterpolation
{
AbstractDataSet abstractDataSet = null;
private double[] x, y;
//vectors of x,y
private double sumX = 0;
private double sumY = 0;
private double sumXY = 0;
private double sumXsquared = 0;
private double sumYsquared = 0;
private double Sxx, Sxy, Syy, n;
private double a = 0, b = 0;
//coefficients of regression
private int dataLength;
private double[][] residual;
// residual[0][i] = x[i], residual[1][i]= residual
private double maxAbsoluteResidual = 0.0;
private double SSR = 0.0;
//regression sum of squares
private double SSE = 0.0;
//error sum of squares
private double minX = Double.POSITIVE_INFINITY;
private double maxX = Double.NEGATIVE_INFINITY;
private double minY = Double.POSITIVE_INFINITY;
private double maxY = Double.NEGATIVE_INFINITY;
//MISC
String xName, yName;
double aCILower, aCIUpper, bCILower, bCIUpper; //confidence interval
double t, bSE, aSE;
double MSE, F;
static double[] t025 =
{
Double.NaN,
12.706,
4.303,
3.182,
2.776,
2.571,
2.447,
2.365,
2.306,
2.262,
2.228,
2.201,
2.179,
2.160,
2.145,
2.131,
2.120,
2.110,
2.101,
2.093,
2.086,
2.080,
2.075,
2.069,
2.064,
2.060,
2.056,
2.052,
2.048,
2.045,
1.960 };
/*------------------------------------------------------------------------*
* constructor
*------------------------------------------------------------------------*/
/**
* Initializes this class.
*/
public LinearInterpolation() throws InterpolationException
{
this.abstractDataSet = null;
}
/**
* Constructor for regression calculator.
*
* @param x is the array of x data
* @param y is the array of y data
*/
public LinearInterpolation(double[] x, double[] y)
{
this.x = x;
this.y = y;
if (x.length != y.length)
{
System.out.println("x, y vectors must be of same length");
}
else
{
dataLength = x.length;
doStatistics();
}
}
/**
* Initializes this class and calculates the second derivative of the spline.
*
* @param abstractDataSet the <code>AbstractDataSet</code>
*/
public LinearInterpolation(AbstractDataSet abstractDataSet)
throws InterpolationException
{
this.setAbstractDataSet(abstractDataSet);
}
public void setAbstractDataSet(AbstractDataSet abstractDataSet)
throws InterpolationException
{
this.abstractDataSet = abstractDataSet;
x = abstractDataSet.getXData();
y = abstractDataSet.getYData();
if (x.length != y.length)
{
System.out.println("x, y vectors must be of same length");
}
else
{
dataLength = x.length;
doStatistics();
}
}
/**
* Find the p value for a given value of F.
* Requires the COLT high performance library:
* http://hoschek.home.cern.ch/hoschek/colt/
*
* @param fValue the value for the CDF
* @return The P value
*/
// public double getP(double fValue) {
// double answer;
// double y1;
// double y2;
// //nu1 = 1;
// //x2 =1
// double nu2 = n - 2;
// y1 = nu2 / (nu2 + fValue);
// y2 = 0.0;
// answer = Gamma.incompleteBeta(nu2 / 2.0, 1 / 2.0, y1)
// - Gamma.incompleteBeta(nu2 / 2.0, 1 / 2.0, y2);
// return answer;
// }
/*
* Here are the accessor methods
*
*/
/**
* Gets the intercept of the regression line.
*
* @return The intercept.
*/
public double getIntercept()
{
return a;
}
/**
* Gets the Slope of the regression line.
*
* @return The slope.
*/
public double getSlope()
{
return b;
}
/**
* Gets the residuals of the regression.
*
* @return The residuals.
*/
public double[][] getResiduals()
{
return residual;
}
/**
* Gets the x data for the regression.
*
* @return The array of x values.
*/
public double[] getDataX()
{
return x;
}
/**
* Gets the y data for the regression.
*
* @return The array of y values.
*/
public double[] getDataY()
{
return y;
}
/**
* Gets the minimum of the x values.
*
* @return The minimum.
*/
public double getMinX()
{
return minX;
}
/**
* Gets the maximum of the x values.
*
* @return The maximum.
*/
public double getMaxX()
{
return maxX;
}
/**
* Gets the minimum of the y values.
*
* @return The minumum.
*/
public double getMinY()
{
return minY;
}
/**
* Gets the maximum of the y values.
*
* @return The maximum.
*/
public double getMaxY()
{
return maxY;
}
/**
* Gets the maximum absolute residual.
*
* @return The maximum.
*/
public double getMaxAbsoluteResidual()
{
return maxAbsoluteResidual;
}
/**
* Gets the sum of the square x deviations from mean of x.
*
* @return The Sxx value
*/
public double getSxx()
{
return Sxx;
}
/**
* Gets the sum of the square y deviations from mean of y.
*
* @return The Syy value
*/
public double getSyy()
{
return Syy;
}
/**
* Gets SSR = Sxy * Sxy / Sxx;
*
* @return The SSR value
*/
public double getSSR()
{
return SSR;
}
/**
* Gets SSE = Syy - SSR.
*
* @return The SSE value
*/
public double getSSE()
{
return SSE;
}
/**
* Gets the mean square error MSE.
*
* @return The MSE value
*/
public double getMSE()
{
return SSE / (n - 2);
}
/**
* Gets the mean XBar of x.
*
* @return The XBar value
*/
public double getXBar()
{
return sumX / n;
}
/**
* Gets the mean YBar of y.
*
* @return The YBar value
*/
public double getYBar()
{
return sumY / n;
}
/**
* Gets the sample size.
*
* @return The sample size.
*/
public int getDataLength()
{
return x.length;
}
/**
* Gets the Pearson R statistic of the regression.
*
* @return The PearsonR value
*/
public double getPearsonR()
{
return Sxy / Math.sqrt(Sxx * Syy);
}
/**
* Gets the sum of the x squared values.
*
* @return The sum of the x squared values.
*/
public double getSumXSquared()
{
return sumXsquared;
}
/**
* reset data to 0
*/
public void reset()
{
x = new double[0];
y = new double[0];
dataLength = 0;
n = 0.0;
residual = new double[0][0];
sumX = 0;
sumXsquared = 0;
sumY = 0;
sumYsquared = 0;
sumXY = 0;
}
/**
* Adds a new point to the regression (for interactive use).
*
* @param xValue The new x value
* @param yValue The new y value
*/
public void addPoint(double xValue, double yValue)
{
dataLength++;
double[] xNew = new double[dataLength];
double[] yNew = new double[dataLength];
System.arraycopy(x, 0, xNew, 0, dataLength - 1);
System.arraycopy(y, 0, yNew, 0, dataLength - 1);
xNew[dataLength - 1] = xValue;
yNew[dataLength - 1] = yValue;
x = xNew;
y = yNew;
updateStatistics(xValue, yValue);
}
private void doStatistics()
{
//Find sum of squares for x,y and sum of xy
for (int i = 0; i < dataLength; i++)
{
minX = Math.min(minX, x[i]);
maxX = Math.max(maxX, x[i]);
minY = Math.min(minY, y[i]);
maxY = Math.max(maxY, y[i]);
sumX += x[i];
sumY += y[i];
sumXsquared += x[i] * x[i];
sumYsquared += y[i] * y[i];
sumXY += x[i] * y[i];
}
//Caculate regression coefficients
n = (double) dataLength;
Sxx = sumXsquared - sumX * sumX / n;
Syy = sumYsquared - sumY * sumY / n;
Sxy = sumXY - sumX * sumY / n;
b = Sxy / Sxx;
a = (sumY - b * sumX) / n;
SSR = Sxy * Sxy / Sxx;
SSE = Syy - SSR;
calculateResiduals();
}
private void calculateResiduals()
{
residual = new double[2][dataLength];
maxAbsoluteResidual = 0.0;
for (int i = 0; i < dataLength; i++)
{
residual[0][i] = x[i];
residual[1][i] = y[i] - (a + b * x[i]);
maxAbsoluteResidual =
Math.max(maxAbsoluteResidual, Math.abs(y[i] - (a + b * x[i])));
}
}
//update statistics for a single additional data point
private void updateStatistics(double xValue, double yValue)
{
//Find sum of squares for x,y and sum of xy
n++;
sumX += xValue;
sumY += yValue;
sumXsquared += xValue * xValue;
sumYsquared += yValue * yValue;
sumXY += xValue * yValue;
//Caculate regression coefficients
n = (double) dataLength;
Sxx = sumXsquared - sumX * sumX / n;
Syy = sumYsquared - sumY * sumY / n;
Sxy = sumXY - sumX * sumY / n;
b = Sxy / Sxx;
a = (sumY - b * sumX) / n;
SSR = Sxy * Sxy / Sxx;
SSE = Syy - SSR;
calculateResiduals();
}
/**
* regression line y = a + bx.
*
* @param x
* @return double
* @throws InterpolationException
*/
public double getY(double x) throws InterpolationException
{
return a + b * x;
}
public String toString()
{
StringBuffer sb = new StringBuffer(1000);
sb.append("Regression Statistics for " + yName + " = a + b*" + xName);
sb.append("");
sb.append("Sample Statistics");
int n = this.getDataLength();
sb.append("Sample size n = " + n);
sb.append("Mean of " + yName + " Y bar = " + this.getYBar());
sb.append("s_Y");
sb.append("= " + Math.sqrt(this.getSyy() / ((float) (n - 1))));
sb.append("Pearson correlation R = " + this.getPearsonR());
sb.append("");
sb.append("Coefficient Estimates");
a = this.getIntercept();
b = this.getSlope();
sb.append("a = " + a);
sb.append("b = " + b);
sb.append("");
sb.append("95% Confidence Intervals");
if (n > 32)
{
t = t025[30];
}
else if (n > 2)
{
t = t025[n - 2];
}
else
{
t = Double.NaN;
}
MSE = this.getMSE();
if (n > 2)
{
bSE = Math.sqrt(MSE / this.getSxx());
}
else
{
bSE = Double.NaN;
}
aSE = bSE * Math.sqrt(this.getSumXSquared() / n);
aCILower = a - t * aSE;
aCIUpper = a + t * aSE;
sb.append("a : (" + aCILower + ", " + aCIUpper + ")");
bCILower = b - t * bSE;
bCIUpper = b + t * bSE;
sb.append("b : (" + bCILower + ", " + bCIUpper + ")");
sb.append("");
sb.append("Analysis of Variance");
sb.append("Source Degrees Freedom Sum of Squares");
sb.append("");
SSR = this.getSSR();
//allow one degree of freedom for mean
sb.append(
"model 1 "
+ SSR);
sb.append(
"error "
+ (n - 2)
+ " "
+ this.getSSE());
sb.append(
"total(corrected) "
+ (n - 1)
+ " "
+ this.getSyy());
sb.append("");
sb.append("MSE =" + MSE);
F = SSR / MSE;
sb.append("F = " + F + " ");
//sb.append("p = " + this.getP(F));
return sb.toString();
}
}
/****************************************************************************
* END OF FILE
****************************************************************************/

View File

@@ -0,0 +1,453 @@
///////////////////////////////////////////////////////////////////////////////
// Filename: $RCSfile: PolyInterpolation.java,v $
// Purpose: Some interpolation stuff.
// Language: Java
// Compiler: JDK 1.4
// Authors: Joerg K. Wegner
// Version: $Revision: 1.1 $
// $Date: 2003/07/22 19:25:30 $
// $Author: wegnerj $
//
// Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
//
///////////////////////////////////////////////////////////////////////////////
package wsi.ra.math.interpolation;
/*==========================================================================*
* IMPORTS
*==========================================================================*/
/**
* Defines the routines for the interpolation of data.
*/
public class PolyInterpolation
{
AbstractDataSet abstractDataSet = null;
boolean sloppy = true;
double[] polynomialCoefficients = null;
/*------------------------------------------------------------------------*
* constructor
*------------------------------------------------------------------------*/
/**
* Initializes this class.
*/
public PolyInterpolation() throws InterpolationException
{
this.abstractDataSet = null;
sloppy = true;
polynomialCoefficients = null;
}
/**
* Initializes this class and calculates the coefficients of the polynom.
*
* @param abstractDataSet the <code>AbstractDataSet</code>
*/
public PolyInterpolation(AbstractDataSet abstractDataSet)
throws InterpolationException
{
this.abstractDataSet = abstractDataSet;
sloppy = true;
this.polynomialCoefficients = calculatePolynomialCoefficients();
}
/**
* Initializes this class and calculates the coefficients of the polynom.
*
* @param abstractDataSet the <code>AbstractDataSet</code>
* @param sloppy if <code>true</code> Neville's algorithm which is used in the
* <code>polynomialInterpolation</code>-routines does only print a
* warning message on the screen and does not throw an
* <code>Exception</code> if two x values are identical.
*/
public PolyInterpolation(AbstractDataSet abstractDataSet, boolean sloppy)
throws InterpolationException
{
this.abstractDataSet = abstractDataSet;
this.sloppy = sloppy;
this.polynomialCoefficients = calculatePolynomialCoefficients();
}
/**
* Sets the new <code>AbstractDataSet</code> and calculates the coefficients
* of the polynom.
*
* @param abstractDataSet the <code>AbstractDataSet</code>
*/
public void setAbstractDataSet(AbstractDataSet abstractDataSet)
throws InterpolationException
{
this.abstractDataSet = abstractDataSet;
this.polynomialCoefficients = calculatePolynomialCoefficients();
}
/**
* Uses the polynom with the calculated coefficients to calculate the
* <code>y</code> value. This algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 5, pages 173-176.</a><br>
* The Neville's algorithm which is used in the <code>polynomialInterpolation</code>-
* routines returns also the error of this interpolated point.
*
* @param x the x value
* @return the interpolated y value
* @see #polynomialInterpolation(double)
* @see #polynomialInterpolation(AbstractDataSet, double)
* @see #polynomialInterpolation(double[], double[], double)
* @see #getYandDerivatives(double, int)
* @see #calculatePolynomialCoefficients()
* @see #calculatePolynomialCoefficients(AbstractDataSet)
* @see #calculatePolynomialCoefficients(double[], double[])
*/
public double getY(double x)
{
int n = polynomialCoefficients.length - 1;
double y = polynomialCoefficients[n];
for (int j = n - 1; j >= 0; j--)
y = y * x + polynomialCoefficients[j];
return y;
}
/**
* Uses the polynom with the calculated coefficients to calculate the
* <code>y</code> value and the derivatives at the point <code>x</code>,
* <code>y</code>. This algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 5, pages 173-176.</a><br>
* The Neville's algorithm which is used in the <code>polynomialInterpolation</code>-
* routines returns also the error of this interpolated point.
*
* @param x the x value
* @param ndDerivateNumber the number of the calculated derivatives
* @return the interpolated y value at ...[0], the 1st derivativ value at
* ...[1], the 2nd derivativ at ...[2] and so on ...
* @see #getY(double)
* @see #polynomialInterpolation(double)
* @see #polynomialInterpolation(AbstractDataSet, double)
* @see #polynomialInterpolation(double[], double[], double)
* @see #calculatePolynomialCoefficients()
* @see #calculatePolynomialCoefficients(AbstractDataSet)
* @see #calculatePolynomialCoefficients(double[], double[])
*/
public double[] getYandDerivatives(double x, int ndDerivateNumber)
throws InterpolationException
{
if (ndDerivateNumber < 0)
throw new InterpolationException("Negative derivative numbers make no sense.");
else if (ndDerivateNumber == 0)
{
double[] pd = new double[1];
pd[0] = getY(x);
return pd;
}
int nnd, j, i;
int nc = polynomialCoefficients.length - 1;
double[] pd = new double[ndDerivateNumber + 1];
double cnst = 1.0;
pd[0] = polynomialCoefficients[nc];
for (j = 1; j <= ndDerivateNumber; j++)
pd[j] = 0.0;
for (i = nc - 1; i >= 0; i--)
{
nnd = (ndDerivateNumber < (nc - i) ? ndDerivateNumber : nc - i);
for (j = nnd; j >= 1; j--)
pd[j] = pd[j] * x + pd[j - 1];
pd[0] = pd[0] * x + polynomialCoefficients[i];
}
for (i = 2; i <= ndDerivateNumber; i++)
{
cnst *= i;
pd[i] *= cnst;
}
return pd;
}
/**
* Neville's interpolation algorithm. This algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 3, pages 108-122.</a><br>
*
* @param x the x value
* @return the interpolated y value and the interpolation error
* @see #polynomialInterpolation(AbstractDataSet, double)
* @see #polynomialInterpolation(double[], double[], double)
* @see #getY(double)
* @see #getYandDerivatives(double, int)
* @see #calculatePolynomialCoefficients()
* @see #calculatePolynomialCoefficients(AbstractDataSet)
* @see #calculatePolynomialCoefficients(double[], double[])
*/
public PolynomialInterpolationResult polynomialInterpolation(double x)
throws InterpolationException
{
if (abstractDataSet == null)
throw new InterpolationException(
"No data." + " The AbstractDataSet was not defined.");
return polynomialInterpolation(
abstractDataSet.getXData(),
abstractDataSet.getYData(),
x);
}
/**
* Neville's interpolation algorithm. This algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 3, pages 108-122.</a><br>
*
* @param abstractDataSet the <code>AbstractDataSet</code>
* @param x the x value
* @return the interpolated y value and the interpolation error
* @see #polynomialInterpolation(double)
* @see #polynomialInterpolation(double[], double[], double)
* @see #getY(double)
* @see #getYandDerivatives(double, int)
* @see #calculatePolynomialCoefficients()
* @see #calculatePolynomialCoefficients(AbstractDataSet)
* @see #calculatePolynomialCoefficients(double[], double[])
*/
public PolynomialInterpolationResult polynomialInterpolation(
AbstractDataSet abstractDataSet,
double x)
throws InterpolationException
{
if (abstractDataSet == null)
throw new InterpolationException(
"No data." + " The AbstractDataSet was not defined.");
return polynomialInterpolation(
abstractDataSet.getXData(),
abstractDataSet.getYData(),
x);
}
/**
* Neville's interpolation algorithm. This algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 3, pages 108-122.</a><br>
*
* @param xa the array of x values
* @param ya the array of y values
* @param x the x value
* @return the interpolated y value and the interpolation error
* @see #polynomialInterpolation(double)
* @see #polynomialInterpolation(AbstractDataSet, double)
* @see #getY(double)
* @see #getYandDerivatives(double, int)
* @see #calculatePolynomialCoefficients()
* @see #calculatePolynomialCoefficients(AbstractDataSet)
* @see #calculatePolynomialCoefficients(double[], double[])
*/
public PolynomialInterpolationResult polynomialInterpolation(
double[] xa,
double[] ya,
double x)
throws InterpolationException
{
if (xa == null || ya == null)
throw new InterpolationException("No data.");
int i, m, ns = 1;
double den, dif, dift, ho, hp, w;
double[] c = new double[xa.length + 1];
double[] d = new double[xa.length + 1];
PolynomialInterpolationResult result =
new PolynomialInterpolationResult();
dif = Math.abs(x - xa[1 - 1]);
for (i = 1; i <= xa.length; i++)
{
if ((dift = Math.abs(x - xa[i - 1])) < dif)
{
ns = i;
dif = dift;
}
c[i] = ya[i - 1];
d[i] = ya[i - 1];
//System.out.println("x"+xa[i-1]+" y"+ya[i-1]);
}
result.y = ya[ns - 1];
//System.out.println("y="+result.y+" ns="+ns);
ns--;
for (m = 1; m < xa.length; m++)
{
for (i = 1; i <= xa.length - m; i++)
{
ho = xa[i - 1] - x;
hp = xa[i + m - 1] - x;
w = c[i + 1] - d[i];
if ((den = ho - hp) == 0.0)
{
if (sloppy)
{
System.out.println(
"Two identical x values. The values must be distinct.");
den = 1.0;
}
else
throw new InterpolationException("Two identical x values.");
}
den = w / den;
d[i] = hp * den;
c[i] = ho * den;
}
result.y
+= (result.yError =
(2 * ns < (xa.length - m) ? c[ns + 1] : d[ns--]));
}
return result;
}
/**
* Calculates the coefficients of a polynom of the grade <code>N-1</code>. This
* interpolation algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 3, pages 108-122.</a><br>
*
* @return the array with the polynomial coefficients y = ...[0] +
* ...[1]*x<SUP>2</SUP> + ...[2]*x<SUP>3</SUP> + ...
* @see #calculatePolynomialCoefficients(AbstractDataSet)
* @see #calculatePolynomialCoefficients(double[], double[])
* @see #polynomialInterpolation(double)
* @see #polynomialInterpolation(AbstractDataSet, double)
* @see #polynomialInterpolation(double[], double[], double)
* @see #getY(double)
* @see #getYandDerivatives(double, int)
*/
public double[] calculatePolynomialCoefficients()
throws InterpolationException
{
if (abstractDataSet == null)
throw new InterpolationException(
"No data." + " The AbstractDataSet was not defined.");
return calculatePolynomialCoefficients(
abstractDataSet.getXData(),
abstractDataSet.getYData());
}
/**
* Calculates the coefficients of a polynom of the grade <code>N-1</code>. This
* interpolation algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 3, pages 108-122.</a><br>
*
* @param abstractDataSet the <code>AbstractDataSet</code>
* @return the array with the polynomial coefficients y = ...[0] +
* ...[1]*x<SUP>2</SUP> + ...[2]*x<SUP>3</SUP> + ...
* @see #calculatePolynomialCoefficients()
* @see #calculatePolynomialCoefficients(double[], double[])
* @see #polynomialInterpolation(double)
* @see #polynomialInterpolation(AbstractDataSet, double)
* @see #polynomialInterpolation(double[], double[], double)
* @see #getY(double)
* @see #getYandDerivatives(double, int)
*/
public double[] calculatePolynomialCoefficients(AbstractDataSet abstractDataSet)
throws InterpolationException
{
if (abstractDataSet == null)
throw new InterpolationException(
"No data." + " The AbstractDataSet was not defined.");
return calculatePolynomialCoefficients(
abstractDataSet.getXData(),
abstractDataSet.getYData());
}
/**
* Calculates the coefficients of a polynom of the grade <code>N-1</code>. This
* interpolation algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 3, pages 108-122.</a><br>
*
* @param x the array of x values
* @param y the array of y values
* @return the array with the polynomial coefficients y = ...[0] +
* ...[1]*x<SUP>2</SUP> + ...[2]*x<SUP>3</SUP> + ...
* @see #calculatePolynomialCoefficients()
* @see #calculatePolynomialCoefficients(AbstractDataSet)
* @see #polynomialInterpolation(double)
* @see #polynomialInterpolation(AbstractDataSet, double)
* @see #polynomialInterpolation(double[], double[], double)
* @see #getY(double)
* @see #getYandDerivatives(double, int)
*/
public double[] calculatePolynomialCoefficients(double x[], double y[])
{
int k, j, i, n = x.length - 1;
double phi, ff, b;
double[] s = new double[n + 1];
double[] cof = new double[n + 1];
for (i = 0; i <= n; i++)
{
s[i] = cof[i] = 0.0;
}
s[n] = -x[0];
for (i = 1; i <= n; i++)
{
for (j = n - i; j <= n - 1; j++)
{
s[j] -= x[i] * s[j + 1];
}
s[n] -= x[i];
}
for (j = 0; j < n; j++)
{
phi = n + 1;
for (k = n; k >= 1; k--)
{
phi = k * s[k] + x[j] * phi;
}
ff = y[j] / phi;
b = 1.0;
for (k = n; k >= 0; k--)
{
cof[k] += b * ff;
b = s[k] + x[j] * b;
}
}
return cof;
}
}
/****************************************************************************
* END OF FILE
****************************************************************************/

View File

@@ -0,0 +1,56 @@
///////////////////////////////////////////////////////////////////////////////
// Filename: $RCSfile: PolynomialInterpolationResult.java,v $
// Purpose: Some interpolation stuff.
// Language: Java
// Compiler: JDK 1.4
// Authors: Joerg K. Wegner
// Version: $Revision: 1.1 $
// $Date: 2003/07/22 19:25:36 $
// $Author: wegnerj $
//
// Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
//
///////////////////////////////////////////////////////////////////////////////
package wsi.ra.math.interpolation;
/*==========================================================================*
* IMPORTS
*==========================================================================*/
/*==========================================================================*
* CLASS DECLARATION
*==========================================================================*/
/**
* The result data for a polynomial interpolation.
*/
public class PolynomialInterpolationResult
{
/*-------------------------------------------------------------------------*
* public member variables
*-------------------------------------------------------------------------*/
public double y = Double.NaN;
public double yError = Double.NaN;
/*------------------------------------------------------------------------*
* constructor
*------------------------------------------------------------------------*/
public PolynomialInterpolationResult()
{
y = Double.NaN;
yError = Double.NaN;
}
public PolynomialInterpolationResult(double y, double yError)
{
this.y = y;
this.yError = yError;
}
}
/****************************************************************************
* END OF FILE
****************************************************************************/

View File

@@ -0,0 +1,299 @@
///////////////////////////////////////////////////////////////////////////////
// Filename: $RCSfile: SplineInterpolation.java,v $
// Purpose: Some interpolation stuff.
// Language: Java
// Compiler: JDK 1.4
// Authors: Joerg K. Wegner
// Version: $Revision: 1.1 $
// $Date: 2003/07/22 19:25:42 $
// $Author: wegnerj $
//
// Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
//
///////////////////////////////////////////////////////////////////////////////
package wsi.ra.math.interpolation;
/*==========================================================================*
* IMPORTS
*==========================================================================*/
/**
* Defines the routines for the spline interpolation of data.
*/
public class SplineInterpolation
{
AbstractDataSet abstractDataSet = null;
double[] secondDerivative = null;
double[] xArray = null;
double[] yArray = null;
boolean ascendingData = true;
/*------------------------------------------------------------------------*
* constructor
*------------------------------------------------------------------------*/
/**
* Initializes this class.
*/
public SplineInterpolation() throws InterpolationException
{
this.abstractDataSet = null;
this.secondDerivative = null;
}
/**
* Initializes this class and calculates the second derivative of the spline.
*
* @param abstractDataSet the <code>AbstractDataSet</code>
*/
public SplineInterpolation(AbstractDataSet abstractDataSet)
throws InterpolationException
{
this.setAbstractDataSet(abstractDataSet);
}
/**
* Sets the new <code>AbstractDataSet</code> and calculates the second
* derivative of the spline.
*
* @param abstractDataSet the <code>AbstractDataSet</code>
*/
public void setAbstractDataSet(AbstractDataSet abstractDataSet)
throws InterpolationException
{
this.abstractDataSet = abstractDataSet;
double[] x = abstractDataSet.getXData();
double[] y = abstractDataSet.getYData();
boolean ascending = false;
boolean descending = false;
int n = x.length;
xArray = new double[n];
yArray = new double[n];
xArray[n - 1] = x[0];
yArray[n - 1] = y[0];
for (int i = 0; i < n - 1; i++)
{
xArray[i] = x[n - i - 1];
yArray[i] = y[n - i - 1];
if (x[i] < x[i + 1])
{
//if(descending)throw new InterpolationException("The x values must be"+
// " in continous ascending/descending order.");
ascending = true;
}
else
{
//if(ascending)throw new InterpolationException("The x values must be"+
// " in continous ascending/descending order.");
descending = true;
}
}
ascendingData = ascending;
if (ascendingData)
{
xArray = null;
yArray = null;
xArray = abstractDataSet.getXData();
yArray = abstractDataSet.getYData();
}
this.secondDerivative =
spline(
xArray,
yArray,
(yArray[1] - yArray[0]) / (xArray[1] - xArray[0]),
(yArray[n - 1] - yArray[n - 2]) / (xArray[1] - xArray[n - 2]));
}
/**
* Uses the spline with the calculated second derivative values to calculate
* the <code>y</code> value. This algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 5, pages 173-176.</a><br>
*/
public double getY(double x) throws InterpolationException
{
return splineInterpolation(xArray, yArray, secondDerivative, x);
}
public double getDerivative(double x) throws InterpolationException
{
return splineInterpolatedDerivative(
xArray,
yArray,
secondDerivative,
x);
}
/**
* Calculates the second derivative of the data. It's important that the
* x<sub>i</sub> values of the function y<sub>i</sub>=f(x<sub>i</sub>) are
* in ascending order, as x<sub>0</sub>&lt;x<sub>1</sub>&lt;x<sub>2</sub>&lt;... .
* This algorithm was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 3, pages 113-116.</a><br>
*/
public double[] spline(double[] x, double[] y, double yp0, double ypn)
throws InterpolationException
{
if (x[0] > x[1])
throw new InterpolationException(
"The x values must be" + " in ascending order.");
int n = x.length;
double[] y2 = new double[n];
double[] u = new double[n - 1];
int i, k;
double p, qn, sig, un;
if (yp0 > 0.99e30)
y2[0] = u[0] = 0.0;
else
{
y2[0] = -0.5;
u[0] =
(3.0 / (x[1] - x[0])) * ((y[1] - y[0]) / (x[1] - x[0]) - yp0);
}
for (i = 2; i <= n - 1; i++)
{
sig = (x[i - 1] - x[i - 2]) / (x[i] - x[i - 2]);
p = sig * y2[i - 2] + 2.0;
y2[i - 1] = (sig - 1.0) / p;
u[i - 1] =
(y[i] - y[i - 1]) / (x[i] - x[i - 1])
- (y[i - 1] - y[i - 2]) / (x[i - 1] - x[i - 2]);
u[i - 1] =
(6.0 * u[i - 1] / (x[i] - x[i - 2]) - sig * u[i - 2]) / p;
}
if (ypn > 0.99e30)
{
qn = un = 0.0;
}
else
{
qn = 0.5;
un =
(3.0 / (x[n - 1] - x[n - 2]))
* (ypn - (y[n - 1] - y[n - 2]) / (x[n - 1] - x[n - 2]));
}
y2[n - 1] = (un - qn * u[n - 2]) / (qn * y2[n - 2] + 1.0);
for (k = n - 1; k >= 1; k--)
{
y2[k - 1] = y2[k - 1] * y2[k] + u[k - 1];
}
return y2;
}
/**
* Calculates the second derivative of the data. This algorithm
* was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 3, pages 113-116.</a><br>
*/
public double splineInterpolation(
double[] xa,
double[] ya,
double[] y2a,
double x)
throws InterpolationException
{
int n = xa.length;
if (n != ya.length || n != y2a.length)
{
throw new InterpolationException("Arrays have different lengths.");
}
double y;
int klo, khi, k;
double h, b, a;
klo = 0;
khi = n - 1;
while (khi - klo > 1)
{
k = (khi + klo) >> 1;
if (xa[k] > x)
khi = k;
else
klo = k;
}
h = xa[khi] - xa[klo];
//System.out.println(""+x+" between "+xa[khi]+" "+xa[klo]);
if (h == 0.0)
throw new InterpolationException("Two identical x values. The values must be distinct.");
a = (xa[khi] - x) / h;
b = (x - xa[klo]) / h;
y =
a * ya[klo]
+ b * ya[khi]
+ ((a * a * a - a) * y2a[klo] + (b * b * b - b) * y2a[khi])
* (h * h)
/ 6.0;
return y;
}
/**
* Calculates the second derivative of the data. This algorithm
* was taken from:<br>
* <a href="http://www.ulib.org/webRoot/Books/Numerical_Recipes/" target="_top">
* William H. Press, Saul A. Teukolosky, William T. Vetterling, Brian P. Flannery,
* "Numerical Recipes in C - The Art of Scientific Computing", Second Edition,
* Cambridge University Press,
* ISBN 0-521-43108-5,
* chapter 3, pages 113-116.</a><br>
*/
public double splineInterpolatedDerivative(
double[] xa,
double[] ya,
double[] y2a,
double x)
throws InterpolationException
{
int n = xa.length;
if (n != ya.length || n != y2a.length)
{
throw new InterpolationException("Arrays have different lengths.");
}
double dydx;
int klo, khi, k;
double h, b, a;
klo = 0;
khi = n - 1;
while (khi - klo > 1)
{
k = (khi + klo) >> 1;
if (xa[k] > x)
khi = k;
else
klo = k;
}
h = xa[khi] - xa[klo];
//System.out.println(""+x+" between "+xa[khi]+" "+xa[klo]);
if (h == 0.0)
throw new InterpolationException("Two identical x values. The values must be distinct.");
a = (xa[khi] - x) / h;
b = (x - xa[klo]) / h;
dydx =
(ya[khi] - ya[klo]) / h
- ((3 * (a * a) - 1) * h * y2a[klo]) / 6.0
+ ((3 * (b * b) - 1) * h * y2a[khi]) / 6.0;
return dydx;
}
}
/****************************************************************************
* END OF FILE
****************************************************************************/