Skip to content

NearestNeighbor.Function.createNearestNeighborInterpolator

Christian d'Heureuse edited this page Sep 25, 2026 · 1 revision

commons-math-interpolation / NearestNeighbor / createNearestNeighborInterpolator

Function: createNearestNeighborInterpolator()

function createNearestNeighborInterpolator(xVals, yVals): UniFunction;

Defined in: NearestNeighbor.ts:37

Returns a nearest neighbor interpolating function for a dataset.

The returned function returns the y value of the interpolation point nearest to its argument. If the argument is exactly in the middle between two points, the y value of the right point is returned. Arguments below the first point return the y value of the first point, and arguments above the last point return the y value of the last point. If the argument is NaN, NaN is returned.

Unlike the other interpolation methods, this method also accepts fewer than two points: With no points, the returned function always returns NaN. With a single point, it returns the y value of that point for all arguments except NaN.

The passed arrays are copied, so the returned function does not depend on them.

Parameters

xVals

ArrayLike<number>

The arguments of the interpolation points, in strictly increasing order. The values must be finite.

yVals

ArrayLike<number>

The values of the interpolation points.

Returns

UniFunction

A function which interpolates the dataset.

Throws

Error If xVals and yVals have different lengths, or if xVals contains non-finite values or is not strictly increasing.

Clone this wiki locally