2
$\begingroup$

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(?)

$\endgroup$
4
  • 1
    $\begingroup$ 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. $\endgroup$
    – eyorble
    Commented Dec 19, 2020 at 22:29
  • $\begingroup$ thanks. So how do I change that? $\endgroup$
    – geom
    Commented Dec 19, 2020 at 22:40
  • 1
    $\begingroup$ Evaluate[NCM[...]], or just avoid using the abbreviation in the first place. ... /: id ** x_ and ... /: y_ ** id should also work. $\endgroup$
    – eyorble
    Commented Dec 19, 2020 at 22:43
  • $\begingroup$ @eyorble Please post your helpful comments as an answer so that I vote it $\endgroup$
    – geom
    Commented Dec 19, 2020 at 22:48

1 Answer 1

4
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thanks for the detailed answer. I found NCM[x___] := NonCommutativeMultiply[x] here but I noticed that it can produce problems, hence my question $\endgroup$
    – geom
    Commented Dec 19, 2020 at 23:12

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.