Access Keys:
Skip to content (Access Key - 0)

How can I write functions in Maple?

Mathematical functions of one or more variables may be defined as follows:

        f:=(x_1,x_2...x_n) -> function definition

For example:

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

defines f as a function of the 3 variables x,y,z.

To evaluate the function at a given point, we just call the function as we normally would passing argument values:


Operations such as differentiation are treated differently from those of ordinary algebraic expressions.

For example, to differentiate the expression d:=x^2*y+z*sin(y*z), we can use diff as follows:


which gives us


However if we had the function

        f:=(x,y,z) -> x^2*y+z*sin(y*z)

then using diff(f,z) gives us zero. In order to differentiate the function, we have to specify the arguments since Maple would treat f as a symbol in and of itself and return 0.

Thus the proper expression would be


A common problem among many users who use Maple to do differentiation of mathematical functions is in the evaluation of the derivatives of functions at points.

Say for example, we wished to evaluate the derivative of f at the points (x1,y1,z1). If we tried to define a new function in terms of the diff function, viz

        r:=(x,y,z) -> diff(f(x,y,z),z)

we would get the result

        r:=(x,y,z) -> diff(f(x,y,z),z)

and evaluating at the point (Pi,Pi,Pi) we would get


which is clearly wrong. This is due to the fact that in defining r as above results in delayed evaluation of the expression and substitues the values of the args before differentiation which results in Maple treating Pi as a symbolic variable with which to differentiate r with respect to.

The way to solve this would be to use the unapply operator, viz


and evaulating r(Pi,Pi,Pi), we obtain


which is indeed correct.

A few notes on unapply, adapted (and corrected) from the ?unapply help page:

  • The result of unapply(expr,x) is a functional operator. Applying this operator to x gives the original expression.
            unapply(expr,x)(x) ==> eval(expr)
  • Typically, for a function f(x):
            unapply(f(x),x) ==> f

    although this depends somewhat on the evaluation of f(x). A safer statement would be:

    	unapply('f'(x),x) ==> f
  • unapply should be used whenever it is desired to construct an operator using contents of variables or evaluated expressions.
  • unapply implements (almost) the lambda-expressions of lambda calculus. The scoping behaviour of unbound names is not the same in the lambda calculus.

See Also

IS&T Contributions

Documentation and information provided by IS&T staff members

Last Modified:

August 03, 2020

Get Help

Request help
from the Help Desk
Report a security incident
to the Security Team
olc-maple olc-maple Delete
maple maple Delete
function function Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
This product/service is:
Easy to use
Difficult to use

This article is:
Adaptavist Theme Builder (4.2.3) Powered by Atlassian Confluence 3.5.13, the Enterprise Wiki