python - sympy - 二分法 - TypeError:无法确定关系的真值
问题描述
我正在尝试使用 Sympy 实现 Bisection 方法,但出现此错误:
if f[a]*f[c] > 0: # Opposite sign a and c
File "C:\Users\maico\AppData\Local\Programs\Python\Python38\lib\site-packages\sympy\core\relational.py", line 376, in __nonzero__
raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
这是我的代码:
from sympy import plot_implicit, latex, lambdify, Float
from sympy.abc import x
from sympy.parsing.latex import parse_latex
eq = input("Latex equation: ")
raw_equation = eq.replace("=0", "").replace("= 0", "").replace("e", "E")
equation = parse_latex("y = " + raw_equation)
f = lambdify(x, parse_latex(self.__raw_equation), 'numpy')
# Bisection
a = 0 # start interval
b = 1 # end interval
eps = a - b
r = None
nlimit = 8
for n in range(nlimit):
c = (a + b) / 2
fd = {a: f(a), b: f(b), c: f(c)}
solved = False
for v in [a, b, c]:
if fd[v] == 0:
r = "Iterations: {} - Result: {}".format(n + 1, v)
solved = True
break
if solved:
break
if fd[a]*fd[c] > 0: # Opposite sign a and c <-- ERROR
b = c
else: # Opposite sign b and c
a = c
...
您可以使用以下等式对此进行测试:xe^x-1=0
你能帮助我吗?谢谢
解决方案
通过这些进口:
from sympy import plot_implicit, latex, lambdify, Float
from sympy import symbols
from sympy.abc import x
from sympy.parsing.latex import parse_latex
E = symbols('E')
equation = E**x*x - 1
f = lambdify(x, equation, 'numpy')
print(f.__doc__)
....
我得到:
1450:~/mypy$ python3 stack61370217.py
Created with lambdify. Signature:
func(x)
Expression:
E**x*x - 1
Source code:
def _lambdifygenerated(x):
return (E**x*x - 1)
Imported modules:
Traceback (most recent call last):
File "stack61370217.py", line 29, in <module>
if fd[a]*fd[c] > 0: # Opposite sign a and c <-- ERROR
File "/usr/local/lib/python3.6/dist-packages/sympy/core/relational.py", line 376, in __nonzero__
raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
如果相反
from sympy import E
羔羊化的表达式变为
def _lambdifygenerated(x):
return (x*exp(x) - 1)
它运行生产c
0.00390625
您没有显示E
. 我不知道乳胶解析是否会产生它。我没有安装足够的软件包来运行它。在任何情况下,该符号都会E
传播到numpy
表达式,
In [86]: E=symbols('E')
In [87]: def f(x):
...: return E**x*x-1
...:
In [88]: f(10)
Out[88]:
10
10⋅E - 1
In [89]: f(10)*f(20)>0
Out[89]:
⎛ 10 ⎞ ⎛ 20 ⎞
⎝10⋅E - 1⎠⋅⎝20⋅E - 1⎠ > 0
In [90]: if f(10)*f(20)>0: pass
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-90-14ce5e03fbac> in <module>
----> 1 if f(10)*f(20)>0: pass
/usr/local/lib/python3.6/dist-packages/sympy/core/relational.py in __nonzero__(self)
374
375 def __nonzero__(self):
--> 376 raise TypeError("cannot determine truth value of Relational")
377
378 __bool__ = __nonzero__
TypeError: cannot determine truth value of Relational
相反,如果E
sympy 被定义e
,则被翻译为exp(x)
,并且一切正常。
推荐阅读
- c# - 在第一个 do while 循环之后如何向用户提供不同的消息?
- mysql - 如何比较字符串开头的字符而不包括尾随数字或字符
- stylelint - 如何显示 stylelint Node API 的错误控制台日志?
- javascript - 从数千个链接中提取图像 src 属性 - 使用 JavaScript 的任务自动化
- ffmpeg - 如何计算在 iPhone 上显示视频的最佳尺寸和比特率?
- java - 如何按文件扩展名过滤 ACTION_OPEN_DOCUMENT 意图
- javascript - 覆盖 Javascript 数组中的值不会与控制台输出发生冲突
- mysql - mysql全文搜索相关性得分不正确
- angular - *ngFor 问题中的 matDatepicker
- python - 在 Robot Framework 或 Python 中使用部分 url 值选择窗口