简体   繁体   English

Python双积分计算时间太长

[英]Python double integral taking too long to compute

I am trying to compute the fresnel integral over a grid of coordinates using dblquad . 我正在尝试使用dblquad在坐标网格上计算fresnel integral But its taking very long and finally it's not giving any result. 但是它花费了很长时间,最终没有得到任何结果。

Below is my code. 下面是我的代码。 In this code I integrated only over a 10 x 10 grid but I need to integrate at least over a 500 x 500 grid. 在此代码中,我仅在10 x 10的网格上集成,但我至少需要在500 x 500的网格上集成。

import time
st = time.time()
import pylab
import scipy.integrate as inte
import numpy as np
print 'imhere 0'
def sinIntegrand(y,x, X , Y):
    a = 0.0001
    R = 2e-3
    z = 10e-3
    Lambda = 0.5e-6
    alpha = 0.01
    k  = np.pi * 2 / Lambda
    return  np.cos(k * (((x-R)**2)*a + (R-(x**2 + y**2)) * np.tan(np.radians(alpha)) + ((x - X)**2 + (y - Y)**2) / (2 * z)))
print 'im here 1'
def cosIntegrand(y,x,X,Y):
    a = 0.0001
    R = 2e-3
    z = 10e-3
    Lambda = 0.5e-6
    alpha = 0.01
    k  = np.pi * 2 / Lambda
    return  np.sin(k * (((x-R)**2)*a + (R-(x**2 + y**2)) * np.tan(np.radians(alpha)) + ((x - X)**2 + (y - Y)**2) / (2 * z)))
def y1(x,R = 2e-3):
    return (R**2 - x**2)**0.5
def y2(x, R = 2e-3):
   return -1*(R**2 - x**2)**0.5
points = np.linspace(-1e-3,1e-3,10)
points2 = np.linspace(1e-3,-1e-3,10)
yv,xv = np.meshgrid(points , points2)
#def integrate_on_grid(func, lo, hi,y1,y2):
#    """Returns a callable that can be evaluated on a grid."""
#    return np.vectorize(lambda n,m: dblquad(func, lo, hi,y1,y2,(n,m))[0])
#
#intensity = abs(integrate_on_grid(sinIntegrand,-1e-3 ,1e-3,y1, y2)(yv,xv))**2 + abs(integrate_on_grid(cosIntegrand,-1e-3 ,1e-3,y1, y2)(yv,xv))**2
Intensity = []
print 'im here2'
for i in points:
    row = []
    for j in points2:
        print 'im here'
        intensity = abs(inte.dblquad(sinIntegrand,-1e-3 ,1e-3,y1, y2,(i,j))[0])**2 + abs(inte.dblquad(cosIntegrand,-1e-3 ,1e-3,y1, y2,(i,j))[0])**2
        row.append(intensity)
    Intensity.append(row)
Intensity = np.asarray(Intensity)
pylab.imshow(Intensity,cmap = 'gray')
pylab.show()
print str(time.time() - st)

I would really appreciate if you could tell any better way of doing this. 如果您能说出更好的方法,我将不胜感激。

Using a scipy.integrate.dblquad to calculate every pixel of your image is going to be slow in any case. 在任何情况下,使用scipy.integrate.dblquad计算图像的每个像素都会很慢。

You should try rewriting your mathematical problem so you can use some classical function in scipy.special instead. 您应该尝试重写数学问题,以便可以在scipy.special使用一些经典函数。 For instance, scipy.special.fresnel might work, although it is 1D and your problem seems to be in 2D. 例如, scipy.special.fresnel可能有效,尽管它是一维的,而您的问题似乎在二维中。 Otherwise, that there is a relationship between the Fresnel integral and the incomplete Gamma function ( scipy.special.gammainc ), if that helps. 否则,如果有帮助,则菲涅耳积分与不完整的伽玛函数( scipy.special.gammainc )之间存在关系

If none of this work, as a last resort you can spend time optimizing your code and adapting it to Cython. 如果这些都不起作用,那么作为最后的选择,您可以花费时间优化您的代码并将其适应Cython。 This it will probably give a speed up of a factor of 10 to 100 (see this answer ). 这可能会使速度提高10到100(请参阅此答案 )。 Though this wouldn't be sufficient to go from a grid 10x10 to a grid 500x500 . 尽管从10x10网格到500x500网格还不够。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM