This is most easily explained with an example:
Say I have a b^2
or a b a
and I want to replace a b
with something.
Of course a b^2 /. a b -> t
fails. Because the pattern does not match due to the fact that we have powers here.
There are three solutions here. We can Hold the a b a
and act on that or we can do something as proposed here Write Integer powers as products
or we can adjust our pattern to something involving powers.
All these solutions are unsatisfying for my purpose. As the real thing I want to do will 1) Already have evaluated to the power form since it comes from a computation. 2) Using the second method we get something ugly on which it still isn't easy to act with replacements. See for instance how
(a b a /. Power[x_, y_] :> Inactive[Times] @@ Table[x, {y}]) /.
a b -> t
fails. 3) The third solution might seem like the best solution (and it is the one I will use if no other solution is available) but it can get very complicated to get the right patterns for more involved expressions.
And it seems that this should be an easy thing for mathematica to do. Can we temporarily turn off the pattern that rewrites into powers? (To turn it back on again once we are done with replacements.)
The actual problem is slightly more complex:
I have expressions such as X[a]X[a]Y[a] Z[a] K[b] L[b]
. I want to group together all expressions with the same argument and get something like f[X,X,Y,Z] f[b,b]
. Things can also be more complicated as I also have expressions such as X[a]X[a]Y[b]delta[a,b]Z[c]Q[c]
that should return f[X,X,Y] f[Z,Q]
. One approach to this is to explicitly write these replacement rules for expressions up to a certain length (so i.e.
{X_[a_]Y_[a_]Z_[a_]->f[X,Y,Z], etc...}
(ordered from largest expressions to smallest and then using replace repeated) when the expressions were small this seemed like a good idea, now I don't think so anymore and I should be working on generating them up to a certain level n). I was annoyed by the fact that I now also have to treat powers as a special case (so I need to include pattern searches for X_^n_
for every object). If someone has another better approach to this problem I'd be glad to hear about that too.
Power
is 1. Then you could do e.g.a b^2 /. a^(p1_.) b^(p2_.) :> a^(p1 - 1) b^(p2 - 1) t
. $\endgroup$