Reputation: 10855
Continuing with my matrix multiplication question, I want to show the following expression in explicit viewable form in mma:
Even if in the case I give a11, ..., b11, ... explicit numbers, I still want it to be (0&&1)||(1&&1) in unevaluated form. Can anyone please help?
Upvotes: 2
Views: 338
Reputation: 18271
One way to achieve this is to define your own matrix wrapper. The wrapper approach has the advantage that you can overload as many built-in functions as you like without impacting any other functionality.
Let's start by defining a wrapper called myMatrix
that displays itself using MatrixForm
:
Format[myMatrix[m_]] ^:= MatrixForm[m]
Next, we'll overload the Times
operator when it acts on myMatrix
:
myMatrix[m1_] myMatrix[m2_] ^:= myMatrix[Inner[And, m1, m2, Or]]
Note that both definitions use ^:=
to attach the rules to myMatrix
as up-values. This is crucial to ensure that the regular built-in definitions are otherwise unaffected.
Armed with these definitions, we can now achieve the desired goal. To demonstrate, let's define two matrices:
m1 = myMatrix[Array[Subscript[a, Row[{##}]]&, {2, 2}]]
m2 = myMatrix[Array[Subscript[b, Row[{##}]]&, {2, 2}]]
The requested "explicitly viewable form" can now be generated thus:
Row[{m1, m2}] == m1 m2
... or, if you prefer to reference Times
explicitly on the left-hand side of the equation:
With[{m1 = m1, m2 = m2}, HoldForm[m1 m2] == m1 m2]
Next, we'll assign random boolean values to each of the matrix elements:
Evaluate[First /@ {m1, m2}] = RandomInteger[1, {2, 2, 2}];
... and then generate the explicitly viewable form once again with the assignments in place:
With[{m1 = m1, m2 = m2}, HoldForm[m1 m2] == m1 m2]
Upvotes: 2
Reputation: 16232
I don't think this is a really good idea (overloading internal functions and all; and && is And not BitAnd, which you wanted to use in the previous question), but you asked for it and you get it:
CircleTimes[a_?MatrixQ, b_?MatrixQ] :=
Inner[HoldForm[BitAnd[##]] &, a, b, HoldForm[BitOr[##]] &]
Unprotect[BitAnd];
Unprotect[BitOr];
BitAnd /: Format[BitAnd[a_, b_]] := a && b;
BitOr /: Format[BitOr[a_, b_]] := a || b;
Protect[BitAnd];
Protect[BitOr]
mat1 = Array[Subscript[a, #1, #2] &, {2, 2}];
mat2 = Array[Subscript[b, #1, #2] &, {2, 2}];
The advantage of defining the operation as CircleTimes is that you get the CircleTimes symbol and operator for free.
Upvotes: 2
Reputation: 5954
Use
Inner[And, Array[Subscript[a, ##] &, {2, 2}],
Array[Subscript[b, ##] &, {2, 2}], Or] // MatrixForm
Edit. Having followed up on your previous question, I think you might consider
Inner[HoldForm[And[##]] &, amat,
bmat, HoldForm[Or[##]] &] // MatrixForm
Upvotes: 3
Reputation: 24336
(0&&1)||(1&&1)
does not evaluate, so I do not see the problem. For True
and False
have you tried using HoldForm?
Upvotes: 2