**13.5****
****Function Interpolation** (.NET, C#, CSharp, VB, Visual Basic, F#)

Abstract class **TabulatedFunction**
extends **OneVariableFunction** (Section 13.1).
Rather than encapsulating an arbitrary function delegate, **TabulatedFunction** holds paired vectors
of known *x*- and *y*-values.
The function can be evaluated at arbitrary points using derived *function interpolation classes*. As a **OneVariableFunction**, a **TabulatedFunction** can be manipulated
algebraically. Numerical integrals and derivatives can also be computed.

A **TabulatedFunction**
type is constructed from paired vectors of known *x*-
and *y*-values. The values for *x* must be in strictly increasing order.
Class **TabulatedFunction** inherits
Function, Integrator,
and Differentiator properties from **OneVariableFunction** (Section 13.1).
Additionally, **TabulatedFunction**
provides these properties:

● X gets the vector of x-values represented by the function.

● Y gets the vector of y-values represented by the function.

● NumberOfTabulatedValues gets the number of tabulated values.

The X and Y properties return a copy of the tabulated data.
Therefore, modifying the returned vectors does not change the **TabulatedFunction**.

To change the tabulated values represented by a **TabulatedFunction**, use the SetTabulatedValues() method. Provided GetX(), SetX(), GetY(), and SetY()
methods also enable you to get and set individual tabulated values, or
a range of values.

Class **TabulatedFunction**
inherits the Evaluate() method from **OneVariableFunction**. This method evaluates
the interpolated function at a given *x*-value,
or vector of *x*

Class **LinearSpline**
extends **TabulatedFunction** and represents
a function whose values are determined by linear interpolation between
tabulated values. For example:

Code Example – C# linear spline interpolation

var xValues = new DoubleVector(10, 0, 1); DoubleVector yValues = xValues * xValues; var ls = new LinearSpline( xValues, yValues ); double yInterpolated = ls.Evaluate( 3.5 );

Code Example – VB linear spline interpolation

Dim XValues As New DoubleVector(10, 0, 1) Dim YValues = XValues * XValues Dim LS As New LinearSpline(XValues, YValues) Dim YInterpolated = LS.Evaluate(3.5)

Evaluating *x*-values
outside the range of tabulated values returns the last know *y*-value. In the example above, ls.Evaluate( 9.5 ) == ls.Evaluate( 9 ).

Abstract class **CubicSpline**
extends **TabulatedFunction** and represents
a function whose values are determined by cubic spline interpolation
between the tabulated values. **NMath**
provides two concrete implementations of **CubicSpline**:
**NaturalCubicSpline** and **ClampedCubicSpline**. The *natural cubic spline* is a
cubic spline where the second derivative of the interpolating function
is required to be zero at the left and right endpoints. The *clamped cubic spline* is a
cubic spline where the first derivative of the interpolating function
is specified at the left and right endpoints.

For example, this code creates a **NaturalCubicSpline**
to resample at a fixed sampling interval a cubic spline fit constructed
from data with a variable sampling interval:

Code Example – C# cubic spline interpolation

var x = new DoubleVector( "1.0 1.3 1.4 1.8 2.0"); var y = new DoubleVector( "2.4 4.6 4.7 2.3 1.0" ); var s = new NaturalCubicSpline( x, y ); var xx = new DoubleVector( "1.0 1.25 1.5 1.75 2.0"); DoubleVector yy = s.Evaluate( xx );

Code Example – VB cubic spline interpolation

Dim X As New DoubleVector("1.0 1.3 1.4 1.8 2.0") Dim Y As New DoubleVector("2.4 4.6 4.7 2.3 1.0") Dim S As New NaturalCubicSpline(X, Y) Dim XX As New DoubleVector("1.0 1.25 1.5 1.75 2.0") Dim YY As DoubleVector = S.Evaluate(XX)

This code creates a **ClampedCubicSpline**
that enforces endslopes of zero for the cubic spline fit:

Code Example – C# cubic spline interpolation

var s = new ClampedCubicSpline( x, y, 0, 0 );

Code Example – VB cubic spline interpolation

Dim S As New ClampedCubicSpline(X, Y, 0, 0)

Class **ClampedCubicSpline**
provides LeftEndSlope and RightEndSlope
properties for getting and setting the clamped values, and method SetEndSlopes() for modifying them together.

Evaluating *x*-values
outside the range of tabulated values in a **NaturalCubicSpline
**returns the last know *y*-value.
In a **ClampedCubicSpline**, the last
fitted cubic is used, or a linear extrapolation is performed in the case
of only 2 or 3
tabulated vaules.

Class **SmoothCubicSpline** derives
from **TabulatedFunction**. The API
is the same as for other cubic spline classes, with the addition of a
smoothing factor, P. The smoothing factor
takes values in the range 0 <= p <= 1,
where 0 results in zero curvature (linear
interpolation), and 1 results to a conventional
cubic spline.

**Creating Your Own Interpolation Classes**

The **NMath**
interpolation class framework is easily extensible. To create your own
interpolation class, simply extend **TabulatedFunction**.
Specify a delegate function of type Func<double,
double> for the instance variable function
in the base class **OneVariableFunction**.
This delegate computes and returns values for arbitrary *x*-values.

In addition, deriving classes may override the virtual
method ProcessTabulatedValues(). This method
is invoked by **TabulatedFunction**
instances whenever the tabulated values are changed.