2017-09-01 40 views
2

我在計算2 * Volumesphere(dimension=4)/Volumecylinder(dimension=4)。我遇到的問題是解決問題的第一種方法。第二種方法給出了正確的答案。也就是說,當答案爲1.17時,我得到1.14。這些計劃如何不同?我無法發現差異。Python - 重構後的程序返回不同的結果

import random 

# FIRST WAY 

print('elegant solution') 

coordinates = [0] * 3 
alpha = 0 
delta = 0.1 
deltas = [0] * 3 
n_trials = 1000000 
n_hits = 0 

for a in range(6): 
    for i in range(n_trials): 
     # gets random deltas, and random alpha for 4th dimension 
     deltas = [random.uniform(-delta, delta) for coordinate in deltas] 
     alpha = random.uniform(-1.0, 1.0) 

     # sum of the (n - 1) first components 
     sum_components = sum((coordinates[j] + deltas[j])**2 for j in range(3)) 

     # if the sample is inside the cylinder 
     if sum_components < 1.0: 
      coordinates = [coordinates[j] + deltas[j] for j in range(3)] 

     # if the sample is inside the sphere 
     if sum_components + alpha**2 < 1.0: 
      n_hits += 1 

    print (2.0 * float(n_hits)/float(n_trials)) # 2V_sph(4)/V_cyl(4) where V_sph=hits Vcyl=trials 
    coordinates = [0] * 3 
    n_hits = 0 

# SECOND WAY 

print('typical solution') 

x, y, z, alpha = 0.0, 0.0, 0.0, 0.0 
delta = 0.1 
n_trials = 1000000 
n_hits = 0 

for a in range (6): 
    for i in range(n_trials): 
     # gets random deltas, and random alpha for 4th dimension 
     del_x, del_y, del_z, alpha = random.uniform(-delta, delta), random.uniform(-delta, delta), random.uniform(-delta, delta), random.uniform(-1, 1) 

     # if the sample is inside the cylinder 
     if (x + del_x)**2 + (y + del_y)**2 + (z + del_z)**2 < 1.0: 
      x, y, z = x + del_x, y + del_y, z + del_z 

     # if the sample is inside the sphere 
     if x**2 + y**2 + z**2 + alpha**2 < 1.0: 
      n_hits += 1 

    print (2.0 * n_hits/float(n_trials)) # 2V_sph(4)/V_cyl(4) where V_sph=hits Vcyl=trials 
    x, y, z = 0.0, 0.0, 0.0 
    n_hits = 0 

回答

4

下一行:

if (sum_components + alpha**2) < 1.0: 

不等於:

if (x**2 + y**2 + z**2 + alpha**2) < 1.0: 

它等於:

if ((x + del_x)**2 + (y + del_y)**2 + (z + del_z)**2 + alpha**2) < 1.0 

你可以改變它旁邊道:

if sum(c**2 for c in coordinates) + alpha**2 < 1.0 

你的代碼不是很pythonic。下面是一些重構:

import random 

delta = 0.1 
n_trials = 1000000 

for _ in range(6): 
    coords = [0] * 3 
    n_hits = 0 

    for _ in range(n_trials): 
     deltas = [random.uniform(-delta, delta) for _ in range(len(coords))] 
     alpha = random.uniform(-1.0, 1.0) 

     if sum((c + d)**2 for c, d in zip(coords, deltas)) < 1.0: 
      coords = [c + d for c, d in zip(coords, deltas)] 

     if sum(c**2 for c in coords) + alpha**2 < 1.0: 
      n_hits += 1 

    print(2.0 * n_hits/n_trials) 
+0

您可以在答案添加工作表現,以及:'(SUM(我** 2我在座標)+字母** 2)':) – officialaimm

+1

謝謝都非常許多! :) – Caterina

+2

@Caterina我已經做了一些重構,我希望這是有用的,因爲你正在寫一個「優雅」的解決方案。 –