Boolean bug?

34 views
Skip to first unread message

Duane Nykamp

unread,
May 14, 2015, 9:42:27 PM5/14/15
to sy...@googlegroups.com
I don't think this is the desired behavior.

In [3]: a=Symbol('a')

In [4]: bool(And(2>a, a>2))
Out[4]: True


Joachim Durchholz

unread,
May 15, 2015, 3:30:59 AM5/15/15
to sy...@googlegroups.com
The `And` does not simplify, and the bool() conversion turns it into a
Pythonic `True`.

The former happens because assumptions don't know about relations (yet),
see "Remarks" in http://docs.sympy.org/latest/modules/assumptions/ask.html .

The latter happens because Python is converting an `And` object to
`bool`, and since it's none of the objects that convert to `False`, it
turns it into a `True`.

Duane Nykamp

unread,
May 15, 2015, 9:44:09 AM5/15/15
to sy...@googlegroups.com
In the end, I would think that an expression like

In [10]: bool(And(a>2, a<3))
Out[10]: True

should raise a TypeError, like

In [11]: bool(a>2)
[snip]
TypeError: cannot determine truth value of
a > 2

I assume so as, as there is no way to know if a is between 2 and 3.  Shouldn't it have the same behavior as this form of the same condition?

In [12]: a>2 and a<3
[snip]
TypeError: cannot determine truth value of
a > 2

Joachim Durchholz

unread,
May 15, 2015, 5:34:21 PM5/15/15
to sy...@googlegroups.com
I agree that `bool(And(a>2, a<3))` and `bool(a>2)` should show analogous
behaviour.
At least that's what I'd assume from my current understanding of
assumptions and evaluation in SymPy. I'd like to defer to the real
experts on these components (I think that's @asmeurer and @certik).

Duane Nykamp

unread,
May 15, 2015, 6:01:48 PM5/15/15
to sy...@googlegroups.com
Yes, it turned out to mess it up for my use case.  It would just confuse folks using my application if And(a>2, a<3) ended up always being true when a is symbolic.

My solution was to make a derived class off And and add the methods

    def __nonzero__(self):
        raise TypeError("cannot determine truth value of And")

    __bool__ = __nonzero__


This means, that if the And was not evaluated to a boolean (i.e., it is still an And), then it shouldn't have a truth value.  It works fine, except for the following strange behavior in ipython (this is with my customized And function):

In [9]: expr=And(a>2, a<3)

In [10]: ?expr
[snip]
TypeError: cannot determine truth value of And

But, it does act as it is supposed to, so I'm fine with it for my purpose.  However, I'm not what the right way would be to make such a change to the actual sympy code if folks agree this is the correct behavior.  I assume one would want ?expr in ipython to work correctly.  (I'm also not sure to which of the parent classes of And one should add this behavior.)

Duane

Aaron Meurer

unread,
May 15, 2015, 6:03:53 PM5/15/15
to sy...@googlegroups.com
I agree. If bool(x > 2) raises TypeError then so should bool(And(a >
2, a < 2)).

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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/9043ddaf-1958-4bf4-9119-c5ea66c4fc13%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Gaurav Dhingra

unread,
May 16, 2015, 1:06:50 AM5/16/15
to sy...@googlegroups.com
@asmeurer 
For this piece of code
>>> x = Symbol('x', real=True)
>>> bool( x > S(2) ) 
TypeError: can not determine the truth value of Relational

I think, even if this returns an error then it should "not" be  TypeError.
Since a real number comparison is there in this. What are your views on this ?

Aaron Meurer

unread,
May 16, 2015, 1:54:52 PM5/16/15
to sy...@googlegroups.com
What do you think it should do?

The point of the TypeError is that the type of the expression cannot
be evaluated to bool (that's a Python bool, not to be confused with a
mathematical boolean), because it is not known if it is True or False.
I think the same applies to any SymPy Boolean object that can't be
reduced to a single bool value.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/c3b02652-f2a7-489b-a403-953ea035aaa8%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages