gpt4 book ai didi

sympy - 如何为多个变量声明具有多个限制的 sympy Piecewise

转载 作者:行者123 更新时间:2023-12-02 09:39:14 33 4
gpt4 key购买 nike

在 sympy 中,如何声明一个对子函数中的多个变量具有多个限制的 Piecewise 函数?

这是我的背景和尝试:

from sympy import Piecewise, Symbol, exp
from sympy.abc import z
x1 = Symbol('x1')
x2 = Symbol('x2')
f = 2*pow(z,2)*exp(-z*(x1 + x2 + 2))
p = Piecewise((f, z > 0 and x1 > 0 and x2 > 0), (0, True))

我收到的错误是:

TypeError                                 Traceback (most recent call last)
<ipython-input-47-5e3db02fe3dc> in <module>()
----> 1 p = Piecewise((f, z > 0 and x1 > 0 and x2 > 0), (0, True))

C:\Anaconda3\lib\site-packages\sympy\core\relational.py in __nonzero__(self)
193
194 def __nonzero__(self):
--> 195 raise TypeError("cannot determine truth value of Relational")
196
197 __bool__ = __nonzero__

TypeError: cannot determine truth value of Relational

最佳答案

啊,有一个 sympy And 函数:

from sympy import Piecewise, Symbol, exp, And
from sympy.abc import z
x1 = Symbol('x1')
x2 = Symbol('x2')
f = 2*pow(z,2)*exp(-z*(x1 + x2 + 2))
p = Piecewise((f, And(z > 0, x1 > 0, x2 > 0)), (0, True))

关于sympy - 如何为多个变量声明具有多个限制的 sympy Piecewise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39030227/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com