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).

  • $\begingroup$ Does this link help? $\endgroup$ – Sjoerd C. de Vries Oct 17 '15 at 21:58
  • 1
    $\begingroup$ 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. $\endgroup$ – march Oct 17 '15 at 22:01
  • $\begingroup$ @SjoerdC.deVries, thank you, I'll need some time to go through that. $\endgroup$ – Ennar Oct 17 '15 at 22:03
  • $\begingroup$ @march, thank you. I already knew that NonCommutativeMultiply is associative, but I didn't know about Flat Attribute. $\endgroup$ – Ennar Oct 17 '15 at 22:05

Have you tried to extend the definition of NonCommutativeMultiply?

x_ ** 1 := x
1 ** x_ := x
x_ ** Power[x_, -1] := 1
Power[x_, -1] ** x_ := 1

After evaluating the above code

y ** 1


1 ** y


y ** (1/y)


(1/y) ** y


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.