       Re: Replacements in NonCommutativeMultiply

• To: mathgroup at smc.vnet.net
• Subject: [mg72569] Re: [mg72558] Replacements in NonCommutativeMultiply
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Tue, 9 Jan 2007 07:53:31 -0500 (EST)
• References: <200701081000.FAA14451@smc.vnet.net>

```This is a consequence of the attributes of NonCommutativeMultiply:

Attributes[NonCommutativeMultiply]

{Flat,OneIdentity,Protected}

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

Times[a,x,NonCommutativeMultiply[x,i,c]]

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

Times[a,x,NonCommutativeMultiply[i,c]]

or, using 1 in place of x:

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

Times[a,NonCommutativeMultiply[i,c]]

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