EconForge / interpolation.py

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

1D interpolation with eval_linear #89

Closed grburgess closed 3 years ago

grburgess commented 3 years ago

I'm trying to figure out how to use eval_linear for 1D interpolation. I know that I can use the interp function for this... but that is causing some pickle error when doing any kind of multiprocessing.

What I have tried is this:

grid = np.linspace(0., np.pi, 10) ,
value = np.sin(grid).reshape((1, 10))

eval_linear(grid, value, np.atleast_2d([0.5]))

But the output is not making sense.

Am I missing something?

albop commented 3 years ago

In the example you give, grid is 1d so values should be représented by a 1d Vector. In your example it id a 2d Vector. Also, grid should be a tupe of 1d components.

grburgess commented 3 years ago

@albop here I will try a better example.

1) the comma after the grid definition packs the grid in a tuple


grid = np.linspace(0., np.pi, 10),

value = np.sin(grid).reshape((1,10))

xx = np.linspace(0.,np.pi,2)

eval_linear(grid, value, np.atleast_1d(xx))

output:

array([0.00000000e+00, 3.42020143e-01, 6.42787610e-01, 8.66025404e-01,
       9.84807753e-01, 9.84807753e-01, 8.66025404e-01, 6.42787610e-01,
       3.42020143e-01, 1.22464680e-16])

It seems it only evals at the grid point and not the new points.

albop commented 3 years ago

Second instruction still looks looks wrong to me:

Le mar. 22 juin 2021 à 09:48, J. Michael Burgess @.***> a écrit :

@albop https://github.com/albop here I will try a better example.

  1. the comma after the grid definition packs the grid in a tuple

grid = np.linspace(0., np.pi, 10), value = np.sin(grid).reshape((1,10)) xx = np.linspace(0.,np.pi,2) eval_linear(grid, value, np.atleast_1d(xx))

output:

array([0.00000000e+00, 3.42020143e-01, 6.42787610e-01, 8.66025404e-01, 9.84807753e-01, 9.84807753e-01, 8.66025404e-01, 6.42787610e-01, 3.42020143e-01, 1.22464680e-16])

It seems it only evals at the grid point and not the new points.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/EconForge/interpolation.py/issues/89#issuecomment-865682958, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACDSKJICNGNZSLD3NBJNN3TUA56HANCNFSM47B3OCPQ .

albop commented 3 years ago

Here is a working version:

grid = np.linspace(0., np.pi, 10),
value = np.sin(grid[0])                   # a 1d array
xx = np.linspace(0.,np.pi,2)          # a 1d array
eval_linear(grid, value, xx[:,None]) # xx[:,None] is a 2x1 array
grburgess commented 3 years ago

Thanks! Now I get the logic