Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I work with traces of long matrix products and I want mathematica to employ the cyclicity when simplifying.

I use this redefinition of the non-commutative multiplication:


(*Factor out numerics-- could generalize to some ScalarQ*)
nc : NonCommutativeMultiply[a__] /; MemberQ[{a}, _?NumericQ] := 
 NCMFactorNumericQ[NCM[a]] /. NCM -> NonCommutativeMultiply

(*Simplify Powers*)    
b___ ** a_^n_. ** a_^m_. ** c___ := 
 NCM[b, a^(n + m), c] /. NCM -> NonCommutativeMultiply

NonCommutativeMultiply[a___, b_Times, c___] := 
 NCMFactorNumericQ[NCM[a, Sequence @@ b, c]] /. 
  NCM -> NonCommutativeMultiply

NCMFactorNumericQ[nc_NCM] := 
 With[{pos = Position[nc, _?NumericQ, 1]}, 
  Times @@ Extract[nc, pos] Delete[nc, pos]]
NCM[a_] := a
NCM[] := 1

which was given as an answer to this question and is very helpful, but I want it to be able to cancel A**B**C with -B**C**A (but only if these matrices stand alone, i.e. I don't want it to cancel A**B**C**D with -B**C**A**D). Any help would be greatly appreciated.

share|improve this question
Beware! By default, ** is associative, but one implication of the last statement is that your multiplication is not. Thus, even an expression like A**B**C is not well-defined, because it could mean either (A**B)**C or A**(B**C). You probably should not be thinking of your manipulations with traces as resulting from a binary operation at all. – whuber Dec 13 '12 at 15:25
Good point, @whuber . I was thinking something along the lines of letting it check if what is left and right is a one (then it can cycle) or something else (then it can't). On the other hand, I don't really need it to be encoded in the multiplication itself. I'd be perfectly happy with a newly defined trace that I can apply on my expressions and that knows that cyclic permutations are identical. – Sebastik Dec 13 '12 at 16:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.