簡體   English   中英

Python雙積分計算時間太長

[英]Python double integral taking too long to compute

我正在嘗試使用dblquad在坐標網格上計算fresnel integral 但是它花費了很長時間,最終沒有得到任何結果。

下面是我的代碼。 在此代碼中,我僅在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)

如果您能說出更好的方法,我將不勝感激。

在任何情況下,使用scipy.integrate.dblquad計算圖像的每個像素都會很慢。

您應該嘗試重寫數學問題,以便可以在scipy.special使用一些經典函數。 例如, scipy.special.fresnel可能有效,盡管它是一維的,而您的問題似乎在二維中。 否則,如果有幫助,則菲涅耳積分與不完整的伽瑪函數( scipy.special.gammainc )之間存在關系

如果這些都不起作用,那么作為最后的選擇,您可以花費時間優化您的代碼並將其適應Cython。 這可能會使速度提高10到100(請參閱此答案 )。 盡管從10x10網格到500x500網格還不夠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM