# Problems with definition of a function with matrices

I want to define the product between two matrices. If I don't define it as function everything is fine (lines at the end of the image), but when I try to define it as a function it gets strange things. What is going on? And how do I solve it? The first one is

Okubo[x03_,y03_] = \[Omega]*(x03.y03) - \[Omega]*\[Omega] (y03.x03) - (\[Omega] - \[Omega]*\[Omega])/3 Tr[x03.y03] IdentityMatrix [3];

Okubo[e, e] // MatrixForm


while the second one (working properly) is

\[Omega]*(e.e) - \[Omega]*\[Omega] (e.e) - (\[Omega] - \[Omega]*\[Omega])/3 Tr[e.e] IdentityMatrix [3]


The lement "e" is the the one on which I'm testing the thing and is

e = ( { {2, 0, 0},{0, -1, 0}, {0, 0, -1}} );

• it will easier to find out why if you post the Mathematica code. From screen shot it should not do that. But without the code hard to try. Jan 28 at 11:20
• Thank you, I added the code... do you have any idea?
– Dac0
Jan 28 at 11:27

Use :=

Clear["Global*"]
e = ({{2, 0, 0}, {0, -1, 0}, {0, 0, -1}});
Okubo[x03_,y03_] := ω*(x03 . y03) - ω*ω (y03.x03) - (ω - ω*ω)/3
Tr[x03.y03] IdentityMatrix[3];

Okubo[e, e] // MatrixForm


(ω*(e . e) - ω*ω (e.e) - (ω - ω*ω)/3 Tr[e . e] IdentityMatrix[3]) // MatrixForm


The reason is, when you used immediate assignment, then Mathematica evaluated the body of the function immediately. This resulted in

Okubo[x03_,y03_] = ω*(x03 . y03) - ω*ω (y03 . x03) - (ω - ω*ω)/3
Tr[x03 . y03] IdentityMatrix[3]


Which is not what you want. Because it did not know that x03 and y03 were matrices. But with delayed assignments, it did the right . operation since the arguments are now known to be matrices.

It is always best to define functions using delayed assignment unless there is specific reason not to.

• didn't seem to work... I copy and pasted... am I missing something? gyazo.com/c3e38f5f3de56aae56b4a949fb8eff99
– Dac0
Jan 28 at 11:47
• @Dac0 did you use := or =` Jan 28 at 11:48
• Now I saw :) Please edit the code in the corrected version with := future leggibility :) Thank you
– Dac0
Jan 28 at 11:50