Application Center - Maplesoft

# Maple Programming: 2.1 & 2.2: Full evaluation

You can switch back to the summary page by clicking here.

2.01and2.02.mws

Programming in Maple

Roger Kraft
Department of Mathematics, Computer Science, and Statistics
Purdue University Calumet

roger@calumet.purdue.edu

2.1. Introduction

This chapter begins our discussions of some of the details of Maple's inner workings. Here we look at Maple's evaluation rules for variables, expressions, and functions.

 >

2.2. Full evaluation

When Maple is presented with an expression of some kind, Maple has to decide what the expression means, or to put it another way, what its value is. For example, if p  represents the expression x^2+2*x+1  and x  represents the number 3 , it is not obvious how Maple should evaluate the name p . If asked the meaning of p , Maple could reasonably return any of x^2+2*x+1 , or 3^2+2*3+1 , or 16 . How Maple decides what to return is called an evaluation rule .  Maple has several different evaluation rules that it uses in different situations. In this section we examine the most common and important of Maple's evaluation rules.

Normally Maple uses an evaluation rule called full evaluation . This means that when Maple comes across a variable name, it looks up the name's value, if any. If the value of the variable name contains another variable, its value is also looked up. Maple continues this until it comes up with only numbers and/or unassigned variable names.

Here is an example. First, make x  unassigned.

 > x := 'x';

Make y  an expression in x .

 > y := 1+x;

Give x  a numerical value.

 > x := 5;

Now let us see how Maple evaluates the expression 1+x^2+y .

 > 1+x^2+y;

How did Maple arrive at this value? The first x  in the expression represents 5 so 1^x^2+y  becomes 1+5^2+y . The y  represents 1+x , so 1+5^2+y  becomes 1+5^2+1+x , but the x  can again be evaluated to 5 so finally Maple has 1+5^2+1+5=32 as the value for 1+x^2+y .

 >

Here is another example. First, make both x  and z  unassigned.

 > x := 'x';

 > z := 'z';

Now give y  a value in terms of x  and z  and then give x  a numeric value.

 > y := 1+x+z;

 > x := 5;

Now let us see what the expression 1+x^2+y  evaluates to.

 > 1+x^2+y;

Since z  does not have a value, Maple cannot evaluate the expression any further.

 >

One more simple example. First, unassign x , y , and z .

 > x:='x': y:='y': z:='z':

Now make x  a name for y , y  a name for z , and give z  a numeric value, in that order.

 > x := y;

 > y := z;

 > z := 3;

What does x  evaluate to now?

 > x;

Change the value of z .

 > z := 5;

Check the value of x .

 > x;

In this example, x  points to y , y  points to z , and at first z  pointed to 3,  so x  (and y ) evaluated to 3.  Then z  was pointed at 5, so x  (and y ) evaluated to 5. This is full evaluation. Maple keeps following the trail of assignments until it gets to a "dead end", either a numeric value or an unassigned name.

 >

So far we have looked at pretty easy examples of full evaluation. The results seem pretty obvious. Now we will look at some examples that are less obvious.

Let us modify our last example a little bit. First unassign x , y , and z .

 > x:='x'; y:='y'; z:='z';

We will reverse the order of the first three assignments from the last example.

 > z := 3;

 > y := z;

 > x := y;

What does x  evaluate to?

 > x;

Now change the value of z .

 > z := 5;

Check the value of x  again.

 > x;

In this example, first z  was assigned the value 3. Then, when y  was assigned z ,   z  was evaluated first to its value 3 and so y  was assigned 3, not   z  (this is the full evaluation rule). Similarly, x  was assigned 3, not   y  (why?). So in this example, when the value of z  is changed, the values of x  and y  do not change.

 >

From these last two examples we see, for one thing, that the meaning of three simple commands like x:=y , y:=z  and z:=3  has a lot to do with the order in which they are executed. Or, to put it another way, the meaning of a Maple command often cannot be determined by just looking at the command. We might need to look at all the commands that were executed before it to determine just what the command means.

Here is another example of full evaluation. Suppose we define f  as a function.

 > f := x -> x^2-1;

Now let z  have a value.

 > z := 3;

Now when we enter f(z)  Maple needs to evaluate both the f  and the z . First Maple evaluates the f  to a function (the function that squares its input and then subtracts 1), then it evaluates z  to the number 3 , then it applies the function to the number and calculates the result.

 > f(z);

In general (but not always), Maple uses full evaluation for all the inputs to both functions and Maple commands.

 >

Maple's use of full evaluation can explain some of Maple's more mysterious error messages.

 > solve( x^2=4, x );

```Error, (in solve) a constant is invalid as a variable, 3

```

Maple first evaluates all of the inputs to the solve  command so x^2=4  evaluates to 9=4  (which is not a problem, it is just an equation that does not have any solutions) and the last x  evaluates to 3  and this is what the solve  command complains about (the solve  command can only solve for variables, not for numbers).

 >

Here is another common error message.

 > plot( x^2, x=0..4 );

```Error, (in plot) invalid arguments

```

Maple evaluates all of the inputs to the plot  command first, so it evaluates the expression x^2  to be 9  (which is not a problem) and it evaluates the range x=0..4  to be 3=0..4 , which makes no sense and leads to the error message.

 >

Maple does not always use full evaluation. The next command is not much different from the previous example, but this command does not lead to an error message.

 > seq( x^2, x=0..4 );

Notice that x  still has a value.

 > x;

So Maple did not use full evaluation for the inputs to the seq  command. There is no easy way to find out which Maple commands use full evaluation of their inputs and which ones do not. So we can only reiterate a rule of thumb that we stated much earlier. If you get a strange error message, first check that the variable names that you are using as unknowns really are unassigned variables and do not have values.

There are several more evaluation rules in Maple and, not surprisingly, several exceptions to these rules. In the rest of the sections of this chapter we will go over the most important evaluations rules and some of their important exceptions. In other worksheets we will occasionally have need to look at still other evaluation rules or some other exceptions to these rules.

 >

 >

 >