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.Wed, 11 Sep 2013 17:04:00 +0200Optimization of boolean evaluationhttps://ask.sagemath.org/question/10535/optimization-of-boolean-evaluation/In a code like
if (P_1 or P_2 or ... or P_n):
or
if (Q_1 and Q_2 and ... and Q_n):
if only one P_i is true, or only one Q_i is false,then the boolean evaluation of the whole propositions (P_1 or P_2 or ... or P_n), (Q_1 and Q_2 and ... and Q_n) are determined.
So in a goal of optimization, the program could stop to evaluate the P_i from the first true.
> Does SAGE stop to evaluate like that ? (if no, is there a way to do that ?)
Wed, 11 Sep 2013 15:02:54 +0200https://ask.sagemath.org/question/10535/optimization-of-boolean-evaluation/Answer by ndomes for <p>In a code like </p>
<pre><code>if (P_1 or P_2 or ... or P_n):
</code></pre>
<p>or </p>
<pre><code>if (Q_1 and Q_2 and ... and Q_n):
</code></pre>
<p>if only one P_i is true, or only one Q_i is false,then the boolean evaluation of the whole propositions (P_1 or P_2 or ... or P_n), (Q_1 and Q_2 and ... and Q_n) are determined. <br/>
So in a goal of optimization, the program could stop to evaluate the P_i from the first true. </p>
<blockquote>
<p>Does SAGE stop to evaluate like that ? (if no, is there a way to do that ?)</p>
</blockquote>
https://ask.sagemath.org/question/10535/optimization-of-boolean-evaluation/?answer=15445#post-id-15445See:
http://docs.python.org/2/tutorial/datastructures.html
"The Boolean operators and and or are so-called short-circuit operators: their arguments are evaluated from left to right, and evaluation stops as soon as the outcome is determined." Wed, 11 Sep 2013 17:04:00 +0200https://ask.sagemath.org/question/10535/optimization-of-boolean-evaluation/?answer=15445#post-id-15445