I'm trying to redefine NonCommutativeMultiply (NCM) to behave like an anticommuting operator, based on the question here. This involves calling Unprotect on NCM and adding a definition that leaves a**b**c as-is, but changes a**c**b to -a**b**c. The simplifications all work fine, but after doing all this MatchQ behaves a bit weirdly, returning False when I think it should return true. If I call Block[{NonCommutativeMultiply},MatchQ[...]] then the pattern does indeed match! This solution works, but I'd really like to know what's going on here. Why is my MatchQ returning False?

Minimal example:

(* Before adding the canonical ordering definition, this expression is matched *)
(* Out[] := True*)

(* Add a canonical ordering definition (leave a**z as a**z, but change z**a to -a**z) *)

(* After defining canonical ordering, the expression isn't matched any more! This is the problem line. *)
(* Out[] := False *)

(* Oddly enough, if s[1] is replaced with a simpler variable, we again match *)
(* Out[] := True *)

(* It can also be fixed with Block. *)
(* Out[] := True *)

It should be noted that s[1]**s[2]**s[3] is not changed or reordered by the rule I've added.

  • 3
    $\begingroup$ The issue is that nothing stops your definition being applied when an object like d___ appears in NonCommutativeMultiply. You can combat this with HoldPattern. $\endgroup$
    – jjc385
    Commented Mar 15, 2018 at 0:51

1 Answer 1


First, a couple comments:

  1. There is no need to use Print.
  2. There is no need to remove the Flat attribute

Next, as @jjc385 comments, the problem is that your pattern evaluates:

NonCommutativeMultiply[s[1], d___]

-d___ ** s[1]

Clearly, NonCommutativeMultiply[s[1], s[2], s[3]] will not match this pattern. To avoid this evaluation, you can use HoldPattern:



Note that I don't use Print.

By the way, you might try the following alternative instead:

NonCommutativeMultiply[a__] /; !OrderedQ[{a}] := Signature[{a}] NonCommutativeMultiply @@ Sort[{a}]

Your Answer

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