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, 17 Jul 2020 22:55:31 +0200Evaluation of logical compound expressionshttps://ask.sagemath.org/question/52483/evaluation-of-logical-compound-expressions/ sage: foo=sin(x) > 0 ; foo
sin(x) > 0
sage: bar=cos(x)>0 ; bar
cos(x) > 0
I do not understand this :
sage: foo and bar
sin(x) > 0
sage: foo or bar
cos(x) > 0
As long as `x` has no value, these expressions can't be evaluated or simplified. Could some kind soul enlighten my confused mind ?Wed, 15 Jul 2020 23:38:13 +0200https://ask.sagemath.org/question/52483/evaluation-of-logical-compound-expressions/Answer by jaydfox for <pre><code>sage: foo=sin(x) > 0 ; foo
sin(x) > 0
sage: bar=cos(x)>0 ; bar
cos(x) > 0
</code></pre>
<p>I do not understand this :</p>
<pre><code>sage: foo and bar
sin(x) > 0
sage: foo or bar
cos(x) > 0
</code></pre>
<p>As long as <code>x</code> has no value, these expressions can't be evaluated or simplified. Could some kind soul enlighten my confused mind ?</p>
https://ask.sagemath.org/question/52483/evaluation-of-logical-compound-expressions/?answer=52484#post-id-52484From my testing, it appears that foo is evaluating to False. Hence, in the expression "foo and bar", the foo is evaluating as False, so it skips evaluation of bar. As such, it is returning the first operand, foo, which is sin(x) > 0.
In the second expression, "foo or bar", the foo evaluates as False. With an "or" operator, the second operand must be evaluated. As such, it's returning the second operand, bar, cos(x) > 0.
You can see similar behavior of the "and" and "or" operators with the following:
sage: 0.0 and sin(x)
0.000000000000000
sage: 0.0 or sin(x)
sin(x)
Edit: Sorry, this probably didn't answer the original question. I can't tell you *why* foo is being evaluated as False, given the unknown value of x. However, I can reasonably assume that foo *is* being evaluated as False. If you plug in a value for x, like foo(2.0), then it correctly evaluates as True.
Thu, 16 Jul 2020 00:04:24 +0200https://ask.sagemath.org/question/52483/evaluation-of-logical-compound-expressions/?answer=52484#post-id-52484Answer by Emmanuel Charpentier for <pre><code>sage: foo=sin(x) > 0 ; foo
sin(x) > 0
sage: bar=cos(x)>0 ; bar
cos(x) > 0
</code></pre>
<p>I do not understand this :</p>
<pre><code>sage: foo and bar
sin(x) > 0
sage: foo or bar
cos(x) > 0
</code></pre>
<p>As long as <code>x</code> has no value, these expressions can't be evaluated or simplified. Could some kind soul enlighten my confused mind ?</p>
https://ask.sagemath.org/question/52483/evaluation-of-logical-compound-expressions/?answer=52492#post-id-52492Okay. As `jaydfox` points out, there are three things :
- `and` evaluates "lazily" from left to right, and stops at the first argument evaluated to `False`.
- Anything that can't be proven `True` evaluates to `False`.
- `and` returns the first `False` argument ***unevaluated***.
The last one baffles me. it means that I can't write ` if sin(x>0 and cos(x>0): doSomething()`: I would `doSomething()` if x was unbound....
I need to write `if (sin(x)>0 and cos(x)>0) is True: doSomething()`. Thu, 16 Jul 2020 09:44:23 +0200https://ask.sagemath.org/question/52483/evaluation-of-logical-compound-expressions/?answer=52492#post-id-52492Comment by jaydfox for <p>Okay. As <code>jaydfox</code> points out, there are three things :</p>
<ul>
<li><p><code>and</code> evaluates "lazily" from left to right, and stops at the first argument evaluated to <code>False</code>.</p></li>
<li><p>Anything that can't be proven <code>True</code> evaluates to <code>False</code>.</p></li>
<li><p><code>and</code> returns the first <code>False</code> argument <strong><em>unevaluated</em></strong>. </p></li>
</ul>
<p>The last one baffles me. it means that I can't write <code>if sin(x>0 and cos(x>0): doSomething()</code>: I would <code>doSomething()</code> if x was unbound....</p>
<p>I need to write <code>if (sin(x)>0 and cos(x)>0) is True: doSomething()</code>. </p>
https://ask.sagemath.org/question/52483/evaluation-of-logical-compound-expressions/?comment=52544#post-id-52544Hmm, I checked that last one. Here's what I'm seeing:
sage: if (sin(x)>0 and cos(x)>0): print("Hello!")
sage: if (sin(x)>0 or cos(x)>0): print("Hello!")
sage: if not (sin(x)>0 and cos(x)>0): print("Hello!")
Hello!Fri, 17 Jul 2020 22:55:31 +0200https://ask.sagemath.org/question/52483/evaluation-of-logical-compound-expressions/?comment=52544#post-id-52544