python-3.x - sympy 在将无理数放入分数时返回 TypeError

标签 python-3.x typeerror sympy

我试着把它放在 sympy 中,但它不起作用:

import sympy
sympy.init_printing(use_unicode=True)

sympy.Rational(3, sympy.sqrt(3))

它返回给我以下内容:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-25b0450ea0d0> in <module>()
      1 root = sympy.sqrt(3)
      2 
----> 3 sympy.Rational(3, sympy.sqrt(3))

~\Anaconda3\lib\site-packages\sympy\core\numbers.py in __new__(cls, p, q, gcd)
   1488         else:
   1489             p = Rational(p)
-> 1490             q = Rational(q)
   1491 
   1492         if isinstance(q, Rational):

~\Anaconda3\lib\site-packages\sympy\core\numbers.py in __new__(cls, p, q, gcd)
   1483 
   1484             if not isinstance(p, SYMPY_INTS + (Rational,)):
-> 1485                 raise TypeError('invalid input: %s' % p)
   1486             q = q or S.One
   1487             gcd = 1

TypeError: invalid input: sqrt(3)

为什么会这样?那么,如何在 sympy 中将无理数放入分数中?

最佳答案

要创建比率,请使用 3/sqrt(3);像这样打印它需要使用未计算的表达式:(Mul(3,Pow(sqrt(3),-1, evaluate=False),evaluate=False)

关于python-3.x - sympy 在将无理数放入分数时返回 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53916900/

相关文章:

python - 为什么会出现 TypeError : 'int' object is not subscriptable using for loop and not with list-comprehension in python

python-3.x - 为什么 sympy 在求解三次方程时会给出复根

python - 比较 Sympy 因子结果为 false

python - 如何使用户输入的符号不区分大小写

python - 如果列存在于 df 中,如何根据条件删除 Pandas 中的列数据框 (df)?

Python 使用 tkinter 在滚动文本框中搜索下一个单词

javascript - 未捕获的 TypeError : (intermediate value)(. ..) 不是函数

python - 安装出现故障时如何安装 scikit-image

python - 在while循环中从服务器接收数据显示窗口没有响应

Python 正则表达式--类型错误 : an integer is required