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).


1 Answer 1

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]]

enter image description here

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]] 

enter image description here

  • $\begingroup$ Thanks. That is better and easier than I expected. I have been wondering in general how to express matrices with [ ] 's around them, instead of ( )'s. Is there a way to wrap these matrices in [ ] 's? If so, would this method extend to a way to do so for matrices in other cells such as Text cells? $\endgroup$ Commented Jul 23, 2014 at 17:16
  • $\begingroup$ I came up with this for wrapping the matrix with []. But I simply combine this with the function given. matrixform[mat_] := RowBox[{StyleBox["[", SpanMaxSize -> \[Infinity]], GridBox[mat], StyleBox["]", SpanMaxSize -> \[Infinity]]}] // DisplayForm // TraditionalForm $\endgroup$ Commented Jul 23, 2014 at 18:37
  • $\begingroup$ Edit to the above comment. But I can't simply combine these functions. Any ideas on how to do so? $\endgroup$ Commented Jul 23, 2014 at 18:43
  • $\begingroup$ Found a modification of matrixform above that handles dividers: matrixform2 = DisplayForm[RowBox[{StyleBox["[", SpanMaxSize -> [Infinity]], GridBox[#1,GridBoxDividers -> {"Columns" -> ReplacePart[Table[False, {Dimensions[#1][[2]]}],(Map[List, #2] + 1) -> Red], "Rows" -> ReplacePart[Table[False, {Dimensions[#1][[1]]}], (Map[List, #3] + 1) -> Red]}], StyleBox["]", SpanMaxSize -> [Infinity]]}]] &; matrixform2[a,{2},{2}] does what I want but not matrixform2[a.b,{2},{2}] $\endgroup$ Commented Jul 23, 2014 at 21:02
  • $\begingroup$ I can pass in 2a or a+1 but I cannot pass in a product of matrices. Any ideas of how to change that? $\endgroup$ Commented Jul 23, 2014 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.