-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import numpy as np | ||
from numba import jit | ||
|
||
@jit(nopython=True) | ||
def interp1d(grid, vals, x): | ||
""" | ||
Linearly interpolate (grid, vals) to evaluate at x. | ||
Parameters | ||
---------- | ||
grid and vals are numpy arrays, x is a float | ||
Returns | ||
------- | ||
a float, the interpolated value | ||
""" | ||
|
||
a, b, G = np.min(grid), np.max(grid), len(grid) | ||
|
||
s = (x - a) / (b - a) | ||
|
||
q_0 = max(min(int(s * (G - 1)), (G - 2)), 0) | ||
v_0 = vals[q_0] | ||
v_1 = vals[q_0 + 1] | ||
|
||
λ = s * (G - 1) - q_0 | ||
|
||
return (1 - λ) * v_0 + λ * v_1 | ||
|
||
|
||
@jit(nopython=True) | ||
def interp1d_vectorized(grid, vals, x_vec): | ||
""" | ||
Linearly interpolate (grid, vals) to evaluate at x_vec. | ||
All inputs are numpy arrays. | ||
Return value is a numpy array of length len(x_vec). | ||
""" | ||
|
||
out = np.empty_like(x_vec) | ||
|
||
for i, x in enumerate(x_vec): | ||
out[i] = interp1d(grid, vals, x) | ||
|
||
return out | ||
|
||
|