Non Commutative Multiply


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


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

How could I solve this problem?

Thanks in advance


Posted 2012-11-07T12:57:14.333

Reputation: 11

Question was closed 2012-11-07T15:08:51.033

3Look up Inner[] and NonCommutativeMultiply[]. – J. M.'s ennui – 2012-11-07T13:01:40.773

more precisely Inner[NonCommutativeMultiply, A, B] – chris – 2012-11-07T13:13:38.057

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. – jVincent – 2012-11-07T13:14:53.887


This answer 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.

– jVincent – 2012-11-07T13:33:34.303

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 – chris – 2012-11-07T14:21:43.680

Thanks for comments! I already tried using NonCommutativeMultiply and Inner, but sometimes I will need to multiply three matrices, one of it possible having scalar entries. Then, I believe the best thing to me is try to define a Times2 function. I was not able to this yet, but I'm trying. thanks! – rodrigo – 2012-11-08T12:34:52.287

No answers