How to replace implicit subexpressions in Mathematica?

I have this expression in Mathematica:

(a^2 (alpha + beta)^2)/(b^2 + c^2) + (a (alpha + beta))/(b^2 + c^2) + 1  

As you can see, the expression has a couple of subexpressions that repeat throughout.

I want to replace a/(b^2+c^2)with dand alpha+betaby gamma.

The final expression should be:

1+d*gamma+a*d*gamma^2

I have much more complex expressions in which the ability to do this will greatly simplify my work.

I tried to sort this question out, and I only find answers that use FactorTerms and ReplaceRepeated, but do not work consistently and for a more complex expression like this. I hope someone has an answer.

+5
source share
3 answers

- d. , , , . , :

expr  = (a^2 (alpha + beta)^2)/(b^2 + c^2) + (a (alpha + beta))/(b^2 + c^2) + 1

, :

rules = {a/(b^2 + c^2) -> d, alpha + beta -> gamma}

, expr, rules. , , , . , , , Hold. , :

Clear[withExpandedPowers];
withExpandedPowers[expr_, f_: Hold] :=
  Module[{times},
    Apply[f,
       Hold[expr] /. x_^(n_Integer?Positive) :>
          With[{eval = times @@ Table[x, {n}]}, eval /; True] /.
       times -> Times //.
       HoldPattern[Times[left___, Times[middle__], right___]] :>
          Times[left, middle, right]]];

:

In[39]:= withExpandedPowers[expr]
Out[39]= Hold[1+(a (alpha+beta))/(b b+c c)+((alpha+beta) (alpha+beta) a a)/(b b+c c)]

:

In[40]:= 
ReleaseHold[
   withExpandedPowers[expr] //. 
      withExpandedPowers[Map[MapAt[HoldPattern, #, 1] &, rules], Identity]]

Out[40]= 1 + d gamma + a d gamma^2

l.h.s. HoldPattern, .

, - Mathematica, . , .

+4

ReplaceRepeated:

(a^2 (alpha + beta)^2)/(b^2 + c^2) + (a (alpha + beta))/(b^2 + c^2) + 
  1 //. {a/(b^2 + c^2) -> d, alpha + beta -> gamma}

TransformationFunctions:

FullSimplify[(a^2 (alpha + beta)^2)/(b^2 + 
     c^2) + (a (alpha + beta))/(b^2 + c^2) + 1, 
 TransformationFunctions -> {Automatic, # /. 
     a/(b^2 + c^2) -> d &, # /. alpha + beta -> gamma &}]

:

1 + gamma (d + (a^2 gamma)/(b^2 + c^2))
+2

I'm humble --- I'm not a computer scientist --- I think it's easier than all the other solutions offered

1+a(alpha+beta)/(b^2 + c^2) +a^2(alpha+beta)^2/(b^2 + c^2) \\.
{a^2-> a  z, a/(b^2 + c^2)-> d,alpha+\beta -> gamma,z-> a}
0
source

All Articles