我创建了一个函数potential(x,K,B,N),其中x,K,B是numpy数组,N是一个整数。我试图在iPython中测试该函数,但我一直得到错误"global name 'sqrt' not defined"。
下面是我的代码:def potential(x,K,B,N):
x = x.reshape((3,N),order='F')
U_b = 0.0
for i in xrange(0,N-1):
for j in xrange(i+1,N):
if K[i,j] == 1.0:
U_b += sum((x[:,i]-x[:,j])**2)
U_b = 0.5*U_b
U_a = 0.0
for i in xrange(0,N-2):
for j in xrange(i+1,N-1):
for l in xrange(j+1,N):
if B[i,j,l] == 1.0:
U_a += B[i,j,l]*sum((x[:,i]-x[:,j])*(x[:,j]-x[:,l]))/(sqrt(sum((x[:,i]-x[:,j])**2))*sqrt(sum((x[:,j]-x[:,l])**2)))
U_a = -U_a
U_r = 0.0
d = 0.0
for i in xrange(0,N-1):
for j in xrange(i+1,N):
d = sqrt(sum((x[:,i]-x[:,j])**2))
if d > sqrt(0.2):
U_r += (1.0/6.0)*(1/(d**6))
else:
U_r += -0.2**(-7.0/2.0)*d + (7.0/6.0)*(0.2)**(-3)
return U_b + U_a + U_r
我试过使用from math import *,但这似乎没有帮助。任何建议都将不胜感激!