2
$\begingroup$

We can generate a Boolean function of $n$ variables (take $n=4$ as an example) as follows:

f = BooleanFunction[10, 4]

I want to express $f$ as a polynomial. This is almost achieved by converting the function to the "ANF" form. All that's left is to get Mathematica to replace the And operator by multiplication, and the Xor operator by addition. Any idea how I can do that?

$\endgroup$
3
  • 3
    $\begingroup$ Why not make the replacements directly? expr /. {And -> Times, Xor -> Plus} $\endgroup$ Commented Mar 9, 2016 at 17:16
  • $\begingroup$ @J.M. Close, but not enough for the general case BooleanConvert[BooleanFunction[1, 4], "ANF"] /. {And -> Times, Xor -> Plus} $\endgroup$ Commented Mar 10, 2016 at 2:53
  • $\begingroup$ @Dr. bel, the Not[] is indeed a problem. OP did not give instructions on how it should be dealt with, however. BTW: no need for BooleanConvert[]: BooleanFunction[1, 4, "ANF"]. $\endgroup$ Commented Mar 10, 2016 at 3:01

1 Answer 1

1
$\begingroup$

I believe this is what you want:

bf[n_, m_] := BooleanFunction[n, m, "DNF"] /. {Not[a_] :> (a - 1)^2, And -> Times,
              Xor -> Plus, Or -> Plus} /. (Plus[a__] &) :> (Unitize[Plus[a]] &)

bf[10, 4]
(* (#1 - 1)^2 (#2 - 1)^2 #4 & *)

Checking:

bt[n_, m_] := Boole /@ BooleanTable[BooleanFunction[n, m]]

And @@ ((bt[#, 6] == bf[#, 6] @@@ Tuples[{1, 0}, 6]) & /@ Range[2^6])

( * True *)
$\endgroup$
1
  • $\begingroup$ It works, somehow. But not always return a proper polynomial. Try for example bf[300, 10]. The Unitize[ ] stays in there. Looking for a better option. $\endgroup$ Commented Mar 10, 2016 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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