When is a function not a function?

One of the interesting properties of Maple is its flexibility. Where a minor typo such as an incorrect bracket might cause a barrage of compiler errors in a language like Fortran or C++, Maple will often accept the input, but the result may not be quite what you expect. A classic example is the following incorrect attempt to define a function in Maple Notation.

f( x ) := x^2 
Of course the correct definition is as follows.
f := x -> x^2 
This has been discussed at length elsewhere (see here and here, but note that the second link is rather old, and the behaviour of Maple in 2-D Math Input mode has since been changed). For an amusing experiment try the following.
g := x -> x^3 :
g( x ) := x^2 :
plot( g ) ;
plot( g( x ) ) ;
Most Maple users can probably explain the result. Recently, a student tried a variant of the example below.
h := x^2 ;
plot( h( x ) ) ;
h( 1 )
It's hardly surprising that the last statement fails, since h isn't a function. The statement h( 1 ) returns x(1)^2, i.e. ‘the function x with argument 1, all squared’. In other words, Maple allows for the fact that x might represent a function. We can see this more clearly if x is defined as a function beforehand.
x := y -> sin( y ) :
h := x^2 :
h( 1 )
sin(1)2
What is surprising is that the plot does work.
restart :  # To get rid of the function x
h := x^2 :
plot( h( x ) )
After the argument is evaluated, plot( h( x ) ) becomes plot( x( x )^2 ), which appears to be gibberish. However, we can guess that plot uses something like eval to insert numerical values, since it accepts either functions or expressions. Using eval to set a value for x in our bizarre looking object does indeed produce the square of that value.
eval( x( x )^2 , x = 3 )
9

After both instances of x are replaced by 3, what remains is 3( 3 )^2. Now for the crunch: the left-hand 3 in 3( 3 ) represents the constant function whose value is 3 for all arguments; that is

3( z ) = 3 for all z.

Therefore 3( 3 )^2 becomes 3^2 and finally 9. Since this works for any numerical value, we can now see why the plot works.

This pattern of incidents happens time and time again: a new Maple user does something wrong (the incorrect function definition in this case), but everything seems to be ok initially (the plot). However, later use of the same object in a different context (evaluating at a point) fails. It can be incredibly frustrating, especially if correcting the problem requires substantial changes to existing work or (worse) if there is nobody on hand to explain Maple's behaviour. The issue can be avoided entirely by learning the proper way to do things from the outset, and the best way to do this is to read Understanding Maple. Here endeth the lesson.


Back to main Understanding Maple page