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 On Sat, May 16, 2015 at 12:06 AM, Gaurav Dhingra <[email protected]> wrote: > @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 > ? > > On Saturday, May 16, 2015 at 3:33:53 AM UTC+5:30, Aaron Meurer wrote: >> >> I agree. If bool(x > 2) raises TypeError then so should bool(And(a > >> 2, a < 2)). >> >> Aaron Meurer >> >> On Fri, May 15, 2015 at 5:01 PM, Duane Nykamp <[email protected]> wrote: >> > 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 >> > >> > >> > >> > >> > >> > On Friday, May 15, 2015 at 4:34:21 PM UTC-5, Joachim Durchholz wrote: >> >> >> >> 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). >> > >> > -- >> > 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 [email protected]. >> > To post to this group, send email to [email protected]. >> > 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. > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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/c3b02652-f2a7-489b-a403-953ea035aaa8%40googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAKgW%3D6L1Yxy4mg4_qLFZfX2_Wj7fFUwKfPdTfZFEZ4%2BSbA%2BJxw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
