I want to demonstrate multiplication of partitioned matrices as in the example here. Using the Insert Menu, you can build a matrix and draw lines between rows and columns. However, I want to be able to select a subset of the column and row lines to add. Then I would like to be able to compute the product of two compatible matrices (so the column partition on the left matrix matches the row partition on the right matrix) and have the output show as a partitioned matrix where the row dividers come from the row dividers on the left matrix and the column dividers come from the column dividers on the right matrix. In the example, this matrix multiplication is defined since the column partition on A is, say, {2} and the same for the row partition on B. The resulting matrix C has a row divider after the 2nd row since A had a row divider after the second row (while B had no column dividers).
Answer
a = {{3, 0, -1}, {-5, 2, 4}, {-2, -6, 3}};
b = {{1, 2}, {-3, 4}, {2, 1}};
grdF = Grid[#1, Dividers -> {#2, #3}] &;
ga = grdF[a, {3 -> Red}, {3 -> Red}];
gb = grdF[b, False, {3 -> Red}];
gab = grdF[ga[[1]].gb[[1]],First[Dividers /. Options[gb]], Last[ Dividers /. Options[ga]]];
Row[{ga, "\[Times]", gb, " = ", gab}, Spacer[3]]
Update: Wrapping matrices with square brackets:
bracketF = (ToBoxes[#] /.
TagBox[x : GridBox[__], y__] :>
TagBox[RowBox[{StyleBox["[", SpanMaxSize -> \[Infinity]], x,
StyleBox["]", SpanMaxSize -> \[Infinity]]}], y] // DisplayForm) &;
Row[{bracketF@ga, "\[Times]", bracketF@gb, " = ", bracketF@ gab}, Spacer[3]]
Comments
Post a Comment