How to programmatically display a partitioned matrix in Mathematica?

I know that using the "Insert" menu you can create a matrix with vertical and horizontal lines, but no more general section, for example, dividing a 4x4 matrix into 4 2x2 sections. In addition, it MatrixFormcan perform any splitting. So, how could I programmatically display such a partitioned matrix? I would like to retain the ability to MatrixFormact only as a wrapper and not affect subsequent evaluations, but this is not strictly necessary. I suspect this is due to use Grid, but I have not tried it.

+3
source share
2 answers

After losing too long, trying to make the Interpretationdiscarded form appear and use the matrix when used in the next lines, I gave up and just made a wrapper that works almost the same way MatrixForm. It was very fast, as it was a simple modification of this question .

Clear[pMatrixForm,pMatrixFormHelper]
pMatrixForm[mat_,col_Integer,row_:{}]:=pMatrixForm[mat,{col},row]
pMatrixForm[mat_,col_,row_Integer]:=pMatrixForm[mat,col,{row}]

pMatrixFormHelper[mat_,col_,row_]:=Interpretation[MatrixForm[
    {Grid[mat,Dividers->{Thread[col->True],Thread[row->True]}]}],mat]

pMatrixForm[mat_?MatrixQ,col:{___Integer}:{},row:{___Integer}:{}]:=
  (CellPrint[ExpressionCell[pMatrixFormHelper[mat,col,row],
     "Output",CellLabel->StringJoin["Out[",ToString[$Line],"]//pMatrixForm="]]];
   Unprotect[Out];Out[$Line]=mat;Protect[Out];mat;)

Then the postfix command //pMatrixForm[#, 3, 3]&will provide the requested split into 2x4x4 matrices. It might be useful to change the default values pMatrixFormwithout going to the central partitions. It's not hard.

+3
source

So this is what I came up with. For matrix M:

M = {{a, b, 0, 0}, {c, d, 0, 0}, {0, 0, x, y}, {0, 0, z, w}};

you create two lists of True / False values ​​(with True for places where you need separators) that take two arguments; first a matrix and a second list of positions for delimiters.

colSep = Fold[ReplacePart[#1, #2 -> True] &, 
              Table[False, {First@Dimensions@#1 + 1}], #2] &;
rowSep = Fold[ReplacePart[#1, #2 -> True] &, 
              Table[False, {Last@Dimensions@#1 + 1}], #2] &;

Grid[] Dividers:

partMatrix = Grid[#1, Dividers -> {colSep[#1, #2], rowSep[#1, #3]}] &;

; , .

, , MatrixForm:

MatrixForm@{partMatrix[M, {3}, {3}]}

2by2 .

+3

Source: https://habr.com/ru/post/1770532/


All Articles