0
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ I think that's it: Can I simplify an expression into form which uses my own definitions, do you agree? $\endgroup$
    – Kuba
    Commented Jul 5, 2017 at 10:38
  • $\begingroup$ @Kuba. Thanks, maybe. I will have to look at it closely. I'd say it was not the solution I was seeking, but might still be a (or the) solution to my problem. I have to check whether it is sufficient for my real problem (the simple a, b problem was a simplified example of what I want to do. I want to do multiple complex replacements that would be easiest to do if I can do them on something written as an expanded product without powers.) $\endgroup$
    – Kvothe
    Commented Jul 5, 2017 at 12:06
  • $\begingroup$ See links in Mr.Wizard's answer in that topic. $\endgroup$
    – Kuba
    Commented Jul 5, 2017 at 12:06
  • $\begingroup$ @ Kuba, yes thanks, I've looked at those too and they are all about a similar approach to the problem that might be sufficient if adapted. However, I think my question of whether it is possible to expand Powers as products (temporarily turning off the fact that MMa will try to evaluate back to its power form) still stands, even though there are other ways to get around the problem. I guess that the example I gave was also to simplified for what I really want to do. I'll update it. ... (continues) $\endgroup$
    – Kvothe
    Commented Jul 5, 2017 at 12:25
  • 1
    $\begingroup$ You might want to use the fact that the default second argument of Power is 1. Then you could do e.g. a b^2 /. a^(p1_.) b^(p2_.) :> a^(p1 - 1) b^(p2 - 1) t. $\endgroup$
    – jjc385
    Commented Jul 5, 2017 at 16:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.