ReplaceAll, ReplaceRepeated

The next cell demonstrates the basic use of ReplaceAll.

Clear[x, y] ;  expr = y + Sin[x] ;  expr/.y4

4 + Sin[x]

Evaluation of the previous cell doesn't change the value of (y) or (expr).

{y, expr}

{y, y + Sin[x]}

If we provide a list of rules or delayed rules on the right side of  ReplaceAll several replacements are made.  The difference between  (lhs→rhs) and (lhs:→rhs) is covered in the section on Rule instead of RuleDelayed.

expr/.{xπ/4, y5}

5 + 1/2^(1/2)

ReplaceAll considers a list of rules starting from the left.  In the next
cell the rule (y→5) is used and the symbol (y) is no longer in the
expression.  As a result the rules (y→3) and (y→4) are not

expr/.{y5, y3, y4}

5 + Sin[x]

If we provide a list of lists of replacement rules on the right side of
ReplaceAll we get a list of results from making several sets of replacements.

expr/.{{x1, y2}, {x5, y6}, {x12, y13}}

{2 + Sin[1], 6 + Sin[5], 13 + Sin[12]}

Any combination of pattern matching constructs can be used on the left  side of rules.  The next cell gives a non-trivial example.  Nuances of pattern matching are discussed in another section.

 x^(1/2) + 4 z^2 + z^3 - Log[x]/y^2/.c_. * s_Symbol^n_Integerc * n * s^(n - 1)

x^(1/2) + 8 z + 3 z^2 + (2 Log[x])/y^3

Once ReplaceAll changes a part of an expression no further replacements are
made on that portion of the expression or any of it's subparts.  For example
the rule in the next cell is used once on each logrithm.

Log[d p q r] Log[a b c d]/.Log[x_  y_] Log[x] + Log[y]

(Log[a] + Log[b c d]) (Log[d] + Log[p q r])

ReplaceRepeated works just like ReplaceAll except it continues trying the
rules until the result no longer changes.  In the next cell ReplaceRepeated
fully expands each logrithm.

Log[d p q r] Log[a b c d]//.Log[x_ y_] Log[x] + Log[y]

(Log[a] + Log[b] + Log[c] + Log[d]) (Log[d] + Log[p] + Log[q] + Log[r])

The order of matching patterns and making replacements

If we put a Print statment at the right place we can see the order that the
pattern matcher considers different parts of an expression for replacement.  
Notice the Print statement never returns True, so nothing can match the
pattern, but each pattern considered is printed.

Clear["Global`*"] ;  h[f1[a1], f2[e][a2]]/.(a_/;Print[a] 0)

h[f1[a1], f2[e][a2]]










h[f1[a1], f2[e][a2]]

Since ReplaceAll starts making replacements at the highest level, Log[x] in
the next cell is changed to Log[x+1].

 x^(1/2) + 2x^3 - Log[x]/y/.{xa x, Log[x] Log[x + 1]}

2 a^3 x^3 + (a x)^(1/2) - Log[1 + x]/y

Replace makes replacements starting at the deepest level, so in the next cell  Log[x] is changed to Log[a x].

 Replace[x^(1/2) + 2x^3 - Log[x]/y, {xa x, Log[x] Log[x + 1]}, {0, ∞}]

2 a^3 x^3 + (a x)^(1/2) - Log[a x]/y

In the next cell we can see (expr/.rules) evaluates (expr), then it  evaluates (rules), it makes the replacements without evaluating the new  results.  A new expression is returned (2^2 + 7) in this case, and this new expression is allowed to evaluate to 11.

expr = x + y ; rules = {x2^2, y7} ;  Trace[expr/.rules]

{{expr, x + y}, {rules, {x2^2, y7}}, x + y/. {x2^2, y7}, 2^2 + 7, {2^2, 4}, 4 + 7, 11}

In the next cell we can see (expr//.rules) evaluates (expr), then it  evaluates (rules).  The first replacement made is  Log[^5 a b] ⟶ (Log[^5] + Log[a b] )  and Log[^5] evaluates to 5.  Then the replacement rule is used again to change Log[a  b] to  (Log[a]+Log[b] ).

expr = Log[^5a b] Log[c d] ; rules = {Log[x_  y_] Log[x] + Log[y]} ;  Trace[expr//.rules]

{{expr, Log[c d] Log[a b ^5]}, {rules, {Log[x_ y_] Log[x] + Log[y]}}, Log[c d] ... g[a] + Log[b]), (5 + Log[a] + Log[b]) (Log[c] + Log[d])}, (5 + Log[a] + Log[b]) (Log[c] + Log[d])}

Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page