Can someone please explain the inconsistent results I'm getting from BooleanTable and BooleanFunction?
Using the following truth table:
tt = {{1, 1, 1, 1} -> 1, {1, 1, 1, 0} -> 1, {1, 1, 0, 1} -> 1,
{1, 1, 0, 0} -> 0, {1, 0, 1, 1} -> 1, {1, 0, 1, 0} -> 1,
{1, 0, 0, 1} -> 0, {1, 0, 0, 0} -> 0, {0, 1, 1, 1} -> 1,
{0, 1, 1, 0} -> 0, {0, 1, 0, 1} -> 0, {0, 1, 0, 0} -> 0,
{0, 0, 1, 1} -> 0, {0, 0, 1, 0} -> 0, {0, 0, 0, 1} -> 0,
{0, 0, 0, 0} -> 0};
I can produce the following boolean expression:
bb = BooleanFunction[tt, {a1, a0, b1, b0}]
(* (a0 && a1 && b0) || (a0 && b0 && b1) || (a1 && b1) *)
However, if I compute the truth values from this expression, I get different values from the original truth table:
tt1 = Boole[BooleanTable[bb]]
(* {1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0} *)
If I turn this into a truth table and compute its boolean expression, I get a different boolean expression from the first one computed:
tt1Full = MapThread[Rule,
{Keys[BooleanTable[Boole[{a1, a0, b1, b0}] -> 0, {a1, a0, b1, b0}]], tt1}
]
(* {{1, 1, 1, 1} -> 1, {1, 1, 1, 0} -> 1, {1, 1, 0, 1} -> 1,
{1, 1, 0, 0} -> 0, {1, 0, 1, 1} -> 1, {1, 0, 1, 0} -> 0,
{1, 0, 0, 1} -> 0, {1, 0, 0, 0} -> 0, {0, 1, 1, 1} -> 1,
{0, 1, 1, 0} -> 0, {0, 1, 0, 1} -> 1, {0, 1, 0, 0} -> 0,
{0, 0, 1, 1} -> 0, {0, 0, 1, 0} -> 0, {0, 0, 0, 1} -> 0,
{0, 0, 0, 0} -> 0} *)
bb1 = BooleanFunction[tt1Full, {a1, a0, b1, b0}]
(* (a0 && a1 && b1) || (a0 && b0) || (a1 && b0 && b1) *)
However, if I repeat this process, I end up with the original truth table and boolean expression it produces:
tt2 = Boole[BooleanTable[bb1]]
(* {1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0} *)
tt2Full = MapThread[Rule,
{Keys[BooleanTable[Boole[{a1, a0, b1, b0}] -> 0, {a1, a0, b1, b0}]], tt2}
]
(* {{1, 1, 1, 1} -> 1, {1, 1, 1, 0} -> 1, {1, 1, 0, 1} -> 1,
{1, 1, 0, 0} -> 0, {1, 0, 1, 1} -> 1, {1, 0, 1, 0} -> 1,
{1, 0, 0, 1} -> 0, {1, 0, 0, 0} -> 0, {0, 1, 1, 1} -> 1,
{0, 1, 1, 0} -> 0, {0, 1, 0, 1} -> 0, {0, 1, 0, 0} -> 0,
{0, 0, 1, 1} -> 0, {0, 0, 1, 0} -> 0, {0, 0, 0, 1} -> 0,
{0, 0, 0, 0} -> 0} *)
bb2 = BooleanFunction[tt2Full, {a1, a0, b1, b0}]
(* (a0 && a1 && b0) || (a0 && b0 && b1) || (a1 && b1) *)
bb = BooleanFunction[tt, {a1, a0, b1, b0}]
evaluates to a different function than if you switch the order of the symbols:bb = BooleanFunction[tt, {a1, b1, a0, b0}]
. ThenBoole[BooleanTable[bb]]
is different in the two cases. $\endgroup$ – march Aug 24 '16 at 20:40{a0, a1, b0, b1}
(the canonical Mathematica ordering of those symbols) throughout, everything is consistent. $\endgroup$ – march Aug 24 '16 at 20:45Cases[BooleanTable[bb] // Trace, {a0, a1, b0, b1}, Infinity]
with your originalbb
, you find many instances of{a0, a1, b0, b1}
, but if you evaluateCases[BooleanTable[bb] // Trace, {a1, a0, b1, b0}, Infinity]
, you get nothing back. The point, I think, is thatBooleanTable
cannot know the original ordering of the symbols, and so it uses the canonical ordering, which messes things up. $\endgroup$ – march Aug 24 '16 at 20:53