3
$\begingroup$

How can I create a permutation automatically with logical expressions ? For example (2 logical variables):

Not[a] && Not[b]
Not[a] && b
a && Not[b]
a && b
$\endgroup$
  • 1
    $\begingroup$ This should give you a start Outer[(#1 ∧ #2) &, {a, Not[a]}, {b, Not[b]}] $\endgroup$ – Sektor Feb 9 '15 at 15:37
  • 1
    $\begingroup$ More generally, vars = {a, b, c}; Outer[And, Sequence @@ ({#, Not@#} & /@ vars)] // Flatten $\endgroup$ – Bob Hanlon Feb 9 '15 at 15:54
3
$\begingroup$
Table[BooleanMinterms[{i}, #], {i, 0, 2^Length[#] - 1}] &[{a, b}]
(* {! a && ! b, ! a && b, a && ! b, a && b} *)

Table[BooleanMinterms[{i}, #], {i, 0, 2^Length[#] - 1}] &[{a, b, c}]
(* {! a && ! b && ! c, ! a && ! b && c, ! a && b && ! c, ! a && b && c, 
      a && ! b && ! c, a && ! b && c, a && b && ! c, a && b && c} *)

Also

Tuples[And @@ ({#, ! #} & /@ #)] &[{a, b}]
(* same output as above *)
Tuples[And @@ ({#, ! #} & /@ #)] &[{a, b, c}]
(* same output as above *)
$\endgroup$
  • $\begingroup$ Nice use of a non-List head in Tuples. $\endgroup$ – Mr.Wizard Feb 9 '15 at 16:46

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.