I need to run a function that finds the root of an equation that depends on one parameter over a large set of data (the real equation is much more complicated but formally equivalent).
def f(x):
return numpy.optimize.brentq(lambda y:numpy.exp(-abs(x)*y)-y,0,1)
Is it convenient to vectorize it?
I tried to use numpy.vectorize(f) that works fine, but I think at the same speed of a python for loop.
Is there some kind of manual vectorization to better exploit the power of numpy array computation?
If it's just a single parameter function, you can just interpolate the inverse function (tabulate x and y, interpolate x vs y, evaluate the interpolator at target values of y).
And no, manual vectorization won't let you avoid a python loop (unless you want to hack on a compiled level and wrap a C level loop around the compiled part of either brentq or fsolve --- and even that is likely slower than interpolating the inverse function.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With