Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
add comment

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.