奇怪的"符号布尔表达式没有真值"错误 - 这是SymPy中的错误吗?

时间:2014-09-30 17:19:22

标签: python scipy sympy

请考虑以下代码:

from sympy import *
from sympy.physics.units import *

x = symbols('x')
eq1 = Eq(10 * (5*kg - x), 5*kg)
eq2 = Eq(10, 5*kg / (5*kg - x))

除非我遗漏了明显的内容,eq1eq2在数学上是相同的。

但是,解决eq1的{​​{1}}工作正常,但解决x的{​​{1}}会返回错误:

正常运作:

eq1

打印:

x

然而,

奇怪的错误:

print(solve(eq1, x))

给出了这个错误:

[9*kg/2]

有什么想法吗?我需要像print(solve(eq2, x)) 之类的东西来为我正在进行的项目工作。

1 个答案:

答案 0 :(得分:2)

这看起来似乎是0.7.5及以下的症状。如果我使用git的最新版本,它可以正常工作:

>>> from sympy import *
>>> from sympy.physics.units import *
>>> 
>>> x = symbols('x')
>>> eq1 = Eq(10 * (5*kg - x), 5*kg)
>>> eq2 = Eq(10, 5*kg / (5*kg - x))
>>> solve(eq2)
[9*kg/2]
>>> solve(eq1)
[9*kg/2]
>>> solve(eq1, x)
[9*kg/2]
>>> solve(eq2, x)
[9*kg/2]
>>> import sympy
>>> sympy.__version__
'0.7.5-git'

使用当前发布的版本,您可以使用check=False解决此问题,或者除kg之外传递x

>>> solve(eq2, x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/sympy/solvers/solvers.py", line 901, in solve
    solution = _solve(f[0], *symbols, **flags)
  File "/usr/local/lib/python2.7/dist-packages/sympy/solvers/solvers.py", line 1418, in _solve
    for den in dens)]
  File "/usr/local/lib/python2.7/dist-packages/sympy/solvers/solvers.py", line 1418, in <genexpr>
    for den in dens)]
  File "/usr/local/lib/python2.7/dist-packages/sympy/core/relational.py", line 111, in __nonzero__
    raise TypeError("symbolic boolean expression has no truth value.")
TypeError: symbolic boolean expression has no truth value.
>>> solve(eq2, x, check=False)
[9*kg/2]
>>> solve(eq2, x, kg)
[9*kg/2]