public double Interpolate(double x)
{
if (y2 == null)
{
PreCompute();
}
IList<double> xa = this.points.Keys;
IList<double> ya = this.points.Values;
int n = ya.Count;
int klo = 0; // We will find the right place in the table by means of
int khi = n - 1; // bisection. This is optimal if sequential calls to this
while (khi - klo > 1)
{
// routine are at random values of x. If sequential calls
int k = (khi + klo) >> 1;// are in order, and closely spaced, one would do better
if (xa[k] > x)
{
khi = k; // to store previous values of klo and khi and test if
}
else
{
klo = k;
}
}
double h = xa[khi] - xa[klo];
double a = (xa[khi] - x) / h;
double b = (x - xa[klo]) / h;
// Cubic spline polynomial is now evaluated.
return a * ya[klo] + b * ya[khi] +
((a * a * a - a) * y2[klo] + (b * b * b - b) * y2[khi]) * (h * h) / 6.0;
}