Application Center - Maplesoft

App Preview:

Maple Programming: 1.7: Functions that return a function

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

Learn about Maple
Download Application


 

1.07.mws

Programming in Maple

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

roger@calumet.purdue.edu

1.7. Functions that return a function (optional)

It is possible to do some unusual things with definitions of Maple functions. For example, we can define a function whose return value is another function. Here is a simple example.

>    f := a -> ( y->a*y );

f := proc (a) options operator, arrow; proc (y) options operator, arrow; a*y end proc end proc

The Maple function f  takes in a number and returns a function  that multiplies its input by the number.

>    f(3);

proc (y) options operator, arrow; 3*y end proc

So f(3)  is a function that multiplies its input by 3. Here is how we apply f(3)  to the input 4.

>    f(3)(4);

12

Here is another call to f .

>    f(-5);

proc (y) options operator, arrow; (-5)*y end proc

Here is a call to the function that was returned by f .

>    f(-5)(2);

-10

In some sense you can think of f  as a function of two variables.

>    f(x)(y);

x*y

But this can be misleading. Consider the next two function calls.

>    f(x);

proc (y) options operator, arrow; x*y end proc

>    f(y);

proc (y) options operator, arrow; y*y end proc

>   

Exercise : Consider the output of f(y) .

>    f(y);

proc (y) options operator, arrow; y*y end proc

Now explain why the following function call does not return 9.

>    f(y)(3);

3*y

>   

Exercise : Each of the following functions that returns a function somehow involves a constant function. Explain where the constant functions are, and explain the reason for the way Maple simplifies each command.

>    (x->x->x^2)(3);

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

>    (x->y->x^2)(3);

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

>   

Here is an example of how we might use the idea of a function that returns a function. Start with a function of two variables.

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

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

Now we shall define "slicing" functions for f . If we hold one input to f  fixed, then we get a new function of a single variable that is sometimes called a "slice" (or a "section") of f . If we just plug a constant into f  for say y , then we get back an expression in x .

>    f(x,3);

3*x^2+45

We can turn that into a Maple function like this.

>    fx3 := x -> f(x,3);

fx3 := proc (x) options operator, arrow; f(x,3) end proc

Now fx3  is the slice of f  with y  fixed at 3 .

>    fx3(x);

3*x^2+45

We can use a function that returns a function to automate these steps for generating slicing functions.

>    slice_f_with_y_fixed := c -> ( x->f(x,c) );

slice_f_with_y_fixed := proc (c) options operator, arrow; proc (x) options operator, arrow; f(x,c) end proc end proc

The input to slice_f_with_y_fixed  is the number we want the slice function at and the output is the slice function.

>    fx3 := slice_f_with_y_fixed(3);

fx3 := proc (x) options operator, arrow; f(x,3) end proc

>    fx3(x);

3*x^2+45

>   

Now do the same for slices in the other direction.

>    slice_f_with_x_fixed := c -> ( y->f(c,y) );

slice_f_with_x_fixed := proc (c) options operator, arrow; proc (y) options operator, arrow; f(c,y) end proc end proc

>    f3y := slice_f_with_x_fixed(3);

f3y := proc (y) options operator, arrow; f(3,y) end proc

>    f3y(y);

27+5*y^2

>   

One nice feature of our slicing functions is that they still work if we redefine f . We do not need to redefine the slicing functions.

>    f := (u,v) -> sin(5*u-v);

f := proc (u, v) options operator, arrow; sin(5*u-v) end proc

>    fu3 := slice_f_with_y_fixed(3);

fu3 := proc (x) options operator, arrow; f(x,3) end proc

>    fu3(u);

sin(5*u-3)

Did you notice something a bit funny about the redefinition of f  and the call to slice_f_with_y_fixed ?

>   

Exercise : Come up with better names for slice_f_with_y_fixed  and slice_f_with_x_fixed .

>   

Exercise : Define a Maple function slicer1  with two inputs, one input a Maple function h  of two variables and the other input a number c , that returns a Maple function that is the slice of the input function with its second input held fixed at c . Also define slicer2  in an analogous way. Test the functions slicer1  and slicer2 .

>   

Exercise : Write Maple functions shift_vertically  and shift_horizontally  (you can abbreviate these names if you wish) that each take in two inputs, one input is a function of a single variable and the other input is a number, and that each return a function. The procedure shift_vertically  produces an output function that is its input function shifted vertically by the amount of the input number, and shift_horizontally  produces an output function that is its input function shifted horizontally by the amount of the input number. Apply your procedures to some test functions and show that they work by graphing the test functions and their shifted versions in the same graph.

>   

Exercise : Suppose you wanted to write a version of slicer1  that worked on expressions instead of Maple functions. That is, suppose you wanted to write a procedure slicer1  that takes as its input an expression of two variables and a number, and produces as its output an expression in one variable that represents the slice function of the function represented by the input expression with one of the inputs held fixed at the input number. What kind of problem are you going to have with trying to write this version of slicer1 ? (The problem you will have is in fact one of the major weaknesses of working with expressions.)

>   

The idea of a function that returns a function is very common in mathematics. Sometimes these kinds of functions are given the special name functionals . One important branch of mathematics is called functional analysis . It is the study of the properties of functionals. In particular, functional analysis tries to generalize classical analysis, that is calculus with real and vector valued functions, to function valued functions.

Functions that return functions are also important in computer science. They lead to an important family of programming languages called functional programming languages . Some common functional programming languages are Lisp, Scheme, ML, and Haskell. These languages are very different from more traditional programming languages like C, C++, C#, Java, Fortran and Basic. As might be expected, functional programming languages have a much more mathematical feel to them then the more traditional programming languages. Functional programming languages have played an important role in the development of the field of artificial intelligence and in the theoretical developement of programming languages in general.

When we get to the chapter about Maple procedures, we will generalize the idea of a function that returns a function to the idea of a procedure that returns a procedure.

>   

>