In course of my work with
NCAlgebra package for Mathematica, http://math.ucsd.edu/~ncalg/ , I encountered unexpected behavior of replacements. I am using Mathematica 8 with compatible version of NCAlgebra, 4.0.4.
I would like to rule out the possibility of botched installation of NCAlgebra, although on my computer it seems to pass all in-built tests.
I define two very similar transformation rules and an expression to use them on:
<<NC` <<NCAlgebra` rule1 = b_ ** a_ :> -a ** b; rule2 = HoldPattern[b_ ** a_] :> -a ** b; expr = -x ** x ** y - y ** x;
Now I apply these rules using two different methods:
expr /. rule1 SubstituteSingleReplace[expr, rule1] expr /. rule2 SubstituteSingleReplace[expr, rule2] (*result*) x ** y - x ** x ** y x ** y + x ** x ** y x ** y - x ** x ** y x ** y - x ** x ** y
ReplaceAll are expected to work a little differently.
b=2; rule1 expr /. rule1 SubstituteSingleReplace[expr, rule1] expr /. rule2 SubstituteSingleReplace[expr, rule2]
Now things become strange.
rule1 now looks differently - non-commutative multiplication
** is replaced by commutative product
b_ a_ :> -a ** b
The results of replacements look really weird (note that the change in result of
ReplaceAll is likely due to change of the rule)
y ** x + x ** x ** y -2 y ** x - 2 x ** x ** y x ** y - x ** x ** y 2 x - x ** x ** y
Are the inner workings of the pattern are affected by the definitions of global variables? I thought previosly that this behavior can be negated by using
HoldPattern[_], but with this example it is proved ineffective.
What can be done to fix this?