我偶然发现这个代码使用Gauss Kronrod数值方法集成函数,我推荐它用于我正在集成的函数。到目前为止,在我使用过的所有试验中,我发现它的准确性相当好。然而,它完全省略了解决方案的虚构部分。
以下是将ln(x)从-5加到0的示例,实部是正确的,但没有虚部:
# let's test integrazi for ln(x) from -1 to 2
import scipy
import cmath
from scipy import array
def quad_routine(func, a, b, x_list, w_list):
c_1 = (b-a)/2.0
c_2 = (b+a)/2.0
eval_points = map(lambda x: c_1*x+c_2, x_list)
func_evals = map(func, eval_points)
return c_1 * sum(array(func_evals) * array(w_list))
def quad_kronrod_15(func, a, b):
x_kr = [-0.991455371120813,-0.949107912342759, -0.864864423359769, -0.741531185599394, -0.586087235467691,-0.405845151377397, -0.207784955007898, 0.0, 0.207784955007898,0.405845151377397, 0.586087235467691, 0.741531185599394, 0.864864423359769, 0.949107912342759, 0.991455371120813]
w_kr = [0.022935322010529, 0.063092092629979, 0.104790010322250, 0.140653259715525, 0.169004726639267, 0.190350578064785, 0.204432940075298, 0.209482141084728, 0.204432940075298, 0.190350578064785, 0.169004726639267, 0.140653259715525, 0.104790010322250, 0.063092092629979, 0.022935322010529]
return quad_routine(func,a,b,x_kr, w_kr)
class Memoize(object):
def __init__(self, func):
self.func = func
self.eval_points = {}
def __call__(self, *args):
if args not in self.eval_points:
self.eval_points[args] = self.func(*args)
return self.eval_points[args]
def quadt(func,a,b):
func = Memoize(func) # Memoize function to skip repeated function calls.
k15 = quad_kronrod_15(func,a,b)
# I don't have much faith in this error estimate taken from wikipedia
# without incorporating how it should scale with changing limits
return k15
def INTEGRATEME(func, **kwargs): # INTEGRATEZI short for Integrating from zero to infinity
def real_func(x):
return scipy.real(func(x))
integral = quadt(real_func, -5.0, 0) # lower limit of integral is -5 and upper limit is 0.
return (integral)
print INTEGRATEME( lambda kp: cmath.log(kp)) # integrating ln(x) from -5 to 0
这是输出:
%run "c:\users\envy14~1\appdata\local\temp\tmpnsvrem.py"
3.05565292766