2
$\begingroup$

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? enter image description here 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}} );
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Nasser
    Jan 28 at 11:20
  • $\begingroup$ Thank you, I added the code... do you have any idea? $\endgroup$
    – Dac0
    Jan 28 at 11:27

1 Answer 1

3
$\begingroup$

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

Mathematica graphics

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

Mathematica graphics

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]

Mathematica graphics

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.