1.2.mws
Programming in Maple
Roger Kraft
Department of Mathematics, Computer Science, and Statistics
Purdue University Calumet
1.2. Functions in Mathematics
We quickly review here the definition of a mathematical function. A function is three things bundled together. It is a set of inputs (the domain), a set of outputs (the codomain), and a rule for associating one of the outputs to each of the inputs. Functions can be defined in several ways, for example by formulas, by tables, and by graphs. Most of the time in mathematics, the sets of inputs and outputs are sets of numbers. For most people, the "rule" part of a function's definition seems the most important, but that is not really a good way to think about functions. To demonstrate that a mathematical function is really more than just its rule, let us look at an example.
>
We shall define three different functions named f, g, and h. The rule for each of these functions will be given by a formula. In fact we will use the same formula in all three cases. What will make the three functions different will be their domains.
The function f has as its domain the set of all real numbers, its codomain is the set of all positive real numbers, and its rule is given by the formula
.
The function g has as its domain the set of all positive real numbers, its codomain is the set of all positive real numbers, and its rule is given by the formula
.
The function h has as its domain the set of all negative real numbers, its codomain is the set of all positive real numbers, and its rule is given by the formula
.
Exercise
: Draw graphs for each of the functions f, g, and h.
>
How do we know that these three functions are different? The answer is that they have different properties. For example, the function f is not invertible (why?). The functions g and h are both invertible (why?) but they have different inverses. The inverse of the function g has its rule given by the formula
. The inverse of the function h has its rule given by the formula
.
So f, g, and h all have the exact same formula (i.e., rule) but they are
not
the same function. The domain (and codomain) really are important parts of the definition of a mathematical function.
Exercise
: What is the domain and codomain for each of
and
?
>
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 non-obvious. 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 evaluations 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.
>
3*x^2 - 5*x + 17;
>
sin( (x+1)^b ) + ln(y);
>
exp(x+y)/sec(x);
>
n! + sum(n^2, n);
>
a*x + b*y + c*z;
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.
>
x -> x^2;
>
x -> a*x^2 + b*x + c;
>
(x,y) -> x^2 + y^2;
>
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.
>
a*x^2;
>
x -> a*x^2;
>
(x,a) -> a*x^2;
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?
>
a -> a*x^2;
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
.
>
f := x^2 - 1;
>
g := x -> x^2 - 1;
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.
>
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.
>
g := x -> 3*x^2-x+5;
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 (
=
).
>
g(x) = x^2-1;
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
.
>
g(x);
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.
>
g := x -> x^2-1;
Now
g
has a new definition.
>
g(x);
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.
>
g(x) := x^2-1;
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.
>
g(x) := 100*x+2;
Now evaluate the function
g
at a point.
>
g(2);
That is not what we might have expected. Let us check the formula for
g
.
>
g(x);
That looks correct. Let us try something different. Let us check the formula for
g
in a different way.
>
g(t);
Exactly what is going on here is a bit hard to explain. We will return to this odd situation later. 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.
>
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
.
>
f := x^2;
Here is a function named
g
defined using
f
.
>
g := x -> 2 * x^3 * f;
What is
g(x)
equal to?
>
Here are two functions,
f
and
g
, where
g
is defined using
f
.
>
f := x -> x^2;
>
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.
>
(x -> x + x^(-1))(w);
>
Maple has some built in functions that have already been assigned names. Here is an example that uses a couple of these built in functions.
>
h := 2*sin - 9/exp;
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
: 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 pi times the cube of the input.
>
>
>