Python 未定义全局名称“sqrt”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24320630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
global name 'sqrt' not defined
提问by user3758890
I've created a function, potential(x,K,B,N)
, where x
,K
,B
are numpy
arrays and N
is an integer. I'm trying to test the function in iPython
but I keep getting the error "global name 'sqrt' not defined"
.
我创建了一个函数,potential(x,K,B,N)
其中x
,K
,B
是numpy
阵列和N
为整数。我正在尝试测试该功能,iPython
但我不断收到错误消息"global name 'sqrt' not defined"
。
Here's a look at my code:
看看我的代码:
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
I've tried using from math import *
but that doesn't seem to help. Any suggestions would be greatly appreciated!
我试过使用,from math import *
但这似乎没有帮助。任何建议将不胜感激!
回答by smci
from math import sqrt
is all that's missing
这就是缺少的一切
I've tried using
from math import *
but that doesn't seem to help.
我试过使用,
from math import *
但这似乎没有帮助。
(Possibly you did that after defining the function. Anyway, fuhgeddaboutit, just reload the code in a clean session, it will work.)
(可能你是在定义函数后这样做的。无论如何,fuhgeddaboutit,只需在干净的会话中重新加载代码,它就会工作。)
UPDATE: strictly, in Python you're supposed to do import package
not from package import identifier1 [,identifier2, identifier3...]
and neverfrom package import *
. But from package import identifier1
is ok when judiciously used, if you don't overdo it, and locally inside a function. If it's unambiguous, and you're going to be doing a lot of it, it shortens the code e.g. sqrt()
instead of math.sqrt()
, log
instead of math.log10()
UPDATE:严格,在Python你应该做的import package
不是from package import identifier1 [,identifier2, identifier3...]
和从来没有from package import *
。但是from package import identifier1
明智地使用它是可以的,如果你没有过度使用它,并且在函数内部。如果它是明确的,并且您将要做很多事情,那么它会缩短代码,例如sqrt()
代替math.sqrt()
,log
而不是math.log10()
UPDATE2: sqrt is not a builtin in Python, unlike R. So yes in Python you need either import math
or from math import sqrt
before you can use it.
UPDATE2:sqrt 不是 Python 中的内置函数,与 R 不同。所以是的,在 Python 中你需要import math
或者from math import sqrt
在你可以使用它之前。
回答by sundar nataraj
just add.
只需添加。
from math import sqrt
回答by Adarsh Chavakula
Since you tagged numpy,
既然你标记了 numpy,
import numpy as np
Then use np.sqrt
instead of sqrt
. Always works.
然后使用np.sqrt
代替sqrt
。总是有效。
回答by Adarsh Chavakula
You have a couple of options here:
您在这里有几个选择:
Additional libraries: e.g., NumPy
附加库:例如, NumPy
import numpy as np
and then use np.sqrt(9)
然后使用 np.sqrt(9)
or
或者
from numpy import sqrt
sqrt(9)
Or Standard Library and in-built solutions:
或标准库和内置解决方案:
1)9**0.5
1)9**0.5
2)
2)
import math
math.sqrt(9)
or
或者
from math import sqrt
sqrt(9)
For the latter part, I'd prefer the math
function for performance reasons. I did this benchmark here:
对于后一部分,math
出于性能原因,我更喜欢该功能。我在这里做了这个基准测试:
Why is the math module more efficient? The math module uses the C implementations of the square root
为什么数学模块更高效?数学模块使用平方根的 C 实现
The code for my benchmark can be found here:
我的基准测试代码可以在这里找到: