所以我直接從sympy文檔嘗試一個例子,我得到一個奇怪的錯誤。我用sympy 0.7.3使用python 3.2。我一直在ipython筆記本上工作,但我認爲這不應該有所作爲。錯誤是,無論何時我創建一個「x」符號並嘗試將math.cos(x)集成,我都會收到一個錯誤,提示「無法將表達式轉換爲浮點數」。蟒蛇sympy計算積分餘弦函數的錯誤
這是一個代碼示例。這取自sympy documentation。
import sympy
import math
x = sympy.Symbol('x')
sympy.integrate(x**2 * math.exp(x) * math.cos(x), x)
導致的錯誤信息是:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-123-84e55454fb60> in <module>()
----> 1 sympy.integrate(x**2 * math.exp(x) * math.cos(x), x)
/usr/local/lib/python3.2/dist-packages/sympy/core/expr.py in __float__(self)
242 if result.is_number and result.as_real_imag()[1]:
243 raise TypeError("can't convert complex to float")
--> 244 raise TypeError("can't convert expression to float")
245
246 def __complex__(self):
TypeError: can't convert expression to float
任何建議,將不勝感激。
嘗試使用'sympy.exp'和'sympy.cos'而不是'math.exp'和'math.cos'。 –