1
$\begingroup$

Possible Duplicate:
Non-commutative symbolic linear algebra

I want to multiply two matrices, for example,

A = {{e, f}, {g, h}}
B = {{a, b}, {c, d}}

Using A.B, Mathematica returns

{{a e + b g, a f + b h}, {c e + d g, c f + d h}}

I would like to get, however, the following result:

{{e a + f c, e b + f d}, {g a + h c, g b + h d}}

since, for me, the entries {a,b,c,d,e,f,g,h} are operators, i.e. they are non-commutative.

I could solve this problem clearing the attribute Orderless in the built-in function Times:

ClearAttributes[Times, Orderless]

I know, however, this can be dangerous. I tried to define a function

Times2[a_,b_]:=Times[a,b]

and then use ClearAttributes[Times2, Orderless] but it doesn't work.

How could I solve this problem?

Thanks in advance

$\endgroup$
  • 3
    $\begingroup$ Look up Inner[] and NonCommutativeMultiply[]. $\endgroup$ – J. M. will be back soon Nov 7 '12 at 13:01
  • $\begingroup$ more precisely Inner[NonCommutativeMultiply, A, B] $\endgroup$ – chris Nov 7 '12 at 13:13
  • $\begingroup$ The reason Times2[a_,b_]:=Times[a,b] doesn't work when as you expect when Times2 is not orderless, is that it only affects the left hand side. You are just passing onwards to Times which is still orderless. $\endgroup$ – jVincent Nov 7 '12 at 13:14
  • 2
    $\begingroup$ This answer mathematica.stackexchange.com/a/5458/1194 by Leonid Shifrin shows how it's possible to circumvent the orderless property of Times when applied to specific expressions, without having to remove it. $\endgroup$ – jVincent Nov 7 '12 at 13:33
  • $\begingroup$ If you like to live dangerously you can always do Unprotect[Times];ClearAttributes[Times, Orderless];Protect[Times]; A.B Though it is typically ill advised to mess with the behaviour of low level functions like Times $\endgroup$ – chris Nov 7 '12 at 14:21