4

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

enter image description here

xzczd
  • 65,995
  • 9
  • 163
  • 468
我心永恒
  • 1,488
  • 6
  • 9
  • Oh, here's a properer possible duplicate: https://mathematica.stackexchange.com/q/3822/1871 – xzczd Dec 08 '22 at 12:25
  • Since you can read Chinese, check this also: http://note.youdao.com/noteshare?id=77b86cb08aaf29ce8747990c9e717298 – xzczd Dec 08 '22 at 12:31

3 Answers3

4

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]

Similar questions were asked and answered here many,many times.

user64494
  • 26,149
  • 4
  • 27
  • 56
  • Don't quite understand why you would do that? – 我心永恒 Dec 08 '22 at 08:39
  • 1
    @我心永恒 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 /. – b3m2a1 Dec 08 '22 at 08:48
  • @b3m2a1 Sometimes my expressions get complicated what do I do? – 我心永恒 Dec 08 '22 at 08:49
  • 2
    @我心永恒 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 – b3m2a1 Dec 08 '22 at 08:51
  • @b3m2a1 Maybe it has to be this way – 我心永恒 Dec 08 '22 at 08:53
  • @我心永恒 there are probably lots of methods but you can consider for example the links provided as duplicate or this post or the linked post in the first answer of that last link. Basically, /. expression1->expression2 uses pattern matching and a looot can go wrong with pattern matching in expressions. If you want to substitute something complicated by something else, using equation solving is more robust/safe/reliable. – userrandrand Dec 08 '22 at 18:23
4
  • 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[%]

enter image description here

  • 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]]

enter image description here

cvgmt
  • 72,231
  • 4
  • 75
  • 133
  • 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 – 我心永恒 Dec 08 '22 at 09:04
  • @rhermans Thanks, updated. – cvgmt Dec 08 '22 at 09:33
4

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.

Michael Seifert
  • 15,208
  • 31
  • 68