# How to get an interpolation weight from a mathematical definition

It was recently explained to me that a "Nearest neighbor" kernel for 1D interpolation can be implemented like this using NumPy

def nearest(delta):
delta = abs(delta)
if delta <= 0.5:
return numpy.asarray([0,1])
else:
return numpy.asarray([1,0])


Whereas the mathematical definition of nearest neighbor is

$$h_{nn}(\delta) = \begin{cases} 1 & \text{if}& -0.5 \le \delta < 0.5 \\ 0 & otherwise \\ \end{cases}$$

Similarly linear interpolation, which in NumPy can be expressed as

def linear(delta):
delta = abs(delta)
return [delta,1-delta]


But the mathematical definition for it goes

$$h_{lin}(\delta) = \begin{cases} 1-|\delta| &\text{if}& 0 \le |\delta| < 1 \\ 0 & \text{if}& 1 \le |\delta| \end{cases}$$

My question is how to form a kernel with weights out of these mathematical definitions, as the code belonging to them does not paint the same picture that the mathematical definitions paint.

• I don't quite understand your question but think about this: nearest neighbor rounding is done by adding 1/2 the interval (I presume delta), and rounding down. Try it by hand to see it, then the algorithm should be equivalent. – rrogers May 28 '19 at 19:50