Unable to determine truth value of a statement

555 views
Skip to first unread message

Duane Nykamp

unread,
Oct 29, 2015, 2:07:59 PM10/29/15
to sympy
I don't understand why sympy cannot determine the truth value of the statement:

In [41]: 1/(-ln(35) + 2*sqrt(4)) > 0.1
Out[41]:
     1           
──────────── > 0.1
-log(35) + 4     


In [42]: bool(1/(-ln(35) + 2*sqrt(4)) > 0.1)
TypeError: cannot determine truth value of Relational


It works if I explicitly tell it to evalf:
In [43]: 1/(-ln(35) + 2*sqrt(4)).evalf() > 0.1
Out[43]: True

I don't understand the logic of how sympy/python determine whether or not to evaluate the inequality.  I'm not sure what is going wrong in this instance to keep the evaluation from occurring.  If I change the numbers, it sometimes works, sometimes doesn't.

Changing sqrt(4) to sqrt(5) still doesn't work.

In [52]: 1/(-ln(35) + 2*sqrt(5)) > 0.1
Out[52]:
       1            
─────────────── > 0.1
-log(35) + 2⋅√5     

Changing sqrt(4) to sqrt(3) works.

In [53]: 1/(-ln(35) + 2*sqrt(3)) > 0.1
Out[53]: False

I can't find anything mathematically special about the cases that don't automatically evaluate to a boolean (and therefore cannot be explicitly evaluated to a boolean, either, as they get a TypeError), so I am mystified by this behavior and the source of what appears to be a bug.

Any ideas?

Duane Nykamp





Aaron Meurer

unread,
Oct 29, 2015, 3:37:07 PM10/29/15
to sy...@googlegroups.com
It looks like a bug.

I dug into it, and it looks like this is the source:

In [6]: (-0.1 + 1/(-log(35) + 4))._eval_evalf(2)
Out[6]: zoo

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/4023ed0c-b69e-428a-97ee-2057accb18fa%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Aaron Meurer

unread,
Oct 29, 2015, 3:50:49 PM10/29/15
to sy...@googlegroups.com
I opened https://github.com/sympy/sympy/issues/10073 for this with my
findings. I figured out what the issue is, but I'm not sure what the
correct fix is.

In the meantime, you can use simplify() to evaluate expressions like
this, and as far as I know, simplify() will work even in cases where
relationals don't automatically evaluate on purpose (not due to a
bug).

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages