3
$\begingroup$

I have a logic expression: f0[a0_, a1_, a2_, a3_] := a0 And Not a1 And Not a2 And a3 Or Not a0 And a2 And a3 Or Not a0 And a1 And a3, I know I should use BooleanTable, but it cannot generate a table like below.

How to generate a truth table in mathematica like below?

enter image description here

$\endgroup$
  • 1
    $\begingroup$ There is the BooleanTable command. Does it help? For creating the corresponding function you can use logical and: && and logical or: || in case you don't know. $\endgroup$ – user59583 Dec 3 '18 at 6:17
  • $\begingroup$ @Buddha_the_Scientist Thank you, I have correct my expression, Of course BooleanTable can help me, but it cannot generate a table like the picture above, it only returns a series of true or false. $\endgroup$ – 余星佑 Dec 3 '18 at 6:23
  • $\begingroup$ Then use the TableForm with the option TableHeadings for the result. Something like: TableForm[{{a, b, c, d}}, TableHeadings -> {None, {"a0", "a1", "a2", "a3"}}] $\endgroup$ – user59583 Dec 3 '18 at 6:24
  • $\begingroup$ @Buddha_the_Scientist, thank you, but how to use my function to generate all series of possible outputs like this? I know that how to generate one possible output commad: TableForm[{{True, False, True, False, f0[True, False, True, False]}}, TableHeadings -> {None, {"a0", "a1", "a2", "a3", "b0"}}] $\endgroup$ – 余星佑 Dec 3 '18 at 6:44
2
$\begingroup$

f = a0 && ! a1 && ! a2 && a3

TableForm[BooleanTable[{a0, a1, a2, a3, f}, {a0, a1, a2, a3}], TableHeadings -> {None, {a0, a1, a2, a3, f}}]

Apparently there are resources like this one or this one

$\endgroup$
  • $\begingroup$ Thank you very much! This absolutely solve my quesiton! $\endgroup$ – 余星佑 Dec 3 '18 at 7:20
1
$\begingroup$
truthTableFormattor[rawData_] := Insert[Insert[
Grid[rawData /. {0 -> 0, 
   1 -> Item[1, Background -> Lighter[Magenta]]}, 
 FrameStyle -> Gray, 
 Frame -> All], {Background -> {None, {GrayLevel[0.7], {White}}}, 
 Dividers -> {Black, {2 -> Black}}, Frame -> True, 
 Spacings -> {2, {2, {0.7}, 2}}}, 2], {Dividers -> All, 
Spacings -> .7 {1, 1}}, 2];


truthTable[f__] :=  Module[{}, atoms = Cases[Most[{f}],
 (a_ /;Length[a] == 0 \[And] Not[StringQ[a]])];heads = 
 ToString[TraditionalForm@#] & /@ {f};
  rawData = Transpose@Boole[BooleanTable[#, atoms] & /@ {f}];
  If[Last[{f}] === 1, 
   Transpose@Boole[BooleanTable[#, atoms] & /@ Most[{f}]], 
    If[Last[{f}] === "rev", 
    truthTableFormattor[{ToString[
       TraditionalForm@#] & /@ (Most@{f})}~Join~
      Transpose[(Reverse /@ 
      Boole[BooleanTable[#, atoms] & /@ (Most@{f})])]], 
     truthTableFormattor[{heads}~Join~rawData]]]];




f0[a0_, a1_, a2_, a3_] := a0 \[And] \[Not] a1 \[And] \[Not] a2 \[And] a3;

truthTable[a0, a1, a2, a3, f0[a0, a1, a2, a3]]

enter image description here

$\endgroup$
  • $\begingroup$ Wow your answer is more perfect $\endgroup$ – 余星佑 Dec 20 '18 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.