1
$\begingroup$

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

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

1 Answer 1

4
$\begingroup$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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