Skip to main content

Interpolation

CSharpNumerics provides a unified interpolation API supporting linear and logarithmic scales:

  • Linear
  • Logโ€“Log (log x, log y)
  • Linโ€“Log (lin x, log y)
  • Logโ€“Lin (log x, lin y)

All methods are routed through one central function.

public enum InterpolationType
{
Linear,
Logarithmic, // logโ€“log
LogLin,
LinLog
}
double Interpolate<T>(
this IEnumerable<T> source,
Func<T, (double x, double y)> selector,
double index,
InterpolationType type);

Example:

var data = new List<Serie>
{
new Serie { Index = 1, Value = 10 },
new Serie { Index = 10, Value = 100 }
};

double y = data.Interpolate(
p => (p.Index, p.Value),
3.5,
InterpolationType.Linear
);

๐Ÿ“ Polynomial Interpolationโ€‹

Passes a single polynomial of degree Nโˆ’1N-1 through all NN data points.

double[] x = { 0, 1, 2, 3 };
double[] y = { 1, 2, 0, 5 };
var poly = new PolynomialInterpolation(x, y);

double val = poly.Evaluate(1.5); // Lagrange basis form
double val2 = poly.EvaluateNewton(1.5); // Newton divided-difference
var (val3, err) = poly.EvaluateNeville(1.5); // Neville with error estimate

// Or via the extension method:
double val4 = data.Interpolate(p => (p.Index, p.Value), 1.5, InterpolationType.Polynomial);

๐ŸŒŠ Cubic Spline Interpolationโ€‹

Piecewise cubic polynomials with C2C^2 continuity. Three boundary conditions:

BoundaryDescription
NaturalSโ€ฒโ€ฒ(x0)=Sโ€ฒโ€ฒ(xn)=0S''(x_0) = S''(x_n) = 0 (free ends)
ClampedFirst derivative specified at endpoints
NotAKnotThird derivative continuous at second & second-to-last knot
double[] x = { 0, 1, 2, 3, 4 };
double[] y = { 0, 1, 0, 1, 0 };

var spline = new CubicSplineInterpolation(x, y); // Natural
var clamped = new CubicSplineInterpolation(x, y, SplineBoundary.Clamped, 1.0, -1.0);
var nak = new CubicSplineInterpolation(x, y, SplineBoundary.NotAKnot);

double val = spline.Evaluate(2.5);
double dydx = spline.Derivative(2.5); // first derivative
double d2y = spline.SecondDerivative(2.5); // curvature

// Or via extension method:
double val2 = data.Interpolate(p => (p.Index, p.Value), 2.5, InterpolationType.CubicSpline);

๐Ÿ”ข Rational Interpolationโ€‹

Ratio of two polynomials โ€” handles poles and near-singularities better than polynomials.

double[] x = { 0, 1, 2, 3, 4 };
double[] y = { 1.0, 0.5, 0.333, 0.25, 0.2 }; // โ‰ˆ 1/(1+x)
var rat = new RationalInterpolation(x, y);

var (val, err) = rat.Evaluate(1.5); // Bulirschโ€“Stoer + error estimate
double val2 = rat.EvaluateFloaterHormann(1.5); // barycentric, guaranteed pole-free

๐ŸŽต Trigonometric Interpolationโ€‹

Best for periodic functions. Builds a trigonometric polynomial from the data.

int N = 16;
double[] x = new double[N], y = new double[N];
for (int i = 0; i < N; i++)
{
x[i] = 2 * Math.PI * i / N;
y[i] = Math.Sin(x[i]) + 0.5 * Math.Cos(2 * x[i]);
}

var trig = new TrigonometricInterpolation(x, y, period: 2 * Math.PI);
double val = trig.Evaluate(Math.PI / 3);
double dydx = trig.Derivative(Math.PI / 3);

// Fourier coefficients
double[] a = trig.CosineCoefficients; // a_0, a_1, ..., a_M
double[] b = trig.SineCoefficients; // b_0, b_1, ..., b_M

// Or via extension method:
double val2 = data.Interpolate(p => (p.Index, p.Value), 1.5, InterpolationType.Trigonometric);

๐ŸŒ Multivariate Interpolationโ€‹

For scattered data in multiple dimensions.

Inverse Distance Weighting (IDW / Shepard)

double[][] points = {
new[] { 0.0, 0.0 }, new[] { 1.0, 0.0 },
new[] { 0.0, 1.0 }, new[] { 1.0, 1.0 }, new[] { 0.5, 0.5 }
};
double[] values = points.Select(p => p[0] * p[0] + p[1] * p[1]).ToArray();

var interp = new MultivariateInterpolation(points, values);
double val = interp.EvaluateIDW(new[] { 0.25, 0.75 }, power: 2);

Radial Basis Functions (RBF)

double val = interp.EvaluateRBF(new[] { 0.25, 0.75 }, RbfKernel.Gaussian);
KernelFormula
Gaussianฯ•(r)=eโˆ’(r/ฮต)2\phi(r) = e^{-(r/\varepsilon)^2}
Multiquadricฯ•(r)=1+(r/ฮต)2\phi(r) = \sqrt{1 + (r/\varepsilon)^2}
InverseMultiquadricฯ•(r)=1/1+(r/ฮต)2\phi(r) = 1/\sqrt{1 + (r/\varepsilon)^2}
ThinPlateSplineฯ•(r)=r2lnโก(r)\phi(r) = r^2 \ln(r)
Cubicฯ•(r)=r3\phi(r) = r^3

Bilinear / Trilinear (Regular Grids)

double val2d = MultivariateInterpolation.Bilinear(xGrid, yGrid, gridValues, xi, yi);
double val3d = MultivariateInterpolation.Trilinear(xGrid, yGrid, zGrid, gridValues, xi, yi, zi);