# Rule can not work? [duplicate]

For example I have this equation,I want to use c->a/b ,but it can not work $$\frac{a^2}{b^2}+\frac{b^2}{a^2}+\frac{a}{b}+e^{a/b}+\frac{b}{a}+\log \left(\frac{a}{b}\right)$$

rule = {a/b -> c};
eq = 1/(a/b)^2 + (a/b)^2 + 1/(a/b) + a/b + Exp[a/b] + Log[a/b];
eq /. rule


Let us write down the same rule in other formula.

rule ={a -> c*b};eq = 1/(a/b)^2 + (a/b)^2 + 1/(a/b) + a/b + Exp[a/b] + Log[a/b];eq /. rule


1/c^2 + 1/c + c + c^2 + E^c + Log[c]

• Don't quite understand why you would do that? Commented Dec 8, 2022 at 8:39
• @我心永恒 it is easier to replace one Symbol than an expression that can be manipulated like a/b. Case in point, Mathematica doesn't see a^2 / b^2 as (a/b)^2 for structural replacements as done with /. Commented Dec 8, 2022 at 8:48
• @b3m2a1 Sometimes my expressions get complicated what do I do? Commented Dec 8, 2022 at 8:49
• @我心永恒 the best thing is to rewrite your rules so they are replacements of a variable by e.g. solving for a in c = a/b. If that fails you might be able to add rules to Simplify using some of the techniques discussed on the site Commented Dec 8, 2022 at 8:51
• @b3m2a1 Maybe it has to be this way Commented Dec 8, 2022 at 8:53
• We need to use Hold to prevent MMA evaluate the eq.
rule = {a/b -> c};
Hold[1/(a/b)^2 + (a/b)^2 + 1/(a/b) + a/b + Exp[a/b] + Log[a/b]] /. rule
ReleaseHold[%]


• Or simple using
Block[{a = b*c}, a^2/b^2 + a/b + b/a + b^2/a^2 + E^(a/b) + Log[a/b]]


• I know that rule is replaced by pattern, simple and practical c->a/b is not acceptable, but in many cases it is difficult to find pattern. What is the principle of using hold? I don't understand Commented Dec 8, 2022 at 9:04
• @rhermans Thanks, updated. Commented Dec 8, 2022 at 9:33

To see why Mathematica does what it does, it can be instructive to write out the FullForm of the expressions in question. This tells you how Mathematica is storing these things internally.

FullForm[rule]
(* List[Rule[Times[a,Power[b,-1]],c]] *)


So what Mathematica is doing when it applies rule is replacing all internal instances of Times[a, Power[b, -1]] with c. Notably, it will not replace sub-expressions that do not literally match this expression.

If we look at Fullform[eq], we can see why some of the expressions get replaced and others don't:

FullForm[eq]
(* Plus[
Times[Power[a,2],Power[b,-2]],
Times[a,Power[b,-1]],
Times[Power[a,-1],b],
Times[Power[a,-2],Power[b,2]],
Power[E,Times[a,Power[b,-1]]],
Log[Times[a,Power[b,-1]]]
] *)


From this, you can see that three of the summands in eq actually contain the literal expression Times[a,Power[b,-1]], and so Mathematica replaces those with c. In contrast, the other three summands do not contain this expression and so Mathematica leaves them alone when rule is applied.

In contrast, if you define rule2 = {a -> c*b}, as suggested by user64494, then internally Mathematica views this rule as

List[Rule[a,Times[b,c]]]


which means that it will replace the expression a with Times[b,c] everywhere it occurs. This gets all of the a expressions, even those embedded inside other expressions involving Times, Power, etc. This is why it's general best to write replacement rules with a single symbol on the left-hand side; if you have a more complicated expression on the left-hand side, you're not guaranteed that Mathematica will replace everything you want it to replace.

Alternately, applying Hold to an expression (as suggested by cvgmt prevents Mathematica from doing internal "simplification" and keeps things in the same form you literally entered them:

holdeq = Hold[1/(a/b)^2 + (a/b)^2 + 1/(a/b) + a/b + Exp[a/b] + Log[a/b]];
FullForm[holdeq]

(* Hold[
Plus[
Times[1,Power[Power[Times[a,Power[b,-1]],2],-1]],
Power[Times[a,Power[b,-1]],2],
Times[1,Power[Times[a,Power[b,-1]],-1]],
Times[a,Power[b,-1]],Exp[Times[a,Power[b,-1]]],
Log[Times[a,Power[b,-1]]]
]
] *)


With the Hold present, Mathematica does not apply any simplification rules, leaving the expression Times[a,Power[b,-1]] in its original form; and so when rule is applied, it successfully replaces all of the instances.