1
$\begingroup$

How can I implement a Boolean function F(A,B) that has outputs that are not specific and, therefore, the truth table output of this two-input function will be (a, b, c, d) -- here the first term refers to the case F(0,0), the second to F(0,1), and so on.

This is how this Boolean function F(A,B) is represented on Wolfram|Alpha.

How would I then evaluate the simplified Boolean expression for:

F(A, B) AND F(B, A XOR B)?

$\endgroup$
2
  • 1
    $\begingroup$ This is quite unclear. If you show what you expect the result to be that might help considerable. Also should be more clear about what are the expected inputs. Are they always 0 or 1? $\endgroup$ Commented Jan 16, 2013 at 0:30
  • $\begingroup$ @DanielLichtblau I edited to show an example of how I implemented the function on Wolfram Alpha. Yes, this is a Boolean function so the inputs are either 0 or 1. $\endgroup$
    – Khaled
    Commented Jan 16, 2013 at 0:42

3 Answers 3

6
$\begingroup$
f[{a_, b_, c_, d_}] :=  BooleanFunction[Thread[Tuples[{0, 1}, 2] -> {a, b, c, d}]]

Usage

f[{True, False, True, False}][0, 1]
(* False *)
$\endgroup$
6
  • $\begingroup$ Looks perfect. +1 $\endgroup$
    – Mr.Wizard
    Commented Jan 16, 2013 at 8:53
  • $\begingroup$ How could I compute F(A, B) AND F(B, A XOR B) from this? $\endgroup$
    – Khaled
    Commented Jan 16, 2013 at 15:51
  • $\begingroup$ @Khaled This site is about Mathematica, not WolframAlpha. Please post Mathematica code. meta.mathematica.stackexchange.com/questions/265/… $\endgroup$ Commented Jan 16, 2013 at 15:57
  • $\begingroup$ Sorry but I'm sure what I wrote constitutes as Logical functions, no idea where WolframAlpha comes into it exactly. $\endgroup$
    – Khaled
    Commented Jan 16, 2013 at 16:04
  • 1
    $\begingroup$ In your example you created the logical function f[{a, b, c, d}][A,B], how could I use this function to implement the logical expression equivalent to f(A,B) ∧ f(A,A⊻B)? $\endgroup$
    – Khaled
    Commented Jan 16, 2013 at 16:09
4
$\begingroup$

The Wolfram Alpha example suggests you want to treat $0$ and $1$ as the Boolean values False and True respectively and, with this convention, to parameterize an arbitrary binary Boolean operator $f$ by means of its truth table values $(a,b,c,d)$:

f[x_, y_, {a_, b_, c_, d_}] := {1 - x, x} . {{a, b}, {c, d}} . {1 - y, y}

(This method exhibits such binary operators as bilinear forms over the field of two elements.)

Here is a comprehensive test proving this works correctly for inputs in the intended domain (that is, all of $a, \ldots, d$ are either $0$ or $1$ and so are $x$ and $y$). It applies all four possible inputs $(x,y)$ and lists the inputs followed by the values of $f$ when applied to them:

Flatten[Array[{#1, #2, f[#1, #2, {a, b, c, d}]} &, {2, 2}, {0, 0}], 1]

{{0, 0, a}, {0, 1, b}, {1, 0, c}, {1, 1, d}}


The second part of the question could be interpreted as asking how the expression

$$f(x,y,(a,b,c,d)) \text{ and } f(y, x \text{ xor } y, (a,b,c,d))$$

(which evidently is a binary Boolean operator in the arguments $x$ and $y$) ought to be parameterized. Let's work it out in steps using the definitions:

and[x_, y_] := f[x, y, {0, 0, 0, 1}];
xor[x_, y_] := f[x, y, {0, 1, 1, 0}];
p = Table[f[x, y, {a, b, c, d}]~and~f[y, x~xor~y, {a, b, c, d}] , {x, 0, 1}, {y, 0, 1}] // Flatten

{a^2, b d, b c, c d}

More generally, if the two occurrences of $f$ are intended to have two different sets of parameters, the same method works:

p = Table[f[x, y, {a, b, c, d}]~and~f[y, x~xor~y, {a1, b1, c1, d1}],
        {x, 0, 1}, {y, 0, 1}] // Flatten

{a a1, b d1, b1 c, c1 d}

In other words, a simplified version of the compound input expression is

f[x, y, {a a1, b d1, b1 c, c1 d}]
$\endgroup$
1
$\begingroup$

Just a different interpretation:

func[list_] := If[# <= Length@list, list[[#]], $Failed] &[1 + FromDigits[{##}, 2]] &

func[{a, b, c, d}] @@@ Tuples[{0, 1}, 2]
{a, b, c, d}
$\endgroup$
3
  • $\begingroup$ +1 This makes me smile--it's always fun to see how you can reconstruct something with such brevity. $\endgroup$
    – whuber
    Commented Jan 16, 2013 at 14:59
  • $\begingroup$ @whuber Thanks! $\endgroup$
    – Mr.Wizard
    Commented Jan 16, 2013 at 15:13
  • $\begingroup$ In trying it out, though, I notice that it does not seem possible to effect the algebraic reductions I illustrated in the second part of my answer, even when the If clause is removed: the problem is that MMA does not know whether (for example) 1+2a+a1 is a valid index in {0,0,0,1} when a or a1 are symbols. $\endgroup$
    – whuber
    Commented Jan 16, 2013 at 18:33

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.