# Behavior of 'or'

Hello !

Here I have observed something in sage.

sage: (x == 1 or x == -3).full_simplify()

x == -3

sage: (x == -1 or x == -3).full_simplify()

x == -3

sage: (x == -1 or x == 3).full_simplify()

x == 3

sage: (x == -1 or x == -3).full_simplify()

x == -3

sage: (x == 1 or x == -3).full_simplify()

x == -3

I wish to know that why sage replies with the second argument. It is very necessary for me to understand this behavior of sage.

Many Thanks !

edit retag close merge delete

Sort by ยป oldest newest most voted

I think it is a mixture of boolean and symbolics that is going on here. If I am not wrong, this is what is happening to your expression, say, (x==1) or (x==-3):

1. First, the part of the expression before or is evaluated. So, most probably bool(x==1) is being run to find out if it evaluates to True or False.
2. Now, bool(x==1) will always return False, so the result of the expression becomes the result of whatever (x==-3) is.
3. x is a symbolic variable and Sage by default does not evaluate an expression like x==-3 to give the truth value of the expression. What Sage does is keep the expression unmodified.

This also goes into the behavior of or and and in Python itself. In particular, read the Note at the end of the section here. Notice how it says that and and or return the last evaluated argument and their output may not be restricted to True or False.

more

Thank you.

( 2013-01-04 09:37:40 +0200 )edit

Nice work - I hadn't refreshed the page. Note that bool(x==1) is False for the reason in my answer.

( 2013-01-04 09:44:45 +0200 )edit

I'm not sure why you need the simplification. This is a feature (!) of Python, the underlying language of Sage.

sage: (x==1 or x==2)
x == 2


See this stackoverflow question. Basically,

sage: bool(x==1)
False


which makes sense in Sage terms, since we only say an expression is True if we can verify that it is true, and then since the first thing is False, or returns the other thing.

more