英文:
How can I get a piecewise function with sympy when some parameter is unknown?
问题
我想知道是否有一种方法可以指定 p 大于 0 且小于 π,以便我不会得到“无法确定关系的真值”的错误消息。
英文:
I wonder if there is a way to specify that p is greater than 0 and smaller than pi, so that I don't get "cannot determine truth value of Relational".
答案1
得分: 2
以下是翻译好的部分:
您可以尝试以下方式,使用两个布尔值而不是关系表达式 0 <= x <= p
。
import sympy as sp
x, h, p = sp.symbols('x h p')
fx = sp.Piecewise(
((x*h)/p, ((0 <= x) & (x <= p))),
((h*(sp.pi - x))/(sp.pi - p), ((p <= x) & (x <= sp.pi)))
)
这将得到如下结果:
英文:
You can do something like the following, using two booleans rather than the relational 0 <= x <= p
.
import sympy as sp
x, h, p = sp.symbols('x h p')
fx = sp.Piecewise(
((x*h)/p, ((0 <= x) & (x <= p))),
((h*(sp.pi - x))/(sp.pi - p), ((p <= x) & (x <= sp.pi)))
)
Which gives
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论