三个矩阵的乘积最终会得到一个奇数块矩阵?

发布于 2024-11-15 08:48:33 字数 236 浏览 5 评论 0原文

在下面的mathematica代码中,

a1 = {{0, -I}, {I, 0}}
a2 = {{0, 1}, {1, 0}}
a3 = {{1, 0}, {0, -1}}
c = I*a1*a2 // MatrixForm
d = c*a3 // MatrixForm

d的显示显示为一个二乘二的矩阵,其中1,1和2,2元素本身就是2x2矩阵,而我希望它是一个普通的旧2x2标量矩阵?

In the following bit of mathematica code

a1 = {{0, -I}, {I, 0}}
a2 = {{0, 1}, {1, 0}}
a3 = {{1, 0}, {0, -1}}
c = I*a1*a2 // MatrixForm
d = c*a3 // MatrixForm

The display of d shows up as a two by two matrix, where the 1,1 and 2,2 elements are themselves 2x2 matrixes, whereas I expect it to be a plain old 2x2 matrix of scalars?

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

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

发布评论

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

评论(2

゛清羽墨安 2024-11-22 08:48:33
use () to protect expression from MatrixFrom which is a wrapper.
use '.' for matrix multiplication. Not '*'

a1 = {{0, -I}, {I, 0}}
a2 = {{0, 1}, {1, 0}}
a3 = {{1, 0}, {0, -1}}
(c = I a1.a2) // MatrixForm
(d = c.a3) // MatrixForm

这是我得到的 d 的输出:

(1  0
 0  1)
use () to protect expression from MatrixFrom which is a wrapper.
use '.' for matrix multiplication. Not '*'

a1 = {{0, -I}, {I, 0}}
a2 = {{0, 1}, {1, 0}}
a3 = {{1, 0}, {0, -1}}
(c = I a1.a2) // MatrixForm
(d = c.a3) // MatrixForm

This is the output I get for d:

(1  0
 0  1)
朮生 2024-11-22 08:48:33

这是 Mathematica 中的经典陷阱之一。

MatrixForm 显示包装器的优先级高于 Set 运算符 (=)。

假设(根据您的标签选择)您打算使用矩阵乘法 Dot (.) 而不是 Times (*),我会写

a1 = {{0, -I}, {I, 0}}
a2 = {{0, 1}, {1, 0}}
a3 = {{1, 0}, {0, -1}}
(c = I a1.a2) // MatrixForm
(d = c.a3) // MatrixForm

分别返回 cd

(1  0
 0  -1)

(1  0
 0  1)

编辑:
我忘记提及如果您确实输入

c = I a1.a2 // MatrixForm

然后快速查看 cFullForm 将告诉您问题是什么:

In[6]:= FullForm[c]
Out[6]//FullForm= MatrixForm[List[List[1,0],List[0,-1]]]

您可以看到它有 Head [c] == MatrixForm 因此它不能与其他矩阵很好地配合。

This is one of the classic gotchas in Mathematica.

The MatrixForm display wrapper has a higher precedence than the Set operator (=).

Assuming (based on your tag selection) that you meant to use matrix multiplication Dot (.) instead of Times (*), I would write

a1 = {{0, -I}, {I, 0}}
a2 = {{0, 1}, {1, 0}}
a3 = {{1, 0}, {0, -1}}
(c = I a1.a2) // MatrixForm
(d = c.a3) // MatrixForm

which returns for c and d respectively:

(1  0
 0  -1)

(1  0
 0  1)

Edit:
I forgot to mention if you do enter

c = I a1.a2 // MatrixForm

Then a quick look at the FullForm of c will show you what the problem is:

In[6]:= FullForm[c]
Out[6]//FullForm= MatrixForm[List[List[1,0],List[0,-1]]]

You can see that it has the Head[c] == MatrixForm and so it won't play nice with the other matrices.

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