Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted
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])

*)
share|improve this answer

Your Answer

 
discard

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.