Application Center - Maplesoft

Maple Programming: 2.8: Evaluating function definitions

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

2.08.mws

Programming in Maple

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

roger@calumet.purdue.edu

2.8. Evaluating function definitions

In the last section we emphasized how Maple evaluates function calls. Here we look at how Maple evaluates a function's definition. We will see that this also gives us more information about the evaluation of function calls.

Let us give x  a value.

 > x := -2;

If we define f  as an expression in x , then full evaluation will evaluate x  right away and so x  will not really be part of the value of f .

 > f := x^2-1;

If we now change x , this does not affect f .

 > x := -5;

 > f;

Now define g  as a Maple function and use x  as the independent variable in the definition of g .

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

Maple did not use full evaluation here. None of the x 's on the right hand side of the assignment operator were evaluated. If we evaluate a function call using g , then the x  in the definition of g  will get the operand of the function call, not the current value of x .

 > g(6);

Notice that the current value of x  (which is ) has nothing to do with the evaluation of g(6) .

 >

Here is another example. Suppose we give the name c  a value and then use it in the definition of g  along with x .

 > c := -2;

 > g := x -> c*x^2-1;

Notice that neither c  nor x  was evaluated in the definition of g .

 > eval( g );

If we evaluate a function call using g , then Maple will use the current value of c  in evaluating the function call but not the current value of x . This is because c  is a parameter in the definition of g  but x  is the independent variable (and independent variables get their values from the function call's operands).

 > g(3);

If we now change the value of c , then the definition of g  is unchanged, but the evaluation of g  in a function call will change.

 > c := 2;

 > eval( g );

 > g(3);

We can even leave c  unassigned.

 > c := 'c';

 > g(3);

 >

So when Maple evaluates a function definition using the arrow operator, it does not evaluate any of the names in the definition, neither the independent variables nor the parameters. When Maple evaluates a function call, then the independent variables get their values from the operands of the function call, and the parameters in the function definition are fully evaluated to whatever values they may currently have at the time of the function call.

 >

So far we have looked at how Maple evaluates a function definition that uses the arrow notation. But we can also define Maple functions using the unapply  command. Let us see how Maple evaluates a function definition that uses the unapply  command.

 > g := unapply( c*x^2-1, x );

```Error, (in unapply) variables must be unique and of type name

```

We got an error message. What happened is that Maple used full evaluation for the unapply  command. So this last command looked to Maple like the following

 > g := unapply( 25*c-1, -5 );

```Error, (in unapply) variables must be unique and of type name

```

because x  has a value (and c  does not).

 > x, c;

Here is a way to verify this.

 > 'unapply'( c*x^2-1, x );

So the unapply  command uses full evaluation. Let us see how that affects the function we are defining. First of all, we must have the independent variable (in this case x ) as an unassigned variable.

 > x := 'x';

Now we can use unapply .

 > g := unapply( c*x^2-1, x );

Notice that since c  is currently unassigned, c  is a parameter in the definition of g .

 > g(3);

We can give c  different values and get different evaluations of function calls to g .

 > c := -1;

 > g(3); g(w);

 > c := 2;

 > g(3); g(w);

What happens if we now use unapply  again to redefine g  using the same expression?

 > g := unapply( c*x^2-1, x );

Now, since c  is an assigned variable and Maple uses full evaluation in the unapply  command, we no longer have c  as a parameter in the definition of g . The value 2 is now a permanent part of the definition of g .

 > eval( g );

 > c := 5;

 > eval( g );

What if we want to use unapply  and force c  to be a parameter in the definition of g ? Then we would need to delay the evaluation of c  in the unapply  command.

 > g := unapply( 'c'*x^2-1, x );

 > eval( g );

 > g(x);

Why is there a 5 in the last expression?

 > c := -2;

 > g(x);

 >

In summary, the arrow operator and the unassign  command use very different evaluation rules when used to define a Maple function. The arrow operator does not evaluate any of the names used in the definition of the function, and the unapply  command uses full evaluation (except in cases where last name evaluation should be used).

 >

Exercise : Explain each the following three sequences of commands.

 > h := x -> x^2;

 > g := x -> c*h(x) -1;

 > g(3);

 > h := x -> x^3;

 > g(3);

 >

 > h := x -> x^2;

 > g := unapply( c*h(x)-1, x );

 > g(3);

 > h := x -> x^3;

 > g(3);

 >

 > h := x -> x^2;

 > g := unapply( c*'h'(x)-1, x );

 > g(3);

 > h := x -> x^3;

 > g(3);

 > eval( g );

 >

Exercise : Explain the following sequence of commands.

 > h := x -> x^3;

 > g := h -> h(5);

 > g(h);

 > h := x -> x^2;

 > g(h);

 > g := unapply( h(5), h );

 > g := unapply( 'h'(5), h );

Since h  has a value, why was there no error message from the last two commands?

 > eval( h );

For example, here we get an error message

 > x := 5;

 > unapply( h(x), x );

```Error, (in unapply) variables must be unique and of type name

```

which we get rid of this way.

 > unapply( h(x), 'x' );

 >

Exercise : Explain the following sequence of commands.

 > h := x -> x^3;

 > g := h -> h(x);

 > g(h);

 > x := y;

 > g(h);

 > h := x -> x^2;

 > g(h);

 > g := unapply('h'(x), h);

 > x := z;

 > g(h);

 > g := unapply('h'('x'), h);

 > g(h);

 >

Exercise : Explain with as much detail as you can how Maple evaluates the following commands. What rules of evaluation are used at each step.

 > x := 0;

 > h := x -> x^2;

 > g := (h, x) -> h(x);

 > g(h, y);

 > g := (h, x) -> 'h'(x);

 > g(h, y);

 > g(sin, Pi);

 > g(eval(h), y);

 > %;

 > eval(g(h, y));

 > g(eval(sin), Pi);

How would we use unapply  to define the exact same function g  that we have right now?

 > eval( g );

The following is not correct. Try to fix it.

 > g := unapply( 'h'('x'), h, 'x');

Explain what happens if you remove any one of the pairs of right-quotes from the unapply  command. And why does the last h  in the unapply  command not even need right-quotes?

 >

 >

 >