6

In sympy, how do I declare a Piecewise function with multiple limits for multiple variables in a sub-function?

Here is my context and attempt:

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

And the error I receive is:

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

2 Answers 2

5

Ah, there is a sympy And function for this:

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))
Sign up to request clarification or add additional context in comments.

Comments

2

You can use & as well:

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) & (x1 > 0) & (x2 > 0)), (0, True))

There are more examples in their documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.