# How to write or extend operators like $*\!\!*$

I'm stuck on trying to define simplification rules for NonCommutativeMultiply, much like ExpandNCM found in Documentation Center, under Applications.

For example, I'd like 1**x = x, x**1 = x, x**(1/x) = (1/x)**x = 1, i.e., I'd like to write rules that would mimic those of multiplication in non-commutative unital ring.

I'm not looking for code (although I wouldn't object if someone would provide one), but for reference where I can learn to write such things myself. My knowledge of Mathematica is basic, I know how to write simple functions, and would like to learn how to write functions using pattern matching.

More generally, I'm interested to know how one could write operation like NonCommutativeMultiply from scratch, that is how could one mimic associativity: (x**y)**z == x**(y**z).

• Does this link help? – Sjoerd C. de Vries Oct 17 '15 at 21:58
• As far as mimicking associativity, NonCommutativeMultiply already has the attribute Flat, which is (I think) Mathematica's way of mimicking associativity. In the documentation for Flat: f[f[a, b], f[c, f[d, e]]] automatically becomes f[a, b, c, d, e] if f has the Flat Attribute. – march Oct 17 '15 at 22:01
• @SjoerdC.deVries, thank you, I'll need some time to go through that. – Ennar Oct 17 '15 at 22:03
• @march, thank you. I already knew that NonCommutativeMultiply is associative, but I didn't know about Flat Attribute. – Ennar Oct 17 '15 at 22:05

Have you tried to extend the definition of NonCommutativeMultiply?

Unprotect[NonCommutativeMultiply];
x_ ** 1 := x
1 ** x_ := x
x_ ** Power[x_, -1] := 1
Power[x_, -1] ** x_ := 1
Protect[NonCommutativeMultiply];


After evaluating the above code

y ** 1


y

1 ** y


y

y ** (1/y)


1

(1/y) ** y


1

• Thank you, this is exactly what I was hoping for. I did try this, but wasn't aware of Unprotect. – Ennar Oct 19 '15 at 4:41