我需要在简单连接的域上计算许多2D积分(大部分时间都是凸的)。我正在使用python函数scipy.integrate.nquad
进行此集成。但是,与在矩形域上进行积分相比,此操作所需的时间非常长。有没有更快的实现方法?
这里是一个例子;我首先在圆形域上(使用函数内部的约束)然后在矩形域(nquad
函数的默认域)上集成常量函数。
from scipy import integrate import time def circular(x,y,a): if x**2 + y**2 < a**2/4: return 1 else: return 0 def rectangular(x,y,a): return 1 a = 4 start = time.time() result = integrate.nquad(circular, [[-a/2, a/2],[-a/2, a/2]], args=(a,)) now = time.time() print(now-start) start = time.time() result = integrate.nquad(rectangular, [[-a/2, a/2],[-a/2, a/2]], args=(a,)) now = time.time() print(now-start)
矩形域仅需要
0.00029
秒,而圆形域需要2.07061
秒才能完成。
此外,循环积分也会发出以下警告:
IntegrationWarning: The maximum number of subdivisions (50) has been achieved.
If increasing the limit yields no improvement it is advised to analyze
the integrand in order to determine the difficulties. If the position of a
local difficulty can be determined (singularity, discontinuity) one will
probably gain from splitting up the interval and calling the integrator
on the subranges. Perhaps a special-purpose integrator should be used.
**opt)
我需要在简单连接的域上计算许多2D积分(大部分时间都是凸的)。我正在使用python函数scipy.integrate.nquad进行此集成。但是,时间...
使计算更快的一种方法是使用numba
,它是Python的即时编译器。