我試圖通過計算身體各側的影響來計算埋藏物體的重力影響,然後總結一個站點的重複測量貢獻,以重複測量多個站點。代碼如下(身體是一個廣場和代碼計算順時針方向圍繞它,這就是爲什麼它的-x追溯到-x座標)我如何加速python嵌套循環?
grav = []
x=si.arange(-30.0,30.0,0.5)
#-9.79742526 9.78716693 22.32153704 27.07382349 2138.27146193
xcorn = (-9.79742526,9.78716693 ,9.78716693 ,-9.79742526,-9.79742526)
zcorn = (22.32153704,22.32153704,27.07382349,27.07382349,22.32153704)
gamma = (6.672*(10**-11))#'N m^2/Kg^2'
rho = 2138.27146193#'Kg/m^3'
grav = []
iter_time=[]
def procedure():
for i in si.arange(len(x)):# cycles position
t0=time.clock()
sum_lines = 0.0
for n in si.arange(len(xcorn)-1):#cycles corners
x1 = xcorn[n]-x[i]
x2 = xcorn[n+1]-x[i]
z1 = zcorn[n]-0.0 #just depth to corner since all observations are on the surface.
z2 = zcorn[n+1]-0.0
r1 = ((z1**2) + (x1**2))**0.5
r2 = ((z2**2) + (x2**2))**0.5
O1 = si.arctan2(z1,x1)
O2 = si.arctan2(z2,x2)
denom = z2-z1
if denom == 0.0:
denom = 1.0e-6
alpha = (x2-x1)/denom
beta = ((x1*z2)-(x2*z1))/denom
factor = (beta/(1.0+(alpha**2)))
term1 = si.log(r2/r1)#log base 10
term2 = alpha*(O2-O1)
sum_lines = sum_lines + (factor*(term1-term2))
sum_lines = sum_lines*2*gamma*rho
grav.append(sum_lines)
t1 = time.clock()
dt = t1-t0
iter_time.append(dt)
在加速這個循環中的任何幫助,將不勝感激謝謝。
暗示http://codereview.stackexchange.com/對於這種類型的問題,更好的論壇。 – mjv
@aix:'si.arange()'可能暗示他已經使用'numpy'數組。 – jfs
@ J.F.Sebastian:很好。我刪除了我的評論,以免引起任何進一步的混淆。 – NPE