piecewise_fold boolean

21 views
Skip to first unread message

Chris Smith

unread,
May 4, 2017, 11:12:40 AM5/4/17
to sympy
The following note appears in piecewise_fold and I can't see the reason for not returning the expression that it tries to avoid. Can anyone enlighten me?

# If expr is Boolean, we must return some kind of PiecewiseBoolean. 
# This is constructed by means of Or, And and Not. 
# piecewise_fold(0 < Piecewise( (sin(x), x<0), (cos(x), True))) 
# can't return Piecewise((0 < sin(x), x < 0), (0 < cos(x), True))        <---- what's wrong with this?
# but instead Or(And(x < 0, 0 < sin(x)), And(0 < cos(x), Not(x<0))) 

/c

Aaron Meurer

unread,
May 4, 2017, 7:22:24 PM5/4/17
to sy...@googlegroups.com
I don't know if any code will break with it, but I think it's better for Piecewise to only deal with expressions. For booleans, you can get the equivalent of a Piecewise using logic classes like And, Or, Not, or even more advanced things like ITE (ITE means "if-then-else"). Piecewise on booleans is basically and ITE chain; it's the "some kind of PicewiseBoolean" that the author of that comment is looking for. 

An advantage is that since the expressions themselves are booleans, it is sometimes possible to rearrange and simplify them with the conditions.  As a simple example, consider Piecewise((sin(x) > 0, x > 0), (x > 0, True)). This is the same as ITE(x > 0, sin(x) > 0, x > 0), which simplifies:

>>> simplify_logic(ITE(x > 0, sin(x) > 0, x > 0))
(x > 0) & (sin(x) > 0)

which makes sense if you think about it: logically, x > 0 has to be true, meaning so does sin(x) > 0.

Also, having a Piecewise as a condition is very confusing when printed. 

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+unsubscribe@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/547f0eca-5030-41ee-9eea-978079c1af0f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages