1.3. Functions in Maple
There are two ways to represent mathematical functions in Maple, as Maple expressions and as Maple functions. These two ways of representing mathematical functions are not equivalent. Each way has it advantages and disadvantages. In this section we review these two representations and in the next section we look at some examples that show how they are not equivalent.
The differences between these two representations can be subtle and nonobvious. To fully understand the differences between these two representations we need to learn about several topics from Maple programming. In particular, a full explanation of the differences between expressions and Maple functions requires an understanding of evaluation rules, data structures, procedures, local, global and lexical variables, parameter passing, remember tables, and several other ideas. Another way to think about this is that learning about Maple programming will not only allow you to do more with Maple but it will also give you a deeper understanding of very basic Maple ideas like defining a function.
Let us first look at mathematical functions represented as Maple expressions. We define an expression in Maple as just about any mathematical formula that you can write down that does not have an equal sign or any inequalities in it. Here are some examples of expressions.
> 
sin( (x+1)^b ) + ln(y);

Notice that some of these are expressions in one variable, others are expressions in two or more variables. (How many variables are in the last expression?) Also notice that calls to Maple procedures are allowed as parts of expressions.
These expressions look a lot like the definitions of mathematical functions, which is why they can be used to represent mathematical functions in Maple. But expressions are not what Maple refers to as "functions". A
Maple function
is something defined using
arrow notation
. Here are several examples of Maple functions.
> 
z > sin(z) + exp(z^2);

Look at the first example. We read this as "the function that sends
to
squared" (the arrow represents the verb "sends"). Another common way to read this arrow notation is "the function that maps
to
squared". On the left of the arrow are variables that represent the input to the function. On the right hand side of the arrow there is an expression that represents the rule of the function. (The arrow, by the way, is made up of a minus sign and a greater than sign. There should not be a space between them.) Notice that Maple functions are not really mathematical functions since there is no mention of a domain or a codomain. But Maple functions are clearly meant to define a rule showing how an output is computed from an input.
So
both Maple expressions and Maple functions can be used in Maple to represent mathematical functions. To see how Maple functions can differ from Maple expressions when used to represent mathematical functions, consider the following three examples.
In the first example the expression is defining a function of two variables since there is really no way in Maple to give either of the unknowns
x
or
a
any more importance than the other. But in the second example the arrow notation clearly singles out the unknown
x
as the input so this defines a function of one variable, and the unknown
a
is to be thought of as a constant (or a "parameter"). In the third example the arrow notation clearly defines a function of two variables. What does the following command define?
Notice how these last examples demonstrate a difference between Maple and standard mathematical notation. For example, when one writes a formula for the general quadratic, one usually writes
, and it is understood that this defines a function of one variable (the
) and the function has three parameters (the
,
, and
). This is because we have a
convention
in mathematics to treat some letters as variables (for example x, y, and z) and some other letters as constants or parameters (for example a, b, and c). Maple does not have any knowledge of this convention so in an expression Maple treats all unassigned names (i.e., all unknowns) as variables.
In all of the examples of expressions and Maple functions given above, we never gave any of them a name. But we can always use the assignment operator to give an expression or function a name, and this makes working with expressions and functions much more convenient. So for example, here is an expression named
f
and a Maple function named
g
.
Notice that the last command does two distinct things. It defines a Maple function and it assigns the function a name. When you define a Maple function using the arrow notation and give it a name at the same time using the assignment operator, you get a Maple command that can look quite strange at first, but you need to get very used to this notation.
Exercise
: Use the arrow notation to define a function that takes two numbers as input and then squares the first number minus the second number and subtracts from that the quotient of the first number squared with one minus the second number.
Exercise
: Use the arrow notation to define a function that takes one number as input and then returns three times the cosine of
times the cube of the input.
Let us compare Maple's notation with the standard mathematical notation for defining and naming a function. The mathematical notation
defines a mathematical function named g that is equivalent to the Maple function named
g
defined by the Maple command
g := x > x^2  1
.
When we compare these two notations we notice a major difference right away. The Maple notation clearly separates the defining of the function (the arrow operation) from the naming of the function (the assignment operation) but the mathematical notation combines these two operations into one use of the equal sign. The mathematical notation has one clear advantage over the Maple notation. The mathematical notation is more compact. But the mathematical notation has the disadvantage of being somewhat ambiguous. Here is an example of how an ambiguity can arise. The following mathematical formula can be interpreted as the definition and naming of a function
.
Now suppose that we follow this formula with the next formula.
How should we interpret this second formula? Is it a redefinition of the function
, or is it a short hand formula for the following equation?
There is really no way to tell from the second formula itself which interpretation is correct. The cause of this ambiguity is that in standard mathematical notation, the equal sign has two distinct uses, as either part of an assignment statement or as part of an equation. If the second formula above is meant to be a redefinition of the function
, then the equal sign in that formula is acting as part of an assignment statement. If on the other hand the second formula is meant to be a short hand for the third formula, then the equal sign in the second formula is acting as part of an equation. Let us translate these formulas into Maple and see what the two interpretations lead to. We translate the first mathematical formula as the definition of a function
g
represented as a Maple function.
Notice that since this was a definition, we used the assignment operator, colon equal (
:=
). Now suppose that the second mathematical formula is to be interpreted as a short hand for the third formula. In this case the second formula is an equation, so it is translated into a Maple equation using an equal sign (
=
).
We see right away that Maple interpreted this equation as a short hand for the third mathematical formula above. Notice that the last Maple command
did not
change the definition of
g
.
Now suppose that the second mathematical formula is to be interpreted as a redefinition of the function
. In this case we translate the second mathematical formula into a Maple statement that redefines
g
by using the assignment operator.
Now
g
has a new definition.
Notice that in Maple, the two possible interpretations of the second mathematical formula translate into two distinctly different commands, one using an equal sign and the other using colon equal. Maple makes a clear distinction between an equation and an assignment but, unfortunately, standard mathematical notation does not.
Warning:
Interpreting the second mathematical formula as a redefinition of
might lead one to use the following Maple command.
Unfortunately, this (reasonable looking) command does not redefine the function
. Just what it does is a bit confusing. Here is an example that shows that it does not redefine the function
. Let us try redefining
again.
Now evaluate the function
g
at a point.
That is not what we might have expected. Let us check the formula for
g
.
That looks correct. Let us try something different. Let us check the formula for
g
in a different way.
Exactly what is going on here is a bit hard to explain. We will return to this odd situation later (in Section 4.11). For now, remember not to use the
g(x)
notation on the left hand of an assignment operator when you define a Maple function, even though the mathematical notation does use
on the left hand side of the equal sign when defining a function.
So far, we have been defining Maple functions by using the arrow notation. It is possible to define a Maple function without using the arrow operator if we build the function up out of predefined Maple functions. For example, the functions
cos
and
ln
are predefined in Maple. Here is a definition of a Maple function
g
built up out of
cos
and
ln
.
We have defined
g
to be the sum of the two functions
cos
and
ln
. Notice how we only needed to tell Maple the names of the functions involved. We did not need to use any variables in the definition of
g
. Since
g
is a Maple function, we can evaluate
g
at an input using regular function notation.
Here is a more subtle example.
> 
h := cos + (x > 3*x1);

We have defined
h
as the sum of two predefined functions, the predefined function named
cos
and the function
x>3*x1
. The later function can be considered as "predefined" in this example because Maple must evaluate the definition
x>3*x1
before it can evaluate the addition that defines
h
.
We end this section with some more examples of using the arrow notation. These examples are purposely a bit confusing in order to make you think about this important notation.
Here is a function named
f
.
> 
f := x > (1 + x^2)/x^3;

Here is another way to define the same function
f
.
> 
f := (1 + (x > x^2))/(x > x^3);

Why did this define the same function as the first definition? Here is a third definition of the same function
f
.
> 
f := (1 + (z > z^2))/(y > y^3);

How does this definition handle an input to
f
?
Here is a function named
g
.
> 
g := x > (1 + exp(x))/x^3;

Here is another way to define the same function
g
.
> 
g := (1 + exp)/(x > x^3);

How does this definition handle an input to
g
?
Here is an expression named
f
.
Here is a function named
g
defined using
f
.
What is
g(x)
equal to?
Here are two functions,
f
and
g
, where
g
is defined using
f
.
> 
g := 2 * (x > x^3) * f;

What is
g(x)
equal to?
Here is another way to define
g
using the function
f
.
> 
g := x > 2 * x^3 * f(x);

Be sure to examine the last three definitions of
g
carefully. They all define the same function. How does each of them make use of functions, expressions, and the arrow notation?
Exercise
: Explain the result of the following command.
Exercise
: Give a simplified definition for each of the following Maple functions.
> 
f := ((x,y)>x^2) + ((x,y)>y^2);

> 
g := ((x,y)>x^2) + ((y,x)>y^2);

Exercise
: Explain in detail the steps that Maple uses to automatically simplify the following command.
> 
f := ((x,y)>((x,y)>x*y)(x,x)+((u,v)>u+v)(3,y))(s,t);

Maple has many built in functions that have already been assigned names. Here is an example that uses a couple of these built in functions.
h
is a function,
not
an expression. Here is another way to define the same function
h
.
> 
h := x > 2*sin(x)  9/exp(x);

Exercise
: The
2
in the following definition of
h
does not have the same meaning as the
2
in the following definition of
f
. Explain the different meanings of these two
2
's.