Ask Your Question
0

Behavior of 'or'

asked 2013-01-04 08:21:04 +0200

sage_learner gravatar image

updated 2014-11-10 16:56:30 +0200

tmonteil gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-01-04 09:18:30 +0200

ppurka gravatar image

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.

edit flag offensive delete link more

Comments

Thank you.

sage_learner gravatar imagesage_learner ( 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.

kcrisman gravatar imagekcrisman ( 2013-01-04 09:44:45 +0200 )edit
1

answered 2013-01-04 09:43:42 +0200

kcrisman gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-01-04 08:21:04 +0200

Seen: 340 times

Last updated: Jan 04 '13