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?