请考虑以下代码:
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))
除非我遗漏了明显的内容,eq1
和eq2
在数学上是相同的。
但是,解决eq1
的{{1}}工作正常,但解决x
的{{1}}会返回错误:
正常运作:
eq1
打印:
x
然而,
奇怪的错误:
print(solve(eq1, x))
给出了这个错误:
[9*kg/2]
有什么想法吗?我需要像print(solve(eq2, x))
之类的东西来为我正在进行的项目工作。
答案 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]