ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 04 Jan 2013 09:44:45 +0100Behavior of 'or'https://ask.sagemath.org/question/9686/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 !
Fri, 04 Jan 2013 08:21:04 +0100https://ask.sagemath.org/question/9686/behavior-of-or/Answer by ppurka for <p>Hello !</p>
<p>Here I have observed something in sage.</p>
<p>sage: (x == 1 or x == -3).full_simplify()</p>
<p>x == -3</p>
<p>sage: (x == -1 or x == -3).full_simplify()</p>
<p>x == -3</p>
<p>sage: (x == -1 or x == 3).full_simplify()</p>
<p>x == 3</p>
<p>sage: (x == -1 or x == -3).full_simplify()</p>
<p>x == -3</p>
<p>sage: (x == 1 or x == -3).full_simplify() </p>
<p>x == -3</p>
<p>I wish to know that why sage replies with the second argument. It is very necessary for me to understand this behavior of sage. </p>
<p>Many Thanks !</p>
https://ask.sagemath.org/question/9686/behavior-of-or/?answer=14426#post-id-14426I 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](http://docs.python.org/2/reference/expressions.html#boolean-operations). Notice how it says that `and` and `or` return the last evaluated argument and their output may not be restricted to `True` or `False`.Fri, 04 Jan 2013 09:18:30 +0100https://ask.sagemath.org/question/9686/behavior-of-or/?answer=14426#post-id-14426Comment by kcrisman for <p>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, <code>(x==1) or (x==-3)</code>:</p>
<ol>
<li>First, the part of the expression before <code>or</code> is evaluated. So, most probably <code>bool(x==1)</code> is being run to find out if it evaluates to <code>True</code> or <code>False</code>.</li>
<li>Now, <code>bool(x==1)</code> will always return <code>False</code>, so the result of the expression becomes the result of whatever <code>(x==-3)</code> is.</li>
<li><code>x</code> is a symbolic variable and Sage by default does <em>not</em> evaluate an expression like <code>x==-3</code> to give the truth value of the expression. What Sage does is keep the expression unmodified.</li>
</ol>
<p>This also goes into the behavior of <code>or</code> and <code>and</code> in Python itself. In particular, read the Note at the end of the <a href="http://docs.python.org/2/reference/expressions.html#boolean-operations">section here</a>. Notice how it says that <code>and</code> and <code>or</code> return the last evaluated argument and their output may not be restricted to <code>True</code> or <code>False</code>.</p>
https://ask.sagemath.org/question/9686/behavior-of-or/?comment=18441#post-id-18441Nice work - I hadn't refreshed the page. Note that `bool(x==1)` is `False` for the reason in my answer.Fri, 04 Jan 2013 09:44:45 +0100https://ask.sagemath.org/question/9686/behavior-of-or/?comment=18441#post-id-18441Comment by sage_learner for <p>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, <code>(x==1) or (x==-3)</code>:</p>
<ol>
<li>First, the part of the expression before <code>or</code> is evaluated. So, most probably <code>bool(x==1)</code> is being run to find out if it evaluates to <code>True</code> or <code>False</code>.</li>
<li>Now, <code>bool(x==1)</code> will always return <code>False</code>, so the result of the expression becomes the result of whatever <code>(x==-3)</code> is.</li>
<li><code>x</code> is a symbolic variable and Sage by default does <em>not</em> evaluate an expression like <code>x==-3</code> to give the truth value of the expression. What Sage does is keep the expression unmodified.</li>
</ol>
<p>This also goes into the behavior of <code>or</code> and <code>and</code> in Python itself. In particular, read the Note at the end of the <a href="http://docs.python.org/2/reference/expressions.html#boolean-operations">section here</a>. Notice how it says that <code>and</code> and <code>or</code> return the last evaluated argument and their output may not be restricted to <code>True</code> or <code>False</code>.</p>
https://ask.sagemath.org/question/9686/behavior-of-or/?comment=18442#post-id-18442Thank you.Fri, 04 Jan 2013 09:37:40 +0100https://ask.sagemath.org/question/9686/behavior-of-or/?comment=18442#post-id-18442Answer by kcrisman for <p>Hello !</p>
<p>Here I have observed something in sage.</p>
<p>sage: (x == 1 or x == -3).full_simplify()</p>
<p>x == -3</p>
<p>sage: (x == -1 or x == -3).full_simplify()</p>
<p>x == -3</p>
<p>sage: (x == -1 or x == 3).full_simplify()</p>
<p>x == 3</p>
<p>sage: (x == -1 or x == -3).full_simplify()</p>
<p>x == -3</p>
<p>sage: (x == 1 or x == -3).full_simplify() </p>
<p>x == -3</p>
<p>I wish to know that why sage replies with the second argument. It is very necessary for me to understand this behavior of sage. </p>
<p>Many Thanks !</p>
https://ask.sagemath.org/question/9686/behavior-of-or/?answer=14427#post-id-14427I'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](http://stackoverflow.com/questions/13870378/python-or-operator-weird-behavior). 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.Fri, 04 Jan 2013 09:43:42 +0100https://ask.sagemath.org/question/9686/behavior-of-or/?answer=14427#post-id-14427