Wolfram Research, Inc.

2.5.1 Principles of Evaluation

The fundamental operation that Mathematica performs is evaluation. Whenever you enter an expression, Mathematica evaluates the expression, then returns the result.

Evaluation in Mathematica works by applying a sequence of definitions. The definitions can either be ones you explicitly entered, or ones that are built into Mathematica.

Thus, for example, Mathematica evaluates the expression 6 + 7 using a built-in procedure for adding integers. Similarly, Mathematica evaluates the algebraic expression x - 3x + 1 using a built-in simplification procedure. If you had made the definition x = 5, then Mathematica would use this definition to reduce x - 3x + 1 to -9.

The two most central concepts in Mathematica are probably expressions and evaluation. Section 2.1 discussed how all the different kinds of objects that Mathematica handles are represented in a uniform way using expressions. This section describes how all the operations that Mathematica can perform can also be viewed in a uniform way as examples of evaluation.

Some interpretations of evaluation.

Mathematica is an infinite evaluation system. When you enter an expression, Mathematica will keep on using definitions it knows until it gets a result to which no definitions apply.

This defines x1 in terms of x2, and then defines x2.

In[1]:= x1 = x2 + 2 ; x2 = 7

Out[1]=

If you ask for x1, Mathematica uses all the definitions it knows to give you a result.

In[2]:= x1

Out[2]=

Here is a recursive definition in which the factorial function is defined in terms of itself.

In[3]:= fac[1] = 1 ; fac[n_] := n fac[n-1]

If you ask for fac[10], Mathematica will keep on applying the definitions you have given until the result it gets no longer changes.

In[4]:= fac[10]

Out[4]=

When Mathematica has used all the definitions it knows, it gives whatever expression it has obtained as the result. Sometimes the result may be an object such as a number. But usually the result is an expression in which some objects are represented in a symbolic form.

Mathematica uses its built-in definitions for simplifying sums, but knows no definitions for f[3], so leaves this in symbolic form.

In[5]:= f[3] + 4f[3] + 1

Out[5]=

Mathematica follows the principle of applying definitions until the result it gets no longer changes. This means that if you take the final result that Mathematica gives, and enter it as Mathematica input, you will get back the same result again. (There are some subtle cases discussed in Section 2.5.12 in which this does not occur.)

If you type in a result from Mathematica, you get back the same expression again.

In[6]:= 1 + 5 f[3]

Out[6]=

At any given time, Mathematica can only use those definitions that it knows at that time. If you add more definitions later, however, Mathematica will be able to use these. The results you get from Mathematica may change in this case.

Here is a new definition for the function f.

In[7]:= f[x_] = x^2

Out[7]=

With the new definition, the results you get can change.

In[8]:= 1 + 5 f[3]

Out[8]=

The simplest examples of evaluation involve using definitions such as f[x_] = x^2 which transform one expression directly into another. But evaluation is also the process used to execute programs written in Mathematica. Thus, for example, if you have a procedure consisting of a sequence of Mathematica expressions, some perhaps representing conditionals and loops, the execution of this procedure corresponds to the evaluation of these expressions. Sometimes the evaluation process may involve evaluating a particular expression several times, as in a loop.

The expression Print[zzzz] is evaluated three times during the evaluation of the Do expression.

In[9]:= Do[Print[zzzz], {3}]