Applying Transformation Rules

Applying transformation rules.

The replacement operator /. (pronounced "slash-dot") applies rules to expressions:

x+y/.x->3

You can give a list of rules to apply. Each rule will be tried once on each part of the expression:

x+y/.{x->a,y->b}

Applying lists of transformation rules.

If you give a list of lists of rules, you get a list of results:

x+y/.{{x->1,y->2},{x->4,y->2}}

Functions such as (RefLink[Solve, paclet:ref/Solve]) and (RefLink[NSolve, paclet:ref/NSolve]) return lists whose elements are lists of rules, each representing a solution:

Solve[x^3-5x^2+2x+8==0,x]

When you apply these rules, you get a list of results, one corresponding to each solution:

x^2+6/.%

When you use expr/.rules, each rule is tried in turn on each part of expr. As soon as a rule applies, the appropriate transformation is made, and the resulting part is returned.

The rule for x^3 is tried first; if it does not apply, the rule for x^n_ is used:

{x^2,x^3,x^4}/.{x^3->u,x^n_->p[n]}

A result is returned as soon as the rule has been applied, so the inner instance of h is not replaced:

h[x+h[y]]/.h[u_]->u^2

The replacement expr/.rules tries each rule just once on each part of expr.

Since each rule is tried just once, this serves to swap x and y:

{x^2,y^3}/.{x->y,y->x}

You can use this notation to apply one set of rules, followed by another:

x^2/.x->(1+y)/.y->b

Sometimes you may need to go on applying rules over and over again, until the expression you are working on no longer changes. You can do this using the repeated replacement operation expr//.rules (or (RefLink[ReplaceRepeated, paclet:ref/ReplaceRepeated])[expr,rules]).

Single and repeated rule application.

With the single replacement operator /. each rule is tried only once on each part of the expression:

x^2+y^6/.{x->2+a,a->3}

With the repeated replacement operator //. the rules are tried repeatedly until the expression no longer changes:

x^2+y^6//.{x->2+a,a->3}

Here the rule is applied only once:

log[a b c d]/.log[x_ y_]->log[x]+log[y]

With the repeated replacement operator, the rule is applied repeatedly, until the result no longer changes:

log[a b c d]//.log[x_ y_]->log[x]+log[y]

When you use //. (pronounced "slash-slash-dot"), the Wolfram Language repeatedly passes through your expression, trying each of the rules given. It goes on doing this until it gets the same result on two successive passes.

If you give a set of rules that is circular, then //. can keep on getting different results forever. In practice, the maximum number of passes that //. makes on a particular expression is determined by the setting for the option (RefLink[MaxIterations, paclet:ref/MaxIterations]). If you want to keep going for as long as possible, you can use (RefLink[ReplaceRepeated, paclet:ref/ReplaceRepeated])[expr,rules,(RefLink[MaxIterations, paclet:ref/MaxIterations])->(RefLink[Infinity, paclet:ref/Infinity])]. You can always stop by explicitly interrupting the Wolfram Language.

By setting the option (RefLink[MaxIterations, paclet:ref/MaxIterations]), you can explicitly tell (RefLink[ReplaceRepeated, paclet:ref/ReplaceRepeated]) how many times to try the rules you give:

ReplaceRepeated[x,x->x+1,MaxIterations->1000]

The replacement operators /. and //. share the feature that they try each rule on every subpart of your expression. On the other hand, (RefLink[Replace, paclet:ref/Replace])[expr,rules] tries the rules only on the whole of expr, and not on any of its subparts.

You can use (RefLink[Replace, paclet:ref/Replace]), together with functions like (RefLink[Map, paclet:ref/Map]) and (RefLink[MapAt, paclet:ref/MapAt]), to control exactly which parts of an expression a replacement is applied to. Remember that you can use the function (RefLink[ReplacePart, paclet:ref/ReplacePart])[expr,new,pos] to replace part of an expression with a specific object.

The operator /. applies rules to all subparts of an expression:

x^2/.x->a

Without a level specification, (RefLink[Replace, paclet:ref/Replace]) applies rules only to the whole expression:

Replace[x^2,x^2->b]

No replacement is done here:

Replace[x^2,x->a]

This applies rules down to level 2, and so replaces x:

Replace[x^2,x->a,2]

Applying rules to whole expressions.

(RefLink[Replace, paclet:ref/Replace]) returns the result from using the first rule that applies:

Replace[f[u],{f[x_]->x^2,f[x_]->x^3}]

(RefLink[ReplaceList, paclet:ref/ReplaceList]) gives a list of the results from every rule that applies:

ReplaceList[f[u],{f[x_]->x^2,f[x_]->x^3}]

If a single rule can be applied in several ways, (RefLink[ReplaceList, paclet:ref/ReplaceList]) gives a list of all the results:

ReplaceList[a+b+c,x_+y_->g[x,y]]

This gives a list of ways of breaking the original list in two:

ReplaceList[{a,b,c,d},{x__,y__}->g[{x},{y}]]

This finds all sublists that are flanked by the same element:

ReplaceList[{a,b,c,a,d,b,d},{___,x_,y__,x_,___}->g[x,{y}]]

Applying rules in one way or all possible ways.