Mixing boolean and abstract types

45 views
Skip to first unread message

Mehul Tikekar

unread,
Apr 4, 2014, 11:53:56 AM4/4/14
to sy...@googlegroups.com
Hi,
   I am looking for a variant of the boolean ITE(cond, x, y) function. I need only "cond" to be a bool while "x" and "y" are arbitrary symbols (they could be numbers, for example). Does sympy have a function like that? If not, any ideas on how I can go about writing one? Ideally, it should also be able to do simplifications. For example, ITE'(cond, a, b) + ITE'(cond, b, a) should be simplified to (a + b).

Mehul

SAHIL SHEKHAWAT

unread,
Apr 4, 2014, 12:17:00 PM4/4/14
to sy...@googlegroups.com
I don't understand what you mean by boolean ITE function can you please explain that?  
BUT I think ITE work well if "cond" is a bool when "x" and "y" are arbitary symbols. for example:
     IN [1]: ITE(True, x , y)
     OUT [1]: x

in the second part of your question if "cond" is a bool then yes , ITE'(cond, a, b) + ITE'(cond, b, a) will return (a+b)


--
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 http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/b5d8f3fc-d413-4ec2-ace0-76e21ac0bae7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mehul Tikekar

unread,
Apr 4, 2014, 12:25:58 PM4/4/14
to sy...@googlegroups.com
I am referring to the ITE function defined in sympy here: http://docs.sympy.org/dev/_modules/sympy/logic/boolalg.html#ITE. But it requires all three arguments to be boolean. When I try:

cond, x, y = symbols('cond x y')
ITE(cond, x, y) + ITE(cond, y, x)

I get a TypeError: unsupported operand type(s) for +: 'Or' and 'Or'

SAHIL SHEKHAWAT

unread,
Apr 4, 2014, 11:33:56 PM4/4/14
to sy...@googlegroups.com
It is not necessary for all the arguments of ITE to be bool. If x,y and z are all arbitrary variables (but not numbers) this is what it gives.
     >>> ITE(x,y,z)
     Or(And(Not(x), z), And(x, y))
And you are getting this error because of the above logical statement (you can not add two logical statements but if by "+" you mean "Or" please use " | ") But if instead of that like, i said earlier, if "Cond" in your example is either True or False your statement will return ( x + y )
    >>> ITE(True,x,y) + ITE(True,y,x)
    x + y

And about x and y being a number, ITE does not support that . It takes them as being bool. 
The reason the arbitary variables work but number don't is that ITE is a logical boolean function which converts number into bools whereas you can use variables because they can be specified to be bools like
    >>>x = symbols(x,is_Boolean=True)

I am not sure if we want numbers to be included in ITE or they are just fine. it will be nice if other developers also comment.
Thanks

Mehul Tikekar

unread,
Apr 5, 2014, 12:02:36 AM4/5/14
to sy...@googlegroups.com
There seems to be some sort of inconsistency when using symbols vs literals -

>>> ITE(True, 3, 4)
True
>>> a, b, c = symbols('a b c')
>>> ITE(a, b, c).subs([(a, True), (b, 3), (c, 4)])
3

SAHIL SHEKHAWAT

unread,
Apr 5, 2014, 12:13:58 AM4/5/14
to sy...@googlegroups.com
I guess this is due to the fact the "subs" replaces the variables in the result (after the expression is computed) not in the expression itself whereas by directly entering the numbers it takes them as bools ( 0 being False and others being True). So, I guess it is consistent.

SAHIL SHEKHAWAT

unread,
Apr 5, 2014, 12:16:36 AM4/5/14
to sy...@googlegroups.com
Still i emphasis on the fact that i am not that much experienced you can wait for other experienced developers to reply or you can also join us on gitter. https://gitter.im/sympy/sympy

Aaron Meurer

unread,
Apr 5, 2014, 2:20:40 AM4/5/14
to sy...@googlegroups.com
You are looking for Piecewise. Piecewise((x, cond), (y, True)) is the same as ITE(cond, x, y), only it acts as an expression (you can add it and so on), not a boolean.

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 http://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
0 new messages