-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
148 additions
and
74 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
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,70 @@ | ||
import numpy as np | ||
from numba import float64 | ||
from numba import guvectorize | ||
from numba import vectorize | ||
|
||
|
||
def interp_reg(x, f): | ||
""" | ||
Linear interpolation over a regular grid | ||
Parameters | ||
---------- | ||
x : array, | ||
assumed to be a linear regular grid | ||
f : array, func | ||
:return: an interpolation function | ||
""" | ||
|
||
x0 = x[0] | ||
dx_inv = 1 / (x[1] - x0) | ||
|
||
@vectorize([float64(float64)], nopython=True) | ||
def _interp_reg(x_intp): | ||
x_dx = (x_intp - x0) * dx_inv | ||
ind = int(x_dx) | ||
w = x_dx - ind | ||
return f[ind] * (1 - w) + f[ind + 1] * w | ||
|
||
return _interp_reg | ||
|
||
|
||
def interp_reg_semilogx_vec(x, f, log=np.log): | ||
""" | ||
Linear interpolation over a logarithmic regular grid | ||
Parameters | ||
---------- | ||
x : array | ||
assumed to be a logarithmic regular grid | ||
f : array | ||
:return: an interpolation function | ||
""" | ||
|
||
x0 = x[0] | ||
dx_inv = 1 / log(x[1] / x0) | ||
|
||
@vectorize([float64(float64)]) | ||
def _interp_reg(x_intp): | ||
x_dx = log(x_intp / x0) * dx_inv | ||
ind = int(x_dx) | ||
w = x_dx - ind | ||
return f[ind] * (1 - w) + f[ind + 1] * w | ||
|
||
return _interp_reg | ||
|
||
|
||
@guvectorize([(float64[:], float64[:], float64, float64, float64[:])], '(n),(m),(),()->(n)') | ||
def interp_reg_semilogx(x_intp, f, x0, x1, res): | ||
""" | ||
Linear interpolation over a logarithmic regular grid | ||
""" | ||
dx_inv = 1 / np.log(x1 / x0) | ||
for i in range(x_intp.size): | ||
x_dx = np.log(x_intp[i] / x0) * dx_inv | ||
ind = int(x_dx) | ||
w = x_dx - ind | ||
res[i] = f[ind] * (1 - w) + f[ind + 1] * w |
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
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,30 @@ | ||
import numpy as np | ||
|
||
from scrrpy.utils import interp_reg | ||
from scrrpy.utils import interp_reg_semilogx | ||
from scrrpy.utils import interp_reg_semilogx_vec | ||
|
||
|
||
def test_interp_reg(tol=1e-4): | ||
x = np.linspace(0, 1, 100) | ||
x_intp = np.random.rand(1000) | ||
y = x**2 | ||
f_intp = interp_reg(x, y)(x_intp) | ||
err = max(abs(x_intp**2 - f_intp)) | ||
assert err < tol, err | ||
|
||
def test_interp_reg_semilogx(tol=1e-3): | ||
x = np.logspace(-5, 5, 1000) | ||
x_intp = 10**((1 - 2*np.random.rand(1000))*5) | ||
y = np.log(x)**2 | ||
f_intp = interp_reg_semilogx(x_intp, y, x[0], x[1]) | ||
err = max(abs(np.log(x_intp)**2 - f_intp)) | ||
assert err < tol, err | ||
|
||
def test_interp_reg_semilogx_vec(tol=1e-3): | ||
x = np.logspace(-5, 5, 1000) | ||
x_intp = 10**((1 - 2*np.random.rand(1000))*5) | ||
y = np.log(x)**2 | ||
f_intp = interp_reg_semilogx_vec(x, y)(x_intp) | ||
err = max(abs(np.log(x_intp)**2 - f_intp)) | ||
assert err < tol, err |