Application Center - Maplesoft

App Preview:

Maple Programming: 1.3: Functions in Maple

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

Learn about Maple
Download Application


 

1.03.mws

Programming in Maple

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

roger@calumet.purdue.edu

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 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.

>    3*x^2 - 5*x + 17;

3*x^2-5*x+17

>    sin( (x+1)^b ) + ln(y);

sin((x+1)^b)+ln(y)

>    exp(x+y)/sec(x);

exp(x+y)/sec(x)

>    n! + sum(n^2, n);

n!+1/3*n^3-1/2*n^2+1/6*n

>    a*x + b*y + c*z;

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;

proc (x) options operator, arrow; x^2 end proc

>    x -> a*x^2 + b*x + c;

proc (x) options operator, arrow; a*x^2+b*x+c end proc

>    (x,y) -> x^2 + y^2;

proc (x, y) options operator, arrow; x^2+y^2 end proc

>    z -> sin(z) + exp(z^2);

proc (z) options operator, arrow; sin(z)+exp(z^2) end proc

Look at the first example. We read this as "the function that sends x  to x  squared" (the arrow represents the verb "sends"). Another common way to read this arrow notation is "the function that maps x  to x  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;

a*x^2

>    x -> a*x^2;

proc (x) options operator, arrow; a*x^2 end proc

>    (x,a) -> a*x^2;

proc (x, a) options operator, arrow; a*x^2 end proc

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;

proc (a) options operator, arrow; a*x^2 end proc

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 a*x^2+b*x+c , and it is understood that this defines a function of one variable (the x ) and the function has three parameters (the a , b , and c ). 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;

f := x^2-1

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

g := proc (x) options operator, arrow; x^2-1 end proc

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 Pi  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

g(x) = x^2-1

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 g(x) .

g(x) = 3*x^2-x+5

Now suppose that we follow this formula with the next formula.

g(x) = x^2-1

How should we interpret this second formula? Is it a redefinition of the function g(x) , or is it a short hand formula for the following equation?

3*x^2-x+5 = x^2-1

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 g(x) , 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;

g := proc (x) options operator, arrow; 3*x^2-x+5 end proc

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;

3*x^2-x+5 = 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);

3*x^2-x+5

Now suppose that the second mathematical formula is to be interpreted as a redefinition of the function g(x) . 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;

g := proc (x) options operator, arrow; x^2-1 end proc

Now g  has a new definition.

>    g(x);

x^2-1

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 g(x)  might lead one to use the following Maple command.

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

g(x) := x^2-1

Unfortunately, this (reasonable looking) command does not redefine the function g(x) . Just what it does is a bit confusing. Here is an example that shows that it does not redefine the function g(x) . Let us try redefining g(x)  again.

>    g(x) := 100*x+2;

g(x) := 100*x+2

Now evaluate the function g  at a point.

>    g(2);

3

That is not what we might have expected. Let us check the formula for g .

>    g(x);

100*x+2

That looks correct. Let us try something different. Let us check the formula for g  in a different way.

>    g(t);

t^2-1

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 g(x)  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 .

>    g := cos + ln;

g := cos+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.

>    g(Pi);

-1+ln(Pi)

>    g(a);

cos(a)+ln(a)

Here is a more subtle example.

>    h := cos + (x -> 3*x-1);

h := cos+proc (x) options operator, arrow; 3*x-1 end proc

We have defined h  as the sum of two predefined functions, the predefined function named cos  and the function x->3*x-1 . The later function can be considered as "predefined" in this example because Maple must evaluate the definition x->3*x-1  before it can evaluate the addition that defines h .

>    h(z);

cos(z)+3*z-1

>   

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;

f := proc (x) options operator, arrow; (1+x^2)/x^3 end proc

Here is another way to define the same function f .

>    f := (1 + (x -> x^2))/(x -> x^3);

f := (1+proc (x) options operator, arrow; x^2 end proc)/proc (x) options operator, arrow; x^3 end proc

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);

f := (1+proc (z) options operator, arrow; z^2 end proc)/proc (y) options operator, arrow; y^3 end proc

How does this definition handle an input to f ?

>   

Here is a function named g .

>    g := x -> (1 + exp(x))/x^3;

g := proc (x) options operator, arrow; (1+exp(x))/x^3 end proc

Here is another way to define the same function g .

>    g := (1 + exp)/(x -> x^3);

g := (1+exp)/proc (x) options operator, arrow; x^3 end proc

How does this definition handle an input to g ?

>   

Here is an expression named f .

>    f := x^2;

f := x^2

Here is a function named g  defined using f .

>    g := x -> 2 * x^3 * f;

g := proc (x) options operator, arrow; 2*x^3*f end proc

What is g(x)  equal to?

>   

Here are two functions, f  and g , where g  is defined using f .

>    f := x -> x^2;

f := proc (x) options operator, arrow; x^2 end proc

>    g := 2 * (x -> x^3) * f;

g := 2*proc (x) options operator, arrow; x^3 end proc*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);

g := proc (x) options operator, arrow; 2*x^3*f(x) end proc

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);

w+1/w

>   

Exercise : Give a simplified definition for each of the following Maple functions.

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

f := proc (x, y) options operator, arrow; x^2 end proc+proc (x, y) options operator, arrow; y^2 end proc

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

g := proc (x, y) options operator, arrow; x^2 end proc+proc (y, x) options operator, arrow; y^2 end proc

>   

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);

f := s^2+3+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 := 2*sin - 9/exp;

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);

h := proc (x) options operator, arrow; 2*sin(x)-9/exp(x) end proc

>   

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.

>    h := 2 + exp;

h := 2+exp

>    f := x -> 2 + exp(x);

f := proc (x) options operator, arrow; 2+exp(x) end proc

>   

>   

>