EconForge / interpolation.py

BSD 2-Clause "Simplified" License
123 stars 35 forks source link

1d linear interpolation with irregular grid #21

Closed albop closed 5 years ago

albop commented 6 years ago

This could be implemented in a new file splines/linear.py.

Proposed api would be interp(x,y,u) (similar to scipy's version). It would interpolate the function given by y=f(x) at point u. If u is a scalar the output is a scalar. If u is a vector then output is a vector too.

One could extend it to vector valued function: if y is a 2d array, the function to interpolate would be such that f(x[i])=y[i,:]. Consequently the output out would be a vector if u is a scalar (with out.shape[0]==y.shape[1]) and a matrix if u is a vector (with out.shape==(u.shape[0],y.shape[1]))

Maybe it would be a good idea to implement first an inplace version interp(x,y,u,out) that doesn't allocate output.

albop commented 6 years ago

Regarding the allocation of output, it's probably best to not worry about it or implement it with a keyword interp(x,y,u,out=). That way the function can be generalized naturally to many dimensions with scalar or vector values: interp(x1,x2,x3,y,u) and interp(x1,x2,x3,y,u, out=...)