You have to treat separately the cases with powers and the cases without powers. This is because patterns match the form of the expression and
Times[Power[a,n],Power[b,n]]
does not contain anywhere the expression Times[a,b]
. I think the following solution might fork for you:
r = {Spaa[1, 2]^n_ Spbb[2, 1]^m_ :> (-Spaa[1, 3] Spbb[3, 1])^# Spaa[1, 2]^(n - #) Spbb[2, 1]^(m - #) &@Min[n, m], Spaa[1,2]Spbb[2,1] -> -Spaa[1,3]Spbb[3,1]};
And then do expr/.r
to any expression expr
to which you want to apply the rule.
The rule you defined in the question, namely
Times[___, Spaa[1, 2], ___, Spbb[2, 1]] -> -Spaa[1, 3] Spbb[3, 1]
(I'm assuming you mistakenly flipped 2 and 3), does not help because Times
has attributes Flat
so all its subsequences are assumed to be wrapped by Times
for the purpose of pattern matching. Let me be clearer: for any function f
so that Attributes[f]
contains {Flat}
this happens
In[1]:= f[a,b,c,d]/.f[b,c]->something
Out[1]:= f[a,something,d]
Moreover it has attribute Orderless
, so the blank in the middle is not needed. Because of these reasons the blanks actually never match anything, if it were otherwise whatever is matched by them would not appear in the r.h.s. (which is not what you wanted I assume).
Spaa[1,2]^2 Spaa[2,3]^2 Spbb[2,1]^3
? $\endgroup$Spaa[1, 2]^2 Spaa[2, 3]^2 Spbb[2, 1]^3 /. Times[a___, Spaa[1, 2]^b_., c___, Spbb[2, 1]^d_.] :> -a Spaa[1, 3]^b c Spbb[3, 1]^d
? $\endgroup$PolynomialReduce
can be used for this.In[268]:= PolynomialReduce[Spaa[1, 2]^2 Spaa[2, 3]^2 Spbb[2, 1]^3, Spaa[1, 2] Spbb[2, 1] - (-Spaa[1, 3] Spbb[3, 1]), {Spaa[1, 2], Spbb[2, 1], Spaa[1, 3], Spbb[3, 1]}][[2]] Out[268]= Spaa[1, 3]^2 Spaa[2, 3]^2 Spbb[2, 1] Spbb[3, 1]^2
$\endgroup$