The Mathematica Journal
Feature Articles
Tricks of the Trade
In and Out
New Products
New Publications
News Bulletins
New in the Library
Contact Us
About the Journal
Staff and Contributors
Back Issues
Download This Issue

Replacement Rules

Rules are a mechanism for replacing a variable with a value in an expression. The ReplaceAll operator, which is abbreviated /. (with no space between the / and the .), applies a rule or list of rules after evaluating the expression. Think of /. as meaning given that and -> (a minus sign together with a greater than sign, with no space in between the two) as meaning goes to. So the expression 2x+3y/.x->5b means 2x+3y given that x goes to 5b.

Notice that all x's in the expression to the left of /. are replaced by 5b, but that x has not been assigned a value.

Here are some more examples of replacement rules, each followed by an explanation of the replacement.

The symbol x gets replaced by 7 and the expression 3y gets replaced by 5a, so the expression 2x+3y is transformed into 2*7+5a, which evaluates to 14+5a.

Mathematica uses a greedy algorithm. It looks for the largest expression that matches a pattern. First Mathematica checks whether there is a rule that matches the entire expression. If not, it checks whether there is a rule that matches part of an expression. In this case, the rule 2x->b matches part of the expression 2x+3y, so the rule is applied. Another way to look at this is that Mathematica matches the most specific expression, and the rule 2x->b is more specific than the rule x->a.

Rules of equal specificity are applied from left to right, so the rule x->a is applied before x->b.

If we put the rule x->b first, it is applied before x->a.

The rule 2x->a is applied here because it matches a larger piece of the entire expression than the rule x->b.

When Mathematica is given a list of lists of rules, each sublist is applied to the original expression. The result is a list of transformed expressions.

Since Mathematica doesn't know anything about x^2 (whether it is positive or negative), it leaves the expression Sqrt[x^2] unchanged. Mathematica internally represents 1/Sqrt[x^2] as (x^2)^-(1/2) and Sqrt[x^2] as (x^2)^(1/2).

Consequently, Mathematica can't find the expression Sqrt[x^2] in the original expression 1/Sqrt[x^2]. Therefore, the rule isn't applied and Mathematica returns the original expression unchanged.

Because Sqrt[x] doesn't appear in the expression 1/Sqrt[x], the first rule (Sqrt[x]->a) is only applied to the first element of the sum (Sqrt[x]). The second rule (x->b) is applied to the second element (1/Sqrt[x]).

Numbers are atomic expressions and can't be subdivided. The rule 1 -> one is applied only when 1 matches the entire number.

Mathematica evaluates the expression to the left of the /. before applying any of the rules. The expression 2x+3x evaluates to 5x and then none of the rules match, so they aren't applied.

Mathematica applies only those rules that apply to the original expression, so 2x gets replaced by a. No other rule applies to the original expression.

The notation //. is an alias for ReplaceRepeated. The rules are applied repeatedly until there are no more rules that apply. On the first pass, 2x gets replaced by a. On the second pass, a gets replaced by b.

The symbol a gets replaced by b, the symbol b gets replaced by c, and the symbol c gets replaced by a.

The rules are applied repeatedly. After applying the result many, many times, Mathematica stops so that it won't get caught in an infinite loop.

Copyright © 2002 Wolfram Media, Inc. All rights reserved.