4

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?

Peeter Joot
  • 7,848
  • 7
  • 48
  • 82

2 Answers2

5
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)
Simon
  • 14,631
  • 4
  • 41
  • 101
Nasser
  • 12,849
  • 6
  • 52
  • 104
  • You beat me by "---" that much! (Also, there is [no need to sign your posts in SO](http://meta.stackexchange.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts), I've removed your signature.) – Simon Jun 15 '11 at 02:33
5

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.

Simon
  • 14,631
  • 4
  • 41
  • 101
  • 3
    @Peeter The `FullForm` importance can't be emphasized enough. You will find yourself using it whenever you need to understand any unexpected result – Dr. belisarius Jun 15 '11 at 03:08
  • 2
    @belisarius: Except in the version 8 `Graph` objects. Where `FullForm` acts more like python's `__repr__(self)` method, it gives you an output which will let you recreate the object. :( – Simon Jun 15 '11 at 03:25
  • @Simon Yeah. If WRI is going that way, many tasks will become tough :) – Dr. belisarius Jun 15 '11 at 03:29
  • "output which will let you recreate the object" -- I seem to recall WReach (I think) writing even that is not true, as it can/does give output that is not valid input. – Mr.Wizard Jun 15 '11 at 07:26
  • @Mr.Wizard: You mean [this answer](http://stackoverflow.com/questions/4301833/new-graph-in-mathematica-8-0/4356488#4356488)? Anyway, this probably isn't the right place to have a [whinge about the new `Graph` objects](http://stackoverflow.com/questions/5485405/multigraphs-in-mathematica-8)! – Simon Jun 15 '11 at 07:45
  • Yes, but I will be glad to delete my comment. – Mr.Wizard Jun 15 '11 at 07:49
  • @Mr.Wizard: It corrects/clarifies my statement, so I have no problem with it. – Simon Jun 15 '11 at 07:55