2

if I define id as:

id /: NonCommutativeMultiply[id, x_] := x
id /: NonCommutativeMultiply[y_, id] := y

then id ** a - a ** id gives 0. However if:

NCM[x___] := NonCommutativeMultiply[x];
id /: NCM[id, x_] := x
id /: NCM[y_, id] := y

then id ** a - a ** id gives -a ** id + id ** a and not 0

I am confused as to why this happens(?)

geom
  • 668
  • 3
  • 13
  • 1
    Attributes[TagSet] includes HoldAll, so NCM is not expanded during the definition of the pattern and NonCommutativeMultiply (which you have with **) does not match NCM. – eyorble Dec 19 '20 at 22:29
  • thanks. So how do I change that? – geom Dec 19 '20 at 22:40
  • 1
    Evaluate[NCM[...]], or just avoid using the abbreviation in the first place. ... /: id ** x_ and ... /: y_ ** id should also work. – eyorble Dec 19 '20 at 22:43
  • @eyorble Please post your helpful comments as an answer so that I vote it – geom Dec 19 '20 at 22:48

1 Answers1

4

Attributes[TagSet] includes HoldAll, so NCM is not expanded during the definition of the pattern and NonCommutativeMultiply (which you have with **) does not match NCM.

To avoid this, you can either expand NCM explicitly in the pattern by using Evaluate:

id /: Evaluate@NCM[id, x_] := x
id /: Evaluate@NCM[y_, id] := y

Or by avoiding the abbreviation entirely:

id /: id ** x_ := x
id /: y_ ** id := y

Alternatively, you can theoretically Unprotect[TagSet] and ClearAttributes[TagSet,HoldAll]. I am not aware of how often TagSet is relied upon internally, but I am quite positive that this will have unintended and likely unwanted side effects, so I can't recommend it.

eyorble
  • 9,383
  • 1
  • 23
  • 37
  • Thanks for the detailed answer. I found NCM[x___] := NonCommutativeMultiply[x] here but I noticed that it can produce problems, hence my question – geom Dec 19 '20 at 23:12