我试图用python计算10维球体的体积,但我的计算不起作用。
这是我的代码:def nSphereVolume(dim,iter):
i = 0
j = 0
r = 0
total0 = 0
total1 = 0
while (i < iter):
total0 = 0;
while (j < dim):
r = 2.0*np.random.uniform(0,1,iter) - 1.0
total0 += r*r
j += 1
if (total0 < 1):
total1 += 1
i += 1
return np.pow(2.0,dim)*(total1/iter)
nSphereVolume(10,1000)
在这里有错误:---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
20 return np.pow(2.0,dim)*(total1/iter)
21
---> 22 nSphereVolume(10,1000)
in nSphereVolume(dim, iter)
14 j += 1
15
---> 16 if (total0 < 1):
17 total1 += 1
18 i += 1
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
谁知道这个算法,谁能试着运行它 以得到这个球体体积?