MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Replacements in NonCommutativeMultiply

  • To: mathgroup at
  • Subject: [mg72569] Re: [mg72558] Replacements in NonCommutativeMultiply
  • From: Andrzej Kozlowski <akoz at>
  • Date: Tue, 9 Jan 2007 07:53:31 -0500 (EST)
  • References: <>

This is a consequence of the attributes of NonCommutativeMultiply:



and the rather unintuitive effect these attributes (or, in this case  
just the attribute Flat) can sometimes have on pattern matching. You  
can check yourself that if you give CircleDot the same attributes:

SetAttributes[CircleDot, Flat]
you will see exactly the same phenomenon as with NonCommutativeMultiply.

This phenomenon has been in the past discussed in detail in various  
places by Allan Hayes so I suggest any one who wants a more complete  
understanding should look at his articles and also posts to the  
MathGroup on this topic. I will just restrict myself to what actually  
happens in this case, without trying to justify it. The situation is,  
in fact, somewhat misleading because 1 is used in the replacement.  
Let's first use something else, e.g. the symbol x:

p = a*NonCommutativeMultiply[]*i**c;

p /. NonCommutativeMultiply[] -> x//FullForm


Now you can see that there are two x's and not just one in the final  
expression. Of course in the case when x is 1 it simply disappears  
from inside of Times in the usual way and that causes the confusion.  
So the question is: why the second x? Well, this is due to one  
particular form of "f-grouping" in Allan Hayes terminology. What  
happens is that the pattern matcher rewrites the expression Times[a,  
NonCommutativeMultiply[], NonCommutativeMultiply[i, c]] in the form  
Times[a, NonCommutativeMultiply[], NonCommutativeMultiply 
[NonCommutativeMultiply[],i, c]]] which leads it to find two matches  
for NonCommutativeMultiply[] rather than one. O.K. so this is all the  
explanation I am willing to provide at this moment. There is of  
course still the question what can be done to stop it. One thing that  
will work is using Verbatim (but not HoldPattern!):

p/. Verbatim[NonCommutativeMultiply[] ]-> x//FullForm


or, using 1 in place of x:

p/. Verbatim[NonCommutativeMultiply[] ]-> 1//FullForm


as was originally wanted.

Andrzej Kozlowski
Oxford, UK

On 8 Jan 2007, at 10:00, David Park wrote:

> Can anyone explain the following result?
> Times[a, NonCommutativeMultiply[], NonCommutativeMultiply[i, c]];
> % /. NonCommutativeMultiply[] -> 1 // FullForm
> giving
> Times[a, NonCommutativeMultiply[1, i, c]]
> I believe the result should be
> Times[a, NonCommutativeMultiply[i, c]]
> The reason I get an empty argument sequence is that the factors  
> contain 'scalars' and 'vectors' and the scalars get factored out.  
> Only the vector part is noncommutative.
> The result Mathematica gives is definitely a problem because when  
> the 1 is factored out again to simplify we arrive back at the  
> starting point.
> A similar replacement is done with CircleDot works properly.
> Times[a, CircleDot[], CircleDot[i, c]];
> % /. CircleDot[] -> 1 // FullForm
> Times[a, CircleDot[i, c]]
> David Park
> djmp at

  • Prev by Date: Re: question on Plot and Show plus Axis Labels:
  • Next by Date: NDSolve EventLocator Method question about the Event option
  • Previous by thread: Re: Replacements in NonCommutativeMultiply
  • Next by thread: what could cause a function "call" to not print during TracePrint?