2014-09-13 52 views
2

我在python中使用math.sqrt来计算东西的squre根,但是“something”是一个符号而不是已知的值。我使用这个“东西”作为后期使用的中间变量。我怎样才能在python中获得符号的平方根?

import math 
from math import sqrt 
x = Symbol('x') 
y = math.sqrt(x) 
print(y) 

豪尔我得到错误信息

File "/Library/Python/2.7/site-packages/sympy/core/expr.py", line 207, in __float__ 
    raise TypeError("can't convert expression to float") 
TypeError: can't convert expression to float 

貌似有没有其他的包,我计算平方根除“math.sqrt”,没有人知道我怎么能摆脱这个问题的?

+2

这不应该关闭。用户正在使用名为sympy的库。我建议用户应该更新问题以明确Symbol()来自该库;这将清除混乱。 – BobbyShaftoe 2014-09-13 02:53:52

回答

3

当使用sympy时,应该使用sympy中对符号进行操作的函数,而不是对浮点数进行操作的函数,如math.*

from sympy import * 
x = Symbol('x') 
y = sqrt(x) 
print(y) 

在这种情况下,该代码使用sympysqrt()功能。

+2

在这种情况下,使用'sympy.Symbol'和'sympy.sqrt'可能会更好。 – hpaulj 2014-09-13 21:28:51