简化sympy中的嵌套分段方程
假设我有两个分段方程(a(t)和b(t)),其中第二个引用第一个参考:
from sympy import *
T = symbols("T")
A = Piecewise((T + 1/T, (T >= 300) & (T < 700)), (log(T), (T >= 700) & (T < 900)), (T**9.0, (T >= 900) & (T < 6000)), (0, True))
B = Piecewise((A + 2*T, (T >= 300) & (T < 900)), (A + 3, (T >= 900) & (T < 6000)), (0, True))
如果我只使用简化,我将a嵌套到b中:
print(simplify(B))
Piecewise((Piecewise((3*T + 1/T, (T >= 300) & (T < 700)), (2*T + log(T), (T >= 700) & (T < 900)), (2*T + T**9.0, (T >= 900) & (T < 6000)), (2*T, True)), (T >= 300) & (T < 900)), (Piecewise((T + 3 + 1/T, (T >= 300) & (T < 700)), (log(T) + 3, (T >= 700) & (T < 900)), (T**9.0 + 3, (T >= 900) & (T < 6000))), (T >= 900) & (T < 6000)), (0, True))
有没有任何方法让Sympy评估表达式范围并将它们结合在一起?理想情况下,我会得到类似的东西:
Piecewise((3*T + 1/T, (T >= 300) & (T < 700)), (log(T) + 2*T, (T >= 700) & (T < 900)), (T**9.0 + 3, (T >= 900) & (T < 6000)), (0, True))
Say I have two piecewise equations (A(T) and B(T)), where the second references the first:
from sympy import *
T = symbols("T")
A = Piecewise((T + 1/T, (T >= 300) & (T < 700)), (log(T), (T >= 700) & (T < 900)), (T**9.0, (T >= 900) & (T < 6000)), (0, True))
B = Piecewise((A + 2*T, (T >= 300) & (T < 900)), (A + 3, (T >= 900) & (T < 6000)), (0, True))
If I just use simplify, I get A nested into B:
print(simplify(B))
Piecewise((Piecewise((3*T + 1/T, (T >= 300) & (T < 700)), (2*T + log(T), (T >= 700) & (T < 900)), (2*T + T**9.0, (T >= 900) & (T < 6000)), (2*T, True)), (T >= 300) & (T < 900)), (Piecewise((T + 3 + 1/T, (T >= 300) & (T < 700)), (log(T) + 3, (T >= 700) & (T < 900)), (T**9.0 + 3, (T >= 900) & (T < 6000))), (T >= 900) & (T < 6000)), (0, True))
Is there any way to get sympy to evaluate the expressions for the ranges and combine them? Ideally I would get something like:
Piecewise((3*T + 1/T, (T >= 300) & (T < 700)), (log(T) + 2*T, (T >= 700) & (T < 900)), (T**9.0 + 3, (T >= 900) & (T < 6000)), (0, True))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PICEEWISE_FOLD
将为您返回单个零件
:请小心指数 - 您可能需要使用
9
而不是9.0 < /代码>。
piecewise_fold
will return a singlePiecewise
for you:Be careful with your exponents -- you might want to use
9
instead of9.0
.