Values for Symbols

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

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

To take an expression such as and replace the symbol 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 with the value , you would create the transformation rule . You must type as a pair of characters, with no space in between. You can think of as being a rule in which " goes to ".

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

This uses the transformation rule in the expression .
In[1]:=
Click for copyable input
Out[1]=
You can replace with any expression. Here every occurrence of is replaced by .
In[2]:=
Click for copyable input
Out[2]=
Here is a transformation rule. Mathematica treats it like any other symbolic expression.
In[3]:=
Click for copyable input
Out[3]=
This applies the transformation rule on the previous line to the expression .
In[4]:=
Click for copyable input
Out[4]=
expr/.x->valuereplace x by value in the expression expr
expr/.{x->xval,y->yval}perform several replacements

Replacing symbols by values in expressions.

You can apply rules together by putting the rules in a list.
In[5]:=
Click for copyable input
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 with whenever occurs.

As discussed in "Defining Variables", you can do this by assigning the value to using . Once you have made the assignment , will always be replaced by , whenever it appears.

This assigns the value to .
In[6]:=
Click for copyable input
Out[6]=
Now will automatically be replaced by wherever it appears.
In[7]:=
Click for copyable input
Out[7]=
This assigns the expression to be the value of .
In[8]:=
Click for copyable input
Out[8]=
Now is replaced by .
In[9]:=
Click for copyable input
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.

x=valuedefine a value for x that will always be used
x=.remove any value defined for x

Assigning values to symbols.

The symbol still has the value you assigned to it.
In[10]:=
Click for copyable input
Out[10]=
This removes the value you assigned to .
In[11]:=
Click for copyable input
Now has no value defined, so it can be used as a purely symbolic variable.
In[12]:=
Click for copyable input
Out[12]=

A symbol such as 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 straight in order to avoid making mistakes. The most important distinction is between the use of 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, 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 , then will always be replaced by , and can no longer serve as a formal variable.

You should understand that explicit definitions such as have a global effect. On the other hand, a replacement such as 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 .
In[13]:=
Click for copyable input
Out[13]=
This finds the value of , and then replaces by in it.
In[14]:=
Click for copyable input
Out[14]=
This finds the value of for a different value of .
In[15]:=
Click for copyable input
Out[15]=
This finds the value of when is replaced by Pi, and then evaluates the result numerically.
In[16]:=
Click for copyable input
Out[16]=
New to Mathematica? Find your learning path »
Have a question? Ask support »