如何使用多个变量的多个限制声明一个sympy

时间:2016-08-19 02:27:38

标签: 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

1 个答案:

答案 0 :(得分:2)

啊,对此有一个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))