3
$\begingroup$

I have two Boolean matrices, that both have dimensions m x n. I want to create a function TableImplies, which would return True if one matrix piecewise implies the other; in other words, given matrices A = (a_ij) and B = (b_ij), then

TableImplies[A, B] = true if for all i and j, a_ij => b_ij.

For example:

truePattern = Table[True, {2}, {3}]

{{True, True, True}, {True, True, True}}

falsePattern = Table[False, {2}, {3}]

{{False, False, False}, {False, False, False}}

TableImplies[falsePattern, truePattern]

True

TableImplies[truePattern, falsePattern]

False

randomPattern = Table[RandomInteger[] == 1, {2}, {3}]

{{True, True, False}, {True, True, False}}

TableImplies[randomPattern, randomPattern]

True

What I got so far:

ElementWiseImplies[x_, y_] = Implies[x, y]

SetAttributes[ElementWiseImplies, Listable]

ElementWiseImplies[RandomPattern, RandomPattern]

{{True, True, True}, {True, True, True}}

Now this is all good. Next:

Apply[And, %, {0, 2}]

True

That seems to work. But if I do like this:

TableImplies[x_, y_] = Apply[And, ElementWiseImplies[x, y], {0, 2}]

x && y

the result is not what I wish. I understand, why this is happening: Mathematica applies And not to the result of ElementWiseImplies, but to the expression ElementWiseImplies[x, y] itself, so the result becomes And[x, y], which is not what I expected.

What should I do? Use local variable x = ElementWiseImplies[x, y]; and then Apply[And, x, {0, 2}]? Or is there more elegant solution?

$\endgroup$
4
$\begingroup$
tableImplies[a_, b_] := And @@ Flatten@MapThread[Implies, {a, b}, 2]

usage

a = Table[aa[i, j], {i, 2}, {j, 2}];
b = Table[bb[i, j], {i, 2}, {j, 2}];
tableImplies[a, b]
(*

(aa[1, 1] \[Implies] bb[1, 1]) && 
(aa[1, 2] \[Implies] bb[1, 2]) && 
(aa[2, 1] \[Implies] bb[2, 1]) && 
(aa[2, 2] \[Implies] bb[2, 2])

*)
$\endgroup$

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.