Industrial Data Science
in C# and .NET:
Simple. Fast. Reliable.

ILNumerics - Technical Computing

Modern High Performance Tools for Technical

Computing and Visualization in Industry and Science

tgt

# ILNumerics: One-Dimensional Interpolation Objects

Most one-dimensional interpolation functions in ILNumerics Interpolation Toolbox internally rely on interpolator objects. When calling functions like Interpolation.interp1(), internally, an interpolator object is created for the provided set of known values V. In a second step, the interpolator object is used to compute the new query points. Afterwards the interpolator object is disposed to free any intermediate results. For subsequent calls to Interpolation.interp1() the same steps are repeated.

Obviously, the repeated creation of such intermediate data can be prevented from, when subsequent interpolations need to be performed for the same set of known data values. In this case, it becomes more efficient to resort to the direct utilization of the interpolator objects. The objects compute any needed intermediate data at construction time. These data are stored in the interpolator object and used for multiple, subsequent computations of new query points, saving repeated computational efforts.

## Interpolator Objects

• LinearInterpolatorDouble() - interpolator object for linear interpolation. At least 2 points needed.
• NearestInterpolatorDouble() - Interpolator for nearest neighbor interpolation. It allows to calculate nearest, next and previous neighbor intepolation. at least 2 points needed.
• SplineInterpolatorDouble() - interpolator object for 1D interpolation using piecewise cubic splines. It allows the configuration of  the derivatives at the lower and upper end of the spline interpolations. Minimal data set needed: 4 points.
• CubicInterpolatorDouble() - interpolator for lagrange polynomial interpolation of order 3. Minimal data set needed: 4 points.
• PolynomialInterpolatorDouble() - interpolator object for lagrange polynomial of configurable order

Individual interpolator objects exist for all supported element types. Listed are the classes for System.Double. Similar types exist for System.SingleILNumerics.complex, and ILNumerics.fcomplex element types.

## Parameter Configuration

All interpolator objects provide a very similar API. The parameters are almost identical to those expected for interp1. Consult the corresponding documentation on the one-dimensional functions interface for details.

### Shape of Input and Output Arrays

The same rules apply for the dimensionality of V as for the interp1 function: if  V is provided as n-dimensional array, the columns of V are considered as m individual sets of measurements and interpolated values will be calculated along the columns of V. In this case, each call to Apply() produces a resulting array of the same size as V, having the first dimension replaced with the length corresponding to the new query points, as provided by Xn.

Some interpolator objects provide the option to define additional parameters, namely for spline, nearest neighbor and polynomial interpolation. Examples are given below.

## Utilization of Interpolator Objects

Interpolator objects are utilized in a three-steps process:

1. First, an Interpolator object is created by using the constructor.
2. Secondly, the Apply() method is used to determine new query positions Xn and compute interpolated values at those positions. Step 2 may be repeated multiple times.
3. Afterwards, the interpolator is disposed to free all its resources.

In order to automate the disposal in step 3, commonly, in .NET, such objects are recommended to be used inside a using block.

Example: utilization of an interpolator object.

## Additional Parameters for Spline Interpolator Objects

SplineInterpolatorXXX objects provide two additional optional parameters upperBoundDerivative and lowerBoundDerivative which by default are set to null. They determine the first derivatives at the lower and upper end of the spline interpolations. By default, not-a-knot splines are computed. Users can control the derivatives at the corresponding end of the spline created, by providing a single scalar value for all columns of V or an array with individual values for every column. Special values are: double.NaN for natural splines and double.PositiveInfinity or null for not-a-knot splines. More details are found in the class reference and the manual on spline interpolation.

Example: Create various spline interpolations using SplineInterpolatorSingle

## Additional Parameters for Polynomial Interpolator Objects

PolynomialInterpolatorXXX objects publish the additional optional constructor parameter order which is set to 0 by default. This parameter defines the order of the Lagrange polynomial function used for calculation of interpolated values. Order is allowed to range from 0 to X.Length - 1. If order is set to 0 or X.Length-1, interpolated values will be calculated using a global polynomial function.

Due to the known properties of polynomials of higher orders, for data length of more than 5..7 points, it is recommended to utilize SplineInterpolatorXXX instead.

Example: calculate interpolated values using PolynomialInterpolatorDouble of various orders.

## Additional Parameters in NearestInterpolatorDouble Constructor

NearestInterpolatorXXX objects provide the optional parameter method which is set to InterpolationMethod.nearest by default. Other options are: InterpolationMethod.next or InterpolationMethod.previous, to cause Apply() to return the sample point left from a new query point or right from a new query point.

Example: use NearestInterpolatorXXX to get nearest, next and previous values.