[软件设计/软件工程] TypeError:分段使用 sympy 时无法确定关系的真值

[复制链接]
发表于 2022-5-4 15:13:58
问题
在我的研究中,我找不到任何与分段函数中的关系值相关的示例。虽然我用 uni_dis 方法得到了上限值和下限值,但我仍然坚持将这些值转换为分段函数。是什么原因?
  1. from sympy import Symbol, Piecewise
  2. import sympy as sym
  3. import sympy.plotting as syp
  4. import math

  5. a = Symbol('a')
  6. b = Symbol('b')
  7. x = Symbol('x')
  8. function = 1 / abs(a-b)

  9. def uni_dis(lower, upper):
  10.     if lower > upper:
  11.         lower, upper = upper, lower
  12.     uniform = Piecewise((0, x < lower), (0, x > upper), (function.subs({a:lower, b:upper}), x >= lower and x <= upper))
  13.     syp.plot(uniform.subs((x,-10,10), title="uni_dis"))

  14. uni_dis(231, 675)
复制代码

我的错误信息如下:
  1. TypeError                                 Traceback (most recent call last)
  2. <ipython-input-3-c28135b22fc4> in <module>
  3. ----> 1 uni_dis(231, 675)

  4. <ipython-input-2-e4a205990c2a> in uni_dis(lower, upper)
  5.       2     if lower > upper:
  6.       3         lower, upper = upper, lower
  7. ----> 4     uniform = Piecewise((0, x < lower), (0, x > upper), (function.subs({a:lower, b:upper}), x >= lower and x <= upper))
  8.       5     syp.plot(uniform.subs((x,-10,10), title="uni_dis"))

  9. C:\ProgramData\Anaconda3\lib\site-packages\sympy\core\relational.py in __nonzero__(self)
  10.     374
  11.     375     def __nonzero__(self):
  12. --> 376         raise TypeError("cannot determine truth value of Relational")
  13.     377
  14.     378     __bool__ = __nonzero__

  15. TypeError: cannot determine truth value of Relational
复制代码

回答
您应该将 x >= lower 和 x <= upper 修改为 (x >= lower) &amp;amp; (x <= 上) 。

问题是使用 SymPy 对象的逻辑评估可能会返回另一个无法推断为真或假的符号逻辑。

您可以看到 (x >= lower).__class__ 如何返回另一个 sympy 不等式实例。





上一篇:更改ggplot几何条上填充变量的顺序
下一篇:为什么我们不能取消引用常量整数?

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表