EconForge / interpolation.py

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

Returning a function for a non-grid domain (replacement for Scipy's `LinearNDInterpolation`) #36

Closed cdagnino closed 4 years ago

cdagnino commented 5 years ago

With LinearNDInterpolation it is possible to get an interpolation function on an irregular grid. For example

from scipy.interpolate import LinearNDInterpolator
interp_f = LinearNDInterpolator(points=[[1., 1.], [2.5, 2.], [2., 0.8]], 
                              values=[2., 10., 11.])
interp_f(2., 1.1)
#Gives: array(9.73076923)

Is there such functionality in interpolation.py, whether with one or multiple dimensions?

I saw that interpolation.interphandles non regular points, but the example only shows it returning points, not a function:

from interpolation import interp
x = np.linspace(0,1,100)**2 # non-uniform points
y = np.linspace(0,1,100)    # values
# interpolate at one point:
interp(x, y, 0.5)
albop commented 5 years ago

@cdagnino : if you use the code from master there is recent support for multilinear interpolation on non regular grid. See README file and functions eval_linear and interp. One constraint of the library is that we try to keep implementation compatible with numba's jitted environment. This is why no object interface is provided yet. Note that if you absolutely need a function, you can still do f = lambda x: eval_linear(grid, values, x) or f = lambda u: interp(x,y,u). It might even work in an njit function since numba now supports anonymous functions.

albop commented 4 years ago

Irregular grid are now supported