How to | Create and Use Rules
Transformation rules in the Wolfram Language let you set local values for symbols, functions, and all other types of expressions. Using rules provides a powerful and extensible method to replace all or part of another expression with the value you specify.
The short form for a rule uses a right arrow, which you get by typing -> (with no space between - and >). The Wolfram System front end automatically converts -> into upon further typing. Either symbol is a short form for Rule.
Create the following transformation rule, which can be thought of as "x goes to 3":
x -> 3By looking at the output for x3, you can see that this rule does not do anything: the output is simply the rule itself. This is because rules do not do anything when they are alone. You must use a rule with an expression for it to be of any use.
Rules can be applied to expressions by using /. (the short form for ReplaceAll). The general syntax for this is expr/.rules.
Use /. to use a rule with an expression:
2x + 1 /. x -> 3To use two or more rules with an expression, place them in a list {}:
2x + y /. {x -> 3, y -> 4}If you give two rules for the same variable, the Wolfram Language will use only the first rule:
2x + y /. {x -> 3, x -> 4}You can replace variables with any expression, not just individual values.
2x + y /. x -> 3yYou can also use a rule to replace larger parts of an expression:
9 + x ^ 2 - 9x ^ 3 /. x ^ 2 - 9x ^ 3 -> 3yIn fact, you can use rules with any expression, including functions.
1 + f[x] + f[y] /. x -> 3Use a rule for f[x]. Note that this rule matches f[x] exactly and does not affect f[y]:
1 + f[x] + f[y] /. f[x] -> pTo replace the function f regardless of its argument, you must use a pattern in the rule.
The rule f[x_]x^2 can be read as "f[anything] goes to anything^2":
1 + f[x] + f[y] /. f[x_] -> x ^ 2For more information on using patterns, see "Introduction to Patterns."
Rules that are set up using are immediate rules. That is, the right-hand side is evaluated at the same time as the rule:
x -> RandomReal[]You may need to use delayed rules instead, which are not evaluated until they are used with an expression. Delayed rules are created by using RuleDelayed.
The short form for a delayed rule is :> (with no space between : and >). The Wolfram System front end automatically converts :> into upon typing. Either represents the short form for RuleDelayed:
x :> RandomReal[]Consider a problem where you want to use a rule to generate three random real numbers in the range of 0 to 1. Using an immediate rule results in the generation of the same three numbers:
{x, x, x} /. x -> RandomReal[]To generate three different numbers, use a delayed rule:
{x, x, x} /. x :> RandomReal[]Assignments set explicitly using = have a global effect, while rules only affect the expression with which they are used.
Use = to assign x to 3, and then evaluate x to see the value:
x = 3;xUse a rule to assign a value for y:
y -> 3;Evaluating y, you can see that the value assigned by the rule was not saved:
yYou must use a rule with an expression for it to work. However, you can explicitly assign a rule to a symbol and then use that symbol as you would the rule.
Use = to assign the rule p2 to n, and then use n with an expression:
n = p -> 2;2p /. nSince p2 is now stored globally as the symbol n, you can continue to use n in place of p2.
Similarly, you can explicitly assign an expression to a symbol and then use a rule on the symbol:
t = 1 + y ^ 2 + y ^ 3 + 3y;t /. y -> 2This is especially convenient if you plan to use the expression in more than one computation.