我该如何在 sympy 中获得一个分段函数,当某些参数未知时?

huangapple go评论62阅读模式
英文:

How can I get a piecewise function with sympy when some parameter is unknown?

问题

我想知道是否有一种方法可以指定 p 大于 0 且小于 π,以便我不会得到“无法确定关系的真值”的错误消息。

英文:

我该如何在 sympy 中获得一个分段函数,当某些参数未知时?

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)))
            )

这将得到如下结果:

我该如何在 sympy 中获得一个分段函数,当某些参数未知时?

英文:

You can do something like the following, using two booleans rather than the relational 0 &lt;= x &lt;= p.

import sympy as sp

x, h, p = sp.symbols(&#39;x h p&#39;)

fx = sp.Piecewise(
                ((x*h)/p, ((0 &lt;= x) &amp; (x &lt;= p))),
                ((h*(sp.pi - x))/(sp.pi - p), ((p &lt;= x) &amp; (x &lt;= sp.pi)))
            )

Which gives

我该如何在 sympy 中获得一个分段函数,当某些参数未知时?

huangapple
  • 本文由 发表于 2020年1月6日 15:57:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定