https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/feed/Comments on: Simplifying Boolean Expressions2009-12-05T05:55:57+00:00https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25667Comment by Daniel Yankowsky2009-12-05T05:55:57+00:002009-12-05T05:55:57+00:00Daniel Yankowskyhttp://balefrost.org/<p>That's a good insight; you have discovered half of the distributive law of boolean algebra (http://en.wikipedia.org/wiki/Boolean_algebra_(structure)#Definition). The other laws listed there are also useful. I have occasionally been hacking an expression and, because of the absorption law, noticed that some cases were redundant.</p>
<p>I'm curious about what you mean about the number of variables not being fixed. Doesn't that imply that you are using a loop of some sort? Or do you mean that the example was a trimmed-down version of something more complicated in the real code?</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25666Comment by Yan2009-11-19T08:09:41+00:002009-11-19T08:09:41+00:00Yan<p>I think this is just a simple mathematical problem if take AND as set intersection, OR as set union, and negation as the set complement. Using some set operations one can get a uniformly simplest form for these problems (software like Mathematica can do this even for very complicated expression). Don't take my comment too series, I promise my idea is correct but I don't promise it is practical. -:)</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25656Comment by swt2009-09-11T20:09:56+00:002009-09-11T20:09:56+00:00swt<p>about the “c || z && d” vs “d && z || c”</p>
<p>sometimes first form may be better if you know something about the variables such as if c may be frequently true, or z/d are lengthy operations. First one will short circuit rest of the expression if c is true without bothering to evaluate z or d. There are other various short circuits in different orders, so one may be better than others, depending on what variables are doing.</p>
<p>bob</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25654Comment by Allan Odgaard2009-09-02T09:35:07+00:002009-09-02T09:35:07+00:00Allan Odgaardhttp://sigpipe.macromates.com/<p>jpc: Good observation, btw.</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25653Comment by Allan Odgaard2009-09-02T09:30:08+00:002009-09-02T09:30:08+00:00Allan Odgaardhttp://sigpipe.macromates.com/<p>Stephan: As mentioned above, the simplifying was in the context of scalability. The expression I show has 7 different variables but > 7 uses of variables. In the context where this is used, the number of variables is not fixed, so non-linear scaling is undesired.</p>
<p>Harry / Stephan: Parenthesis are good when they aid in readability. In some cases I’d argue they don’t, for example a shell script like:</p>
<pre><code>make || exit 1
</code></pre>
<p>But assume we want to tag on an error message:</p>
<pre><code>make || echo "Error building product" && exit 1
</code></pre>
<p>Or it could have been:</p>
<pre><code>make clean && make || exit 1
</code></pre>
<p>I say parenthesis doesn’t help in readability, but in reality it is the unfamiliarity with what exactly parenthesis do in a shell script, is it grouping? is it a sub shell?</p>
<p>Anyway, remembering operator precedence is always better than not remembering it, as you read code written by other people who may not have added parenthesis, I would e.g. never use parenthesis in something like: <code>32*x + 11*y</code> and I expect everyone reading such expression to know that <code>*</code> binds stronger than <code>+</code>. Someone who writes boolean logic all day probably feels similar about <code>AND</code> and <code>OR</code> :)</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25648Comment by Stephan2009-08-30T01:22:31+00:002009-08-30T01:22:31+00:00Stephan<p>@Sebastian: Karnaugh maps give you a easy way to build a dis- or conjuctive normal form; with already is given at the beginning. It is limited to "two levels", which helps to keep the propagation time short in circuits, but doesn't matter in programming. There may always be a shorter form if one uses more parenthesis.
Furthermore, a karnaugh map is not that easy to build for more than 4 variables; or maybe 6 if you use 3d. Other roles can be found on http://en.wikipedia.org/wiki/Boolean_algebra_(structure)</p>
<p>The final expression uses 6 instead of 9 operators, which roughly should need only 1/3 less time to execute. The compiler probably optimizes both cases to the short form, because it's mathematically exactly the same.</p>
<p>So much about optimizing. But simplifying? In my opinion, the first one was simpler. And save on parenthesis can bite back, too. Just like using the form "c || z && d" which is confusing while reading from left to right instead of "d && z || c". The second one doesn't change it's meaning while reading.</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25638Comment by jpc2009-08-18T06:45:46+00:002009-08-18T06:45:46+00:00jpchttp://jpc.jogger.pl<p>The fun part is that OR is also distributive over AND (and that is quite difficult to see for a normal person when you start switching && and || to dots and pluses). :) Compare:
(A || B) && (A || C) == A || (B && C)
to
(A + B) * (A + C) == A + (B * C)</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25637Comment by Sebastian2009-08-18T03:43:48+00:002009-08-18T03:43:48+00:00Sebastian<p>You can always use a "Karnaugh map" that will gurantee to give you the minimal expression</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25629Comment by Allan Odgaard2009-08-11T20:08:33+00:002009-08-11T20:08:33+00:00Allan Odgaardhttp://sigpipe.macromates.com/<p>The motivation for the optimization was due to number of variables not being fixed, and what I had didn’t scale linearly with number of variables — so it wasn’t exactly a performance optimization.</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25627Comment by Gerd Knops2009-08-11T19:54:08+00:002009-08-11T19:54:08+00:00Gerd Knops<p>The problem is that the previous form was a heck of a lot easier to read (and therefore maintain) than the end result. So unless it gets executed a trillion times (I tend to leave the more readable version.</p>
https://sigpipe.macromates.com/2009/simplifying-boolean-expressions/#comment-25623Comment by Harry Jordan2009-07-27T12:47:57+00:002009-07-27T12:47:57+00:00Harry Jordanhttp://inquisitivesoftware.com/<p>But you an never have too many parenthesis! :)</p>