# Simplifying a b = 1 in non-commutative products?

Yesterday, I try to define some rules for non-communicative multiplication, here. I find some quite helpful rules, but things still do not work properly; the distributive rule can't be applied (e.g.: -a**(b + c) = -a**b - a**c), and the identity is not recongnized (e.g.: 1**a should be equal to a). Then I try to solve all these problem by removing the attribute Orderless from the built-in operator Times.

Any way, then I have some expression as (some term omitted):

ClearAttributes[Times, Orderless]
fab = a b + a b c + c a b + a^2 b^2 + a^3 b c + c a b^3


(It should be noted that a b is not equal to b a in my case).

I want to apply the relation a b = 1 and b a = 1 with the following rule:

rule ={a b -> 1, b a -> 1, a^i_ b^i_ -> 1 b^i_ a^i_ -> 1}


Sadly, It seems that the above rule doesn't work.

• The notebbook available here has a section covering various ways to work with noncommutative products, defining commutators, canonicalizing, etc. Commented Oct 5, 2014 at 17:47

First, you really don't want to modify Times, as this will have all sorts of unforeseen side-effects. Your best route will be to use ** (NonCommutativeMultiply), for which you'll have to write rules that enforce the behaviour you want.

Here's how you might go about that:

genericRules = {
(* Move numeric factors outside of NonCommutativeMultiply. *)
before___ ** (a_?NumericQ*x_) ** after___ :> a  before ** x ** after,
before___ ** a_?NumericQ ** after___ :> a  before ** after,
(* Distribute over Plus. *)
before___ ** sum_Plus ** after___ :> (before ** # ** after & /@ sum),
(* Remove singletons. *)
NonCommutativeMultiply[x_] :> x
};

specificRules = {
a ** b -> 1,
b ** a -> 1
};

ApplyRules[expr_] := expr //. Join[specificRules, genericRules]


Then you can simplify expressions as follows:

fab = a ** b + a ** b ** c + c ** a ** b + a ** a ** b ** b
+ a ** a ** a ** b ** c + c ** a ** b ** b ** b;

fab // ApplyRules

2 + 2 c + a ** a ** c + c ** b ** b

• what's the function of NonCommutativeMultiply[x_] :> x Commented Oct 4, 2014 at 12:51
• @vanabel That's for the last step in this simplification sequence: a ** b ** c -> 1 ** c -> NonCommutativeMultiply[c] -> c. This is similar to what happens for Times, compare namely the following: Times[1, x] -> Times[x] -> x. Commented Oct 4, 2014 at 15:02
• @van By the way to automatically apply this set of rules to output you can set \$Post = ApplyRules. Commented Oct 4, 2014 at 16:47
• @TeakeNutma It seems that it is not: when I modify the code as:genericRules = { NonCommutativeMultiply[x_] :> x };, then 1**c//ApplyRules output 1**c rather than c. Commented Oct 5, 2014 at 0:56
• @TeakeNutma May I invite you to take a look at my question? Commented Oct 5, 2014 at 2:10