Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)?

share|improve this question
Welcome to Mathematica.SE! I suggest the following : 1) As you receive help, try to give it too, by answering questions in your area of expertise.2) Read the FAQs! 3) When you see good Q & A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge.ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign. – DavidC Jan 16 '13 at 0:27
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? – Daniel Lichtblau Jan 16 '13 at 0:30
@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. – Khaled Jan 16 '13 at 0:42
up vote 6 down vote accepted
f[{a_, b_, c_, d_}] :=  BooleanFunction[Thread[Tuples[{0, 1}, 2] -> {a, b, c, d}]]


f[{True, False, True, False}][0, 1]
(* False *)
share|improve this answer
Looks perfect. +1 – Mr.Wizard Jan 16 '13 at 8:53
How could I compute F(A, B) AND F(B, A XOR B) from this? – Khaled Jan 16 '13 at 15:51
@Khaled This site is about Mathematica, not WolframAlpha. Please post Mathematica code.… – Dr. belisarius Jan 16 '13 at 15:57
Sorry but I'm sure what I wrote constitutes as Logical functions, no idea where WolframAlpha comes into it exactly. – Khaled Jan 16 '13 at 16:04
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)? – Khaled Jan 16 '13 at 16:09

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}]
share|improve this answer

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}
share|improve this answer
+1 This makes me smile--it's always fun to see how you can reconstruct something with such brevity. – whuber Jan 16 '13 at 14:59
@whuber Thanks! – Mr.Wizard Jan 16 '13 at 15:13
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. – whuber Jan 16 '13 at 18:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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