Application Center - Maplesoft

App Preview:

Parameter Processing

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

Learn about Maple
Download Application


 

ImageParameter Processing? Maplesoft, a division of Waterloo Maple Inc., 2008 

Introduction 

Maple provides an extensive set of parameter processing tools for procedures, including type declarations, optional arguments with default values, and keyword processing.  This Tips and Techniques provides an introduction to various parameter processing tools which can be used to make your procedures more robust and easier to create and maintain. More information can be found in the help system, starting from the ?parameter page. 

Type Declarations of Parameters 

It is good practice to give your parameters meaningful names. Doing so reduces coding errors.  Consider Typesetting:-mrow(Typesetting:-msup(Typesetting:-mi( vs. Typesetting:-mrow(Typesetting:-msup(Typesetting:-mi( - the first expressions tells you nothing, but the second is clearly wrong. Meaningful names also make it easier for users to call your procedure correctly.  

> p2 := proc(base, power)
   base^power;
end proc:
 

> p2(2,3);
 

8 (2.1)
 

 

But meaningful names aren't enough. You'll still run into problems if the arguments are not what you are expecting: 

> p2([a], "string");
 

Error, (in p2) non-algebraic exponent
 

>
 

 

In this case, you see a (not terribly informative) error message. But you can run into problems even when no error messages are generated, because your procedure is creating unexpected results. For example, you may be assuming that the result of Typesetting:-mrow(Typesetting:-mi(is an integer, but sometimes: 

> p2(2,-3);
 

`/`(1, 8) (2.2)
 

 

Any code you write that assumes the result is an integer will give unexpected results. 

 

To avoid these problems, you can do type-checking on the arguments. Rather than writing extra code to do this, you can build the type expectations directly into the procedure definition using this syntax: 

parameterName :: parameterType 

 

> p3 := proc(base::integer, power::nonnegint)
   base^power;
end proc:
 

> p3(2,3);
 

8 (2.3)
 

> p3([a], "string");
 

Error, invalid input: p3 expects its 1st argument, base, to be of type integer, but received [a]
 

> p3(-2, -2);
 

Error, invalid input: p3 expects its 2nd argument, power, to be of type nonnegint, but received -2
 

 

Notice that the error messages are much more meaningful.  Maple will check each argument in order, and generate an error message the first time a problem is encountered. 

 

Advanced Type Declarations 

You are not limited to simple type declarations. Maple has structured types available which allow you to build up quite precise declarations, including the ability to specify mathematical properties, not just datastructures. In the following example, the procedure takes a 3-element list where the first element is an integer, the second is a prime number, and there are no restrictions on the the third element: 

> p4 := proc(L::[integer, prime, anything])
   L;
end proc:
 

> p4([-3, 7, 3*I]);
 

[-3, 7, `*`(3, `*`(I))] (2.1.1)
 

> p4([-3, 8, x]);
 

Error, invalid input: p4 expects its 1st argument, L, to be of type [integer, prime, anything], but received [-3, 8, x]
 

>
 

Tip: For more information on types in Maple, see ?type and ?type,structure

 

Undeclared Parameters 

In Maple, there is actually no need to declare any parameters in your procedure. You lose the benefits of meaningful names and type-checking, but it can be done. You can get at the arguments using the symbol _passed

 

> p := proc()  
  [_passed];
end proc:
 

> p(a,b,c);
 

[a, b, c] (2.2.1)
 

 

The _passed mechanism can be used to access any extra arguments passed to the procedure beyond the declared ones, though generally it is better to use other parameter processing tools, such as optional parameters,  instead. 

 

Tip: The symbols _passed and _npassed (number of arguments passed to this procedure) used to be called args and nargs. For backwards compatibility, the old names still work. 

 

Optional Parameters 

It is possible to declare a parameter to be optional. This can be useful when you want to be able to specify more about the solution, such as which algorithm to use or the form of output, but don't want to insist these items be provided.  For instance, the Maple plot command has many optional parameters which allow you to control items like color, grid size and line style. 

 

In Maple, you declare a parameter to be optional by specifying a default value, using the following syntax: 

parameterName :: parameterType := defaultValue  

 

For example, the following procedure calculates Typesetting:-mrow(Typesetting:-mi(. If Typesetting:-mrow(Typesetting:-mi( is not supplied, it defaults to the value Typesetting:-mrow(Typesetting:-mn(

 

> p5 :=proc(base::integer, power::nonnegint, multiplier::integer:=1)
   multiplier*base^power;
end proc:
 

> p5(2,3, 2);
 

16 (3.1)
 

> p5(2,3);
 

8 (3.2)
 

>
 

 

Tip: The default value does not need to be of the same type as the the parameter; it can have any value.  This can sometimes be useful, as it provides a way for the procedure to test  if  value was supplied by the user or not. 

Keyword Parameters 

You can define your own keyword parameters for a procedure, similar to the "color = " parameter in the plot commands.Keyword parameters can appear in any order in the procedure call, or can be left out entirely. 

 

The syntax is almost identical to the optional parameter declarations, except that the declarations are enclosed in set brackets: 

{ ... keyword :: parameterType := defaultValue ... }  

 

The following procedure returns a list by default, but you can ask it to return a vector instead. 

 

> p6 := proc(start::integer, length::nonnegint, {vector::truefalse := false})
   local result, i:
   result := [ seq(i, i=start..(start+length-1))];
   if vector then result := convert(result, Vector) end if;
   result;
end proc:
 

> p6(3,10);
 

[3, 4, 5, 6, 7, 8, 9, 10, 11, 12] (4.1)
 

> p6(1,15, vector=true);
 

Vector[column](%id = 151327664) (4.2)
 

 

The parameterType is optional. If a type is declared, then an exception is raised if the keyword value does not have the correct type. 

Advanced Parameter Processing 

This article touches briefly on key aspects of parameter processing in Maple procedures. However, there are more options available, including: 

  • Alternate spelling of keywords
 

  • Setting default values that depend on the values of other parameters
 

  • End of parameters marker
 

  • Different approaches for handling optional parameters
 

  • Preventing the evaluation of the argument
 

 

See ?parameter for more details on these and other options. 

Legal Notice: The copyright for this application is owned by Maplesoft. The application is intended to demonstrate the use of Maple to solve a particular problem. It has been made available for product evaluation purposes only and may not be used in any other context without the express permission of Maplesoft.
 

Image