On Feb 26, 2010, at 7:46 AM, Vinzent Steinberg wrote: > On 26 Feb., 07:46, Brian Granger <[email protected]> wrote: >> Hi, >> >> I am working to get Piecewise to understand intervals. But I am running >> into issues with how Interval and And work. Here are my notes and >> questions: >> >> Boolean Operators >> ================= >> >> In [39]: e = And(x>0,x<1) >> >> In [40]: e.subs(x,0.5) >> Out[40]: And(0 < 0.5, 0.5 < 1) >> >> Shouldn't this be true? How does And get its truth value and why is this >> not True? >> >> Interval >> ======== >> >> In [41]: i = Interval(0,1) >> >> In [42]: 0.5 in i >> Out[42]: True >> >> In [43]: 1.5 in i >> Out[43]: False >> >> In [45]: -0.5 in i >> Out[45]: False >> >> In [44]: x in i >> Out[44]: True >> >> This last should not return True!! >> >> This comes from how Interval._contains works by building an And. It returns >> the And(x>0,x<1), which evaluates as True even though it remains symbolic: >> >> In [9]: bool(And(0<=x, x<=1)) >> Out[9]: True >> >> What should __contains__ do if it can't decide the result because it is >> symbolic? Currently it returns the indeterminant And, which Python decides >> is True. Hmm. > > The problem is that __contains__ always returns a boolean. To > circumvent this limitation we could use another method or raise an > error if we don't know whether the result is True or False. So it > would probably be better if bool(And(0<=x, x<=1)) raised an exception. > > Vinzent This could also be part of the problem:
In [48]: bool(x<0) Out[48]: False If we are going to make <, >, <=, and >= shortcuts to Lt, Gt, Le, and Ge, then we are going to have to make then not able to return booleans. See also the discussions at issues 1567 and 1646. If you enter the first one directly, it works: In [49]: And(0 < 0.5, 0.5 < 1) Out[49]: True So there must be some problem with And not reevaluating after subs. Aaron Meurer -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
