Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

product of three matrices ends up an odd block matrix?

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?

like image 391
Peeter Joot Avatar asked Dec 27 '22 19:12

Peeter Joot


2 Answers

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)
like image 108
Nasser Avatar answered Jan 09 '23 09:01

Nasser


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.

like image 22
Simon Avatar answered Jan 09 '23 10:01

Simon