1.4. Expressions vs. functions: Some puzzles
The following examples are meant to show that there are still a lot of subtle things to learn about variables and functions and how Maple handles them. Do not expect to fully understand these examples now. You should return to these examples after you have gone through the chapter on Maple's evaluation rules and the chapter on procedures in Maple.
Puzzle 1
Here is a subtle example of a difference between an expression and a function. First we define a couple of expressions. The first is an expression in
x
and the second is an expression in
y
.
Now we add these two expressions and get an expression in the two variables
x
and
y
.
Now let us define a couple of Maple functions equivalent to the above expressions. Each of the next two functions is a function of one variable.
Now add these two Maple functions. What do we get?
> |
g3 := g1 + g2; # What kind of function is g3?
|
Is
g3
a function of two variables like
f3
is an expression in two variables? Or is it a function of one variable? The following command shows the formula for
g3
.
So in fact,
g3
is not a function of two variables like
f3
is an expression in two unknowns;
g3
is a function of one variable. This shows that Maple functions and Maple expressions handle unassigned variables in different ways. Let us look at
g3
again. Here is another way to see the formula for
g3
.
If we try to treat
g3
as a function of two variables,
g3
just ignores the second variable.
Why is it that the sum of
f1
and
f2
has two variables in it but the sum of
g1
and
g2
does not?
Puzzle 2
There are two ways that a Maple expression can be converted into a Maple function but these two ways are not equivalent. Here is an example. First, make sure that
x
is unassigned and give
a
,
b
, and
c
values.
> |
x:='x': a:=1: b:=2: c:=3:
|
Here is an expression.
Here is one way to convert this expression into a Maple function.
> |
f := unapply( a*x^2+b*x+c, x );
|
Here is another way. Just use the expression on the right hand side of the arrow operator.
Notice that
f
does not have the constants
a
,
b
, and
c
in its definition, but
g
does! Let us try evaluating both functions.
Now they both have the values for
a
,
b
, and
c
in them. Let us try to differentiate each of these functions.
Notice that the derivative of
g
has the unevaluated constants in it but the derivative of
f
has the constants evaluated.
Let us try changing one of the "constants". Change the value of
c
.
Now evaluate the functions again.
Notice that the definition of
f
did not change, but the definition of
g
did, when we changed
c
.
Puzzle 3
First, let us give the variable
x
a value.
Now try to plot the expression
x^2
.
> |
plot( x^2, x=-10..10 );
|
Error, (in plot) invalid arguments
Maple returned an error (why?). Now try to plot the function
x->x^2
.
> |
plot( x->x^2, -10..10 );
|
There is no problem with this, even though the variable
x
still has a value.
This shows once again that there are subtle differences in the way that Maple treats variables used in expressions and variables used in functions.
Puzzle 4
Let
f
be the name for an expression.
Now use
f
to redefine
f
.
Let us try to do something similar with functions. Let
g
be the name for a function equivalent to
f
.
Now use
g
to redefine
g
.
Error, recursive assignment
Let us try a slightly different way.
Now use
g
to redefine
g
.
Error, (in g) too many levels of recursion
There was no problem when we used
f
to redefine
f
, but we cannot use
g
to redefine
g
. This shows that there are subtle differences in the way that Maple treats the names of expressions and the names of functions.
Puzzle 5
Here is an anonymous expression.
Now let us give it a name.
Now graph the expression by referring to its name.
Now let us try to do something similar with a Maple function. Here is the anonymous expression again.
Use the anonymous expression to define a Maple function.
But now the following graph is empty.
In this sequence of commands we used the last result variable,
%
, twice. But the last result variable in the definition of
f
has a different meaning from the last result variable in the definition of
g
, which is why the definition of
g
does not work the way we might (reasonably) expect it to. Once again this shows that we need to understand the details of exactly how Maple interprets different kinds of variables in different kinds of situations.