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: Substitute and ReplaceAll:
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
Nothing special, Substitute and 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 Times:
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?
rule1changed fromNoncommutativeMultiplytoTimesI would guess that the symbolbis leaking somewhere in such a way thatrule1was seen as scalar-(nc-)times-something` and that allowed the type of product to change to ordinaryTimes. But that's just a vague guess and this is something the package authors would need to investigate. $\endgroup$NCAlgebraor a consequence of external factors. $\endgroup$NCAlgebrabut I can say that testing the parts that do not rely on that package gave different results from what you showed. $\endgroup$bugstag because the bug appears to be in the package and not Mathematica itself. $\endgroup$