Nested piecewise functions

36 views
Skip to first unread message

Vincent Noel

unread,
Aug 18, 2016, 12:24:17 PM8/18/16
to sympy
Hi,

I'm trying to represent a weird case using piecewise functions :

z = sympy.Piecewise((True, x>0), (False, True))

A piecewise function returning a boolean value... so far so good. The problems comes when I want to use that as a condition inside another Piecewise :

z2 = sympy.Piecewise((1,z), (0, True), evaluate=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/sympy/functions/elementary/piecewise.py", line 104, in __new__
    " Boolean, or a built-in bool." % (cond, type(cond)))
TypeError: Cond Piecewise((True, x > 0), (False, True)) is of type Piecewise, but must be a Relational, Boolean, or a built-in bool.

z being obviously a boolean, is there no way for sympy to recognize that and accept it as a condition ?

Vincent

Aaron Meurer

unread,
Aug 18, 2016, 4:10:02 PM8/18/16
to sy...@googlegroups.com
I would use ITE for this case. It's the boolean version of Piecewise.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/d0a66ce7-d965-4810-bb33-eca9bb2bdc54%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Aaron Meurer

unread,
Aug 18, 2016, 4:12:36 PM8/18/16
to sy...@googlegroups.com
Of course, for Piecewise((True, x>0), (False, True)), you could just
replace it with x > 0. It seems ITE doesn't do this automatically but
it does if you call to_nnf() on it.

In [12]: ITE(x > 0, True, False).to_nnf()
Out[12]: x > 0

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages