I have a piecewise mathematical function that needs to be evaluated a lot of times, as a subfunction in quad
and curve_fit
routines. In trying to improve the performance of my program, I found that a lot of time was spent in evaluating conditions in the if
statements.
My function looks like :
def myfunc(x):
if x < x0:
return 0.
elif x < x1:
return func1(x)
elif x < x2:
return c2
else:
return 0.
def func1(x):
return c1*((math.cos(x)/math.cos(x2))**2 - 1)**0.5
Is there a way to define the same function to reduce the function evaluation time?
note:
x0 < x1 < x2
c1, c2 are constants