Wolfram Research, Inc.

1.4.2 Values for Symbols

When Mathematica transforms an expression such as x + x into 2x, it is treating the variable x in a purely symbolic or formal fashion. In such cases, x is a symbol which can stand for any expression.

Often, however, you need to replace a symbol like x with a definite "value". Sometimes this value will be a number; often it will be another expression.

To take an expression such as 1 + 2x and replace the symbol x that appears in it with a definite value, you can create a Mathematica transformation rule, and then apply this rule to the expression. To replace x with the value 3, you would create the transformation rule x -> 3. You must type -> as a pair of characters, with no space in between. You can think of x -> 3 as being a rule in which "x goes to 3".

To apply a transformation rule to a particular Mathematica expression, you type expr /. rule. The "replacement operator" /. is typed as a pair of characters, with no space in between.

This uses the transformation rule x->3 in the expression 1 + 2x.

In[1]:= 1 + 2x /. x -> 3

Out[1]=

You can replace x with any expression. Here every occurrence of x is replaced by 2 - y.

In[2]:= 1 + x + x^2 /. x -> 2 - y

Out[2]=

Here is a transformation rule. Mathematica treats it like any other symbolic expression.

In[3]:= x -> 3 + y

Out[3]=

This applies the transformation rule on the previous line to the expression x^2 - 9.

In[4]:= x^2 - 9 /. %

Out[4]=

Replacing symbols by values in expressions.

You can apply rules together by putting the rules in a list.

In[5]:= (x + y) (x - y)^2 /. {x -> 3, y -> 1 - a}

Out[5]=

The replacement operator /. allows you to apply transformation rules to a particular expression. Sometimes, however, you will want to define transformation rules that should always be applied. For example, you might want to replace x with 3 whenever x occurs.

As discussed in Section 1.2.2, you can do this by assigning the value 3 to x using x = 3. Once you have made the assignment x = 3, x will always be replaced by 3, whenever it appears.

This assigns the value 3 to x.

In[6]:= x = 3

Out[6]=

Now x will automatically be replaced by 3 wherever it appears.

In[7]:= x^2 - 1

Out[7]=

This assigns the expression 1 + a to be the value of x.

In[8]:= x = 1 + a

Out[8]=

Now x is replaced by 1 + a.

In[9]:= x^2 - 1

Out[9]=

You can define the value of a symbol to be any expression, not just a number. You should realize that once you have given such a definition, the definition will continue to be used whenever the symbol appears, until you explicitly change or remove the definition. For most people, forgetting to remove values you have assigned to symbols is the single most common source of mistakes in using Mathematica.

Assigning values to symbols.

The symbol x still has the value you assigned to it above.

In[10]:= x + 5 - 2x

Out[10]=

This removes the value you assigned to x.

In[11]:= x =.

Now x has no value defined, so it can be used as a purely symbolic variable.

In[12]:= x + 5 - 2x

Out[12]=

A symbol such as x can serve many different purposes in Mathematica, and in fact, much of the flexibility of Mathematica comes from being able to mix these purposes at will. However, you need to keep some of the different uses of x straight in order to avoid making mistakes. The most important distinction is between the use of x as a name for another expression, and as a symbolic variable that stands only for itself.

Traditional programming languages that do not support symbolic computation allow variables to be used only as names for objects, typically numbers, that have been assigned as values for them. In Mathematica, however, x can also be treated as a purely formal variable, to which various transformation rules can be applied. Of course, if you explicitly give a definition, such as x = 3, then x will always be replaced by 3, and can no longer serve as a formal variable.

You should understand that explicit definitions such as x = 3 have a global effect. On the other hand, a replacement such as expr /. x->3 affects only the specific expression expr. It is usually much easier to keep things straight if you avoid using explicit definitions except when absolutely necessary.

You can always mix replacements with assignments. With assignments, you can give names to expressions in which you want to do replacements, or to rules that you want to use to do the replacements.

This assigns a value to the symbol t.

In[13]:= t = 1 + x^2

Out[13]=

This finds the value of t, and then replaces x by 2 in it.

In[14]:= t /. x -> 2

Out[14]=

This finds the value of t for a different value of x.

In[15]:= t /. x -> 5a

Out[15]=

This finds the value of t when x is replaced by Pi, and then evaluates the result numerically.

In[16]:= t /. x -> Pi //N

Out[16]=