如何在 Mathematica 中以编程方式显示分区矩阵?

发布于 2024-09-28 21:33:22 字数 205 浏览 2 评论 0原文

我知道使用“插入”菜单,您可以创建一个具有垂直线和水平线的矩阵,但不能创建更通用的分区,例如将 4x4 矩阵划分为 4 个 2x2 分区。 MatrixForm 也不能进行任何类型的分区。那么,我将如何以编程方式显示这样的分区矩阵呢?我想保留 MatrixForm 仅充当包装器而不影响后续评估的能力,但这并不是绝对必要的。我怀疑这会涉及使用Grid,但我还没有尝试过。

I know that using the Insert menu, you can create a matrix with vertical and horizontal lines, but not a more generic partition, such as dividing a 4x4 matrix into 4 2x2 partitions. Nor, can MatrixForm do any sort of partitioning. So, how would I go about programmatically displaying such a partitioned matrix? I would like to retain the ability of MatrixForm to act only as a wrapper and not affect subsequent evaluations, but it is not strictly necessary. I suspect this would involve using a Grid, but I haven't tried it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰つ倒转 2024-10-05 21:33:22

在尝试让 Interpretation 放弃显示的表单并在后续行中使用矩阵时尝试了太长时间后,我放弃了,只是制作了一个几乎与 MatrixForm完全相同的包装器/代码>。这真的很快,因为它是对这个问题的简单修改。

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

然后后缀命令 //pMatrixForm[#, 3, 3]& 将给出所请求的 4x4 矩阵的 2x2 分区。将 pMatrixForm 的默认设置从无分区更改为中央分区可能很有用。这并不难。

After playing around for far too long trying to make Interpretation drop the displayed form and use the matrix when used in subsequent lines, I gave up and just made a wrapper that acts almost exactly like MatrixForm. This was really quick 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 give the requested 2x2 partitioning of a 4x4 matrix. It maybe useful to change the defaults of pMatrixForm from no partitions to central partitions. This would not be hard.

南七夏 2024-10-05 21:33:22

这就是我想出的办法。对于矩阵 M:

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

构造两个 True/False 值列表(True 表示需要分隔符的位置),它们采用两个参数;第一个是矩阵,第二个是分隔符位置列表。

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 分区。

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

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

you construct two list of True/False values (with True for places where you want separators) that take two arguments; first the matrix and second a list of positions for separators.

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

Now the partitioned view using Grid[] is made with the use of Dividers:

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

This takes three arguments; first the matrix, second the list of positions for column dividers, and third the list of values for row dividers.

In order for it to display nicely you just wrap it in brakets and use MatrixForm:

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

Which does the 2by2 partitioning you mentioned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文