python - how to turn an array into a callable function -
i have np.piecewise
function turn callable.
for example, suppose have:
import numpy np x = np.linspace(0,10,1001) my_func = np.piecewise(x, [x<8, x>=8], [np.sin, np.cos])
i interested in making function my_callable_func
has reasonable evaluation of my_func. reasonable, either default previous step in x
, or use kind of linear approximation between successive x
values.
for example, in case x = [0, 0.01, 0.02, ...]
, given my_new_func(0.015)
, i'd return np.sin(0.01)
or that...
you wrap np.piecewise
call inside function definition,
in [1]: def my_callable_func(x): ...: return np.piecewise(x, [x<8, x>=8], [np.sin, np.cos]) ...: my_callable_func(0.015) out[1]: array(0.01499943750632809)
the value of original x
vector not matter. produces 0d numpy array, if necessary can cast float, return float(...)
.
Comments
Post a Comment