【发布时间】:2017-04-09 14:32:07
【问题描述】:
我读过this SO post,它说命名空间冲突是导致此错误的原因之一。我经常陷入这个错误。所以,我想了解这里到底发生了什么?图书馆的期望是什么?
编辑:fun = lambda x: 4*x*(np.sin(x**2) - 3)*np.cos(x**2) 来自一个测试用例,所以实际上我必须将它用作“有趣”功能。很抱歉错过了这些信息。请讨论尊重此约束。
EDIT2:这是一个错误重现代码,而不是完整的脚本。 任务是计算输入函数的微分,该函数可以通过使用扰动 Δ=10 -8 的前向差分近似来评估 numpy 数组。
代码:
import sympy
import numpy as np
# TESTING...
x = sympy.Symbol('x')
fun = lambda x: 4*x*(np.sin(x**2) - 3)*np.cos(x**2)
print fun
h = 10e-8 #perturbation
print fun(x)
print fun(x+h)
df = (fun(x+h) - fun(x)) / h
print "diff is:", df
错误:
<function <lambda> at 0x000000001068E2E8>
Traceback (most recent call last):
File "<ipython-input-75-0582d8ebb11b>", line 1, in <module>
runfile('D:/test_hw3.py', wdir='D:')
File "D:\anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "D:\anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "D:/test_hw3.py", line 23, in <module>
print fun(x)
File "D:/test_hw3.py", line 20, in <lambda>
fun = lambda x: 4*x*(np.sin(x**2) - 3)*np.cos(x**2)
AttributeError: 'Pow' object has no attribute 'sin'
【问题讨论】: