2
私は2 * Volume
のsphere(dimension=4)/Volume
をcylinder(dimension=4)
と計算しています。私が間違っているのは、この問題に対する最初のアプローチです。 2番目のアプローチは正解です。つまり、答えが1.17のときは1.14になります。これらのプログラムはどのように異なるのですか?私は違いを見つけることができません。次の方法は、あなたがそれを変更することができリファクタリング後のPython - プログラムが異なる結果を返す
if ((x + del_x)**2 + (y + del_y)**2 + (z + del_z)**2 + alpha**2) < 1.0
:
if (x**2 + y**2 + z**2 + alpha**2) < 1.0:
以下のようになります。
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
あなたは答えに同様の作業の式を追加することができます '(合計(座標におけるiに対するI ** 2)+アルファ** 2)' :) – officialaimm
は非常にあなたの両方をありがとう多く! :) – Caterina
@Caterina私はいくつかのリファクタリングを行ってきました。あなたが "エレガント"なソリューションを書いているときに役立つことを願っています。 –