First time here? Check out the FAQ!

Ask Your Question
0

Behavior of 'or'

asked 12 years ago

sage_learner gravatar image

updated 10 years ago

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 !

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 12 years ago

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.

Preview: (hide)
link

Comments

Thank you.

sage_learner gravatar imagesage_learner ( 12 years ago )

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

kcrisman gravatar imagekcrisman ( 12 years ago )
1

answered 12 years ago

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.

Preview: (hide)
link

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: 12 years ago

Seen: 471 times

Last updated: Jan 04 '13