Contents Previous Next Index

9 Basic Programming


You have used Maple interactively in the previous chapters, sequentially performing operations such as executing a single command. Because Maple has a complete programming language, you can also use sophisticated programming constructs.
In Maple, you can write programs called procedures, and save them in modules. These modules can be used and distributed in the same way as Maple packages.
Important: It is strongly recommended that you use the Worksheet mode and 1D Math input when programming or using programming commands. Hence, all input in this chapter is entered as 1D Math.

9.1 In This Chapter


Section

Topics

Flow Control  Basic programming constructs: ifthen statements and loops

•

Conditional Execution (if Statement)

•

Repetition (for Statement)


Iterative Commands  Specialized, efficient iterative commands

•

Adding and Multiplying Expressions

•

Selecting Expression Operands

•

Mapping a Command over a Set or List

•

Mapping a Binary Command over Two Lists or Vectors


Procedures  Maple programs

•

Defining and Running Simple Procedures

•

Procedure Return Values

•

Displaying Procedure Definitions

•

Displaying Maple Library Procedure Definitions


Programming in Documents  Display methods for Maple code






9.2 Flow Control


Two basic programming constructs in Maple are the if statement, which controls the conditional execution of statement sequences, and the for statement, which controls the repeated execution of a statement sequence (a loop).

Conditional Execution (if Statement)


You can specify that Maple perform an action only if a condition holds. You can also perform an action, from a set of many, depending on which conditions hold.
Using the if statement, you can execute one statement from a series of statements based on a boolean (true, false, or FAIL) condition. Maple tests each condition in order. When a condition is satisfied, Maple executes the corresponding statement, and then exits the if statement.

Syntax


The if statement has the following syntax.
The conditional expressions (conditional_expression1, conditional_expression2, ...) can be any boolean expression. You can construct boolean expressions using:
•

Relational operators  <, <=, =, >=, >, <>

•

Logical operators  and, or, xor, implies, not

•

Logical names  true, false, FAIL

The statement sequences (statement_sequence1, statement_sequence2, ..., statement_sequenceN) can be any sequence of Maple statements, including if statements.
The elif clauses are optional. You can specify any number of elif clauses.
The else clause is optional.


Simple if Statements


The simplest if statement has only one conditional expression.
If the conditional expression evaluates to true, the sequence of statements is executed. Otherwise, Maple immediately exits the if statement.
For example:
>

if not isprime(x) then
ifactor(x);
end if;

${}\left({3}\right){}{}\left({17}\right){}{}\left({23}\right)$


else Clause


In a simple if statement with an else clause, if the evaluation of the conditional expressions returns false or FAIL, Maple executes the statement sequence in the else clause.
For example:
>

if false then
"if statement";
else
"else statement";
end if;

${''else\; statement''}$


elif Clauses


In an if statement with elif clauses, Maple evaluates the conditional expressions in order until one returns true. Maple executes the corresponding statement sequence, and then exits the if statement. If no evaluation returns true, Maple exits the if statement.
The keyword elif stands for else if.
>

if not type(x, integer) then
printf("%a is not an integer.", x);
elif x >= 10 then
printf("%a is an integer with more than one digit.", x);
elif x >= 0 then
printf("%a is an integer with one digit.", x);
end if;

11 is an integer with more than one digit.
Order of elif Clauses: An elif clause's statement sequence is executed only if the evaluation of all previous conditional expressions returns false or FAIL, and the evaluation of its conditional expression returns true. This means that changing the order of elif clauses may change the behavior of the if statement.
In the following if statement, the elif clauses are in the wrong order.
>

if not(type(x, integer)) then
printf("%a is not an integer.", x);
elif x >= 0 then
printf("%a is an integer with one digit.", x);
elif x >= 10 then
printf("%a is an integer with more than one digit.", x);
end if;

11 is an integer with one digit.


elif and else Clauses


In an if statement with elif and else clauses, Maple evaluates the conditional expressions in order until one returns true. Maple executes the corresponding statement sequence, and then exits the if statement. If no evaluation returns true, Maple executes the statement sequence in the else clause.
>

if not type(x, integer) then
printf("%a is not an integer.", x);
elif x >= 10 then
printf("%a is an integer with more than one digit.", x);
elif x >= 0 then
printf("%a is an integer with one digit.", x);
else
printf("%a is a negative integer.", x);
end if;

12 is a negative integer.
For more information on the if statement, refer to the if help page.



Repetition (for Statement)


Using repetition statements, you can repeatedly execute a statement sequence. You can repeat the statements in three ways.
•

Until a counter variable value exceeds a limit (for/from loop)

•

For each operand of an expression (for/in loop)

•

Until a boolean condition does not hold (while loop or until loop)


for/from Loop


The for/from loop statement repeats a statement sequence until a counter variable value exceeds a limit.

Syntax


The for/from loop has the following syntax.
The behavior of the for/from loop is:
1.

Assign the initial value to the name counter.

2.

Compare the value of counter to the value of final. If the counter value exceeds the final value, exit the loop. (This is the loop bound test.)

3.

Execute the statement_sequence.

4.

Increment the counter value by the value of increment.

5.

Repeat steps 2 to 4, until Maple exits the loop.

The from, by, and to clauses are optional and can be in any order between the for clause and the do keyword. Table 9.1 lists the default clause values.
Table 9.1: Default Clause Values 
Clause

Default Value

from initial

1

by increment

1

to final

infinity (∞)




Examples


The following loop returns the square root of the integers 1 to 5 (inclusive).
>

$\mathbf{for}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}n\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{to}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}5\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\mathrm{evalf}\left(\mathrm{sqrt}\left(n\right)\right)\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{do}$

${1.}$
${1.414213562}$
${1.732050808}$
${2.}$
${2.236067977}$
When the value of the counter variable n is strictly greater than 5, Maple exits the loop.
${6}$
The previous loop is equivalent to the following for/from statement.
>

$\mathbf{for}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}n\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{to}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}5\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\mathrm{evalf}\left(\mathrm{sqrt}\left(n\right)\right)\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{do}$

${1.}$
${1.414213562}$
${1.732050808}$
${2.}$
${2.236067977}$
The by value can be negative. The loop repeats until the value of the counter variable is strictly less than the final value.
>

$\mathbf{for}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}n\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{from}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}10\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{by}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}1\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{to}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}3\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\mathbf{if}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathrm{isprime}\left(n\right)\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\mathrm{print}\left(n\right)\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\mathbf{end}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{if}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{do}$

${7}$
${5}$
${3}$
${2}$



for/in Loop


The for/in loop statement repeats a statement sequence for each component (operand) of an expression, for example, the elements of a list.

Syntax


The for/in loop has the following syntax.
The for clause must appear first.
The behavior of the for/in loop is:
1.

Assign the first operand of expression to the name variable.

2.

Execute the statement_sequence.

3.

Assign the next operand of expression to variable.

4.

Repeat steps 2 and 3 for each operand in expression. If there are no more operands, exit the loop. (This is the loop bound test.)



Example


The following loop returns a floatingpoint approximation to the sin function at the angles (measured in degree) in the list L.
>

L := [23.4, 87.2, 43.0, 99.7]:

>

for i in L do
evalf(sin(i*Pi/180));
end do;

${0.3971478907}$
${0.9988061374}$
${0.6819983602}$
${0.9857034690}$



while Loop and until Loop


The while loop repeats a statement sequence until a boolean expression does not hold.
The until loop also repeats a statement sequence until a boolean expression does not hold, but it tests the terminating condition at the end of each iteration of the loop, instead of the beginning.

Syntax


The while loop has the following syntax.
A while loop repeats until its boolean expression conditional_expression evaluates to false or FAIL. For more information on boolean expressions, see Conditional Execution (if Statement).
The until loop has the following syntax.
Note the difference in syntax: the until clause appears in place of end do


Example


Compare the following loops. In the first one, the terminating condition is met the first time it is tested, and the loop terminates immediately. In the second one, which uses the until clause, the body of the loop is performed before the condition is tested, and the loop is used.
>

while a mod 7 <>1 do
a:=a+1;
end do:

>

do
a:=a+1;
until a mod 7=1:

The following loop computes the digits of 872,349 in base 7 (in order of increasing significance).
>

while x > 0 do
irem(x, 7);
x := iquo(x, 7);
end do;

${2}$
${x}{:=}{124621}$
${0}$
${x}{:=}{17803}$
${2}$
${x}{:=}{2543}$
${2}$
${x}{:=}{363}$
${6}$
${x}{:=}{51}$
${2}$
${x}{:=}{7}$
${0}$
${x}{:=}{1}$
${1}$
${x}{:=}{0}$
To perform such conversions efficiently, use the convert/base command.
>

convert(872349, base, 7);

$\left[{2}{\,}{0}{\,}{2}{\,}{2}{\,}{6}{\,}{2}{\,}{0}{\,}{1}\right]$
For information on nonbase 10 numbers, see NonBase 10 Numbers.



General Loop Statements


You can include a while statement in a for/from or for/in loop.
The general for/from loop has the following syntax.
The general for/in loop has the following syntax.
After testing the loop bound condition at the beginning of each iteration of the for loop, Maple evaluates conditional_expression.
•

If conditional_expression evaluates to false or FAIL, Maple exits the loop.

•

If conditional_expression evaluates to true, Maple executes statement_sequence.

The equivalent general until loops have the same form but with end do replaced with until conditional_expression.


Infinite Loops


You can construct a loop for which there is no exit condition, for example, a while loop in which the conditional_expression always evaluates to true. This is called an infinite loop. Maple indefinitely executes an infinite loop unless it executes a break, quit, or return statement or you interrupt
the computation using the interrupt icon
in the toolbar (in worksheet versions).


Additional Information


For more information on the for statement and looping, refer to the do help page and the Loops section of the Maple Statements chapter of the Maple Programming Guide.




9.3 Iterative Commands


Maple has commands that perform common selection and repetition operations. These commands are more efficient than similar algorithms implemented using library commands. Table 9.2 lists the iterative commands.
Table 9.2: Iterative Commands 
Command

Description

seq

Create sequence

add

Compute numeric sum

mul

Compute numeric product

select

Return operands that satisfy a condition

remove

Return operands that do not satisfy a condition

selectremove

Return operands that satisfy a condition and separately return operands that do not satisfy a condition

map

Apply command to the operands of an expression

zip

Apply binary command to the operands of two lists or vectors



Creating a Sequence


The seq command creates a sequence of values by evaluating a specified expression over a range of index values or the operands of an expression. See Table 9.3.
Table 9.3: The seq Command 
Calling Sequence Syntax

Examples

seq(expression, name = initial .. final);

${{\ⅇ}}^{{}{2}}{\,}{{\ⅇ}}^{{}{1}}{\,}{1}$

seq(expression, name in expression);

>

seq(u, u in [Pi/4, Pi^2/2, 1/Pi]);

$\frac{{\mathrm{\pi}}}{{4}}{,}\frac{{{\mathrm{\pi}}}^{{2}}}{{2}}{,}\frac{{1}}{{\mathrm{\pi}}}$




Adding and Multiplying Expressions


The add and mul commands add and multiply sequences of expressions over a range of index values or the operands of an expression. See Table 9.4.
Table 9.4: The add and mul Commands 
Calling Sequence Syntax

Examples

add(expression, name = initial .. final);
mul(expression, name = initial .. final);

${{\ⅇ}}^{{2}}{\+}{{\ⅇ}}^{{3}}{\+}{{\ⅇ}}^{{4}}$
${3715891200}$

add(expression, name in expression);
mul(expression, name in expression);

>

add(u, u in [Pi/4, Pi/2, Pi]);

$\frac{{7}}{{4}}{}{\mathrm{\π}}$
>

mul(u, u in [Pi/4, Pi/2, Pi]);

$\frac{{1}}{{8}}{}{{\mathrm{\π}}}^{{3}}$


The endpoints of the index range (initial and final) in the add and mul calling sequence must evaluate to numeric constants. For information on symbolic sums and products, refer to the sum and product help pages.


Selecting Expression Operands


The select, remove, and selectremove commands apply a booleanvalued procedure or command to the operands of an expression. For information on operands, refer to the op help page.
•

The select command returns the operands for which the procedure or command returns true.

•

The remove command returns the operands for which the procedure or command returns false or FAIL.

•

The selectremove command returns two expressions of the same type as the input expression.
 The first consists of the operands for which the procedure or command returns true.
 The second consists of the operands for which the procedure or command returns false or FAIL.

The structure of the output is the same as the structure of the input. See Table 9.5.
For information on Maple procedures, see Procedures.
Table 9.5: The select, remove, and selectremove Commands 
Calling Sequence Syntax

Examples

select(proc_cmd, expression);

>

select(issqr, {198331, 889249, 11751184, 9857934});

$\left\{{889249}{\,}{11751184}\right\}$

remove(proc_cmd, expression);

>

remove(var > degree(var) > 3, 2*x^3*y  y^3*x + z );

${z}$

selectremove(proc_cmd, expression);

>

selectremove(x > evalb(x > round(x)), [sin(0.), sin(1.), sin(3.)]);

$\left[{0.1411200081}\right]{\,}\left[{0.}{\,}{0.8414709848}\right]$


For information on optional arguments to the selection commands, refer to the select help page.


Mapping a Command over a Set or List


The map command applies a name, procedure, or command to each element in a set or list. See Table 9.6.
Table 9.6: The map Command 
Calling Sequence Syntax

Examples

map(name_proc_cmd, expression);

$\left\{{f}{}\left({a}\right){\,}{f}{}\left({b}\right){\,}{f}{}\left({c}\right)\right\}$
>

map(u > int(cos(x), x = 0 .. u), [Pi/4, Pi/7, Pi/3.0]);

$\left[\frac{\sqrt{{2}}}{{2}}{\,}{\mathrm{cos}}{}\left(\frac{{5}{}{\mathrm{\pi}}}{{14}}\right){\,}{0.8660254037}\right]$


An alternative to the map command is to apply a function elementwise, using ~. For more information ~, see Applying an Operation or Function to All Elements in a List, Set, Table, Array, Matrix, or Vector. For information on mapping over the operands of other expressions, optional arguments to the map command, and other mapping commands, refer to the map help page.


Mapping a Binary Command over Two Lists or Vectors


The zip command applies a name or binary procedure or command componentwise to two lists or vectors.
By default, the length of the returned object is that of the shorter list or vector. If you specify a value as the (optional) fourth argument, it is used as the value of the missing elements of the shorter list or vector. In this case, the length of the return value is that of the longer list or vector. See Table 9.7.
Table 9.7: The zip Command 
Calling Sequence Syntax

Examples

zip(proc_cmd, a, b);
zip(proc_cmd, a, b, fill);

>

zip(f, [i, j], [k, l]);

$\left[{f}{}\left({i}{\,}{k}\right){\,}{f}{}\left({j}{\,}{l}\right)\right]$
>

zip(AiryAi, [1, 2], [0], 1);

${\left[{}\frac{{{3}}^{\raisebox{1ex}{${1}$}\!\left/ \!\raisebox{1ex}{${6}$}\right.}{}{\mathrm{\Gamma}}{}\left(\frac{{2}}{{3}}\right)}{{2}{}{\mathrm{\pi}}}{\,}{\mathrm{Ai}}{\u2033}{}\left({1}\right)\right]}}$
This is equivalent to $\left[\mathrm{AiryAi}\left(1\,0\right)\,\mathrm{AiryAi}\left(2\,1\right)\right]\.$


For more information on the zip command, refer to the zip help page.


Additional Information


For more information on looping commands, refer to the corresponding command help page.



9.4 Procedures


A Maple procedure is a program consisting of Maple statements. Using procedures, you can quickly execute the contained sequence of statements.

Defining and Running Simple Procedures


To define a procedure, enclose a sequence of statements between proc(...) and end proc statements. In general, you assign a procedure definition to a name.
The following procedure returns the square root of 2.
>

p := proc() sqrt(2); end proc;

${p}{:=}{\mathbf{proc}}\left({}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{sqrt}}{}\left({2}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$
Note: Maple returns the procedure definition.
To improve readability of procedures, it is recommended that you define a procedure using multiple lines, and indent the lines using space characters. To begin a new line (without evaluating the incomplete procedure definition), press Shift+Enter. When you have finished entering the procedure, press Enter to create the procedure.
For example:
>

p := proc()
sqrt(2);
end proc:

To run the procedure p, enter its name followed by parentheses (( )).
$\sqrt{{2}}$


Procedures with Inputs


You can define a procedure that accepts user input. In the parentheses of the proc statement, specify the parameter names. For multiple parameters, separate the names with commas.
>

geometric_mean := proc(x, y)
sqrt(x*y);
end proc:

When the user runs the procedure, the parameter names are replaced by the argument values.
>

geometric_mean(13, 17);

$\sqrt{{221}}$
>

geometric_mean(13.5, 17.1);

${15.19374871}$
For more information on writing procedures, including options and local and global variables, refer to the procedure help page.


Procedure Return Values


When you run a procedure, Maple returns only the last statement result value computed. Maple does not return the output for each statement in the procedure. It is irrelevant whether you use semicolons or colons as statement separators.
>

p := proc(a, b)
a + b;
a  b:
end proc:

${\mathrm{1}}$


Displaying Procedure Definitions


Unlike simple Maple objects, you cannot display the value of a procedure by entering its name.
${\mathrm{geometric\_mean}}$
You must evaluate the name of the procedure using the print (or eval) command.
${\mathbf{proc}}\left({x}{\,}{y}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{sqrt}}{}\left({y}{\*}{x}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$


Displaying Maple Library Procedure Definitions


Maple procedure definitions are a valuable learning tool. To learn how to program in Maple, it is recommended that you examine the procedures available in the Maple library.
By default, the print command returns only the proc and end proc statements and (if present) the description fields of a Maple procedure.
${\mathbf{proc}}\left({a}{\,}{b}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$
To display a Maple library procedure definition, first set the value of the interface verboseproc option to 2. Then reexecute the print calling sequence.
>

interface('verboseproc' = 2):

${\mathbf{proc}}\left({a}{\,}{b}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{remember}}{\,}{\mathrm{Copyright\; (c)\; 1990\; by\; the\; University\; of\; Waterloo.\; All\; rights\; reserved.}}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{q}{\,}{t}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{nargs}}{\=}{0}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{1}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{nargs}}{\=}{1}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{t}{\u2254}{\mathrm{expand}}{}\left({a}\right){\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{sign}}{}\left({t}\right){\*}{t}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{2}{<}{\mathrm{nargs}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{foldl}}{}\left({\mathrm{procname}}{\,}{\mathrm{args}}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{elif}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{type}}{}\left({a}{\,}{\'}{\mathrm{integer}}{\'}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{and}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{type}}{}\left({b}{\,}{\'}{\mathrm{integer}}{\'}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{ilcm}}{}\left({a}{\,}{b}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{else}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathrm{gcd}}{}\left({a}{\,}{b}{\,}{\'}{q}{\'}\right){\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{q}{\*}{b}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; if}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$


Modules


Maple procedures associate a sequence of commands with a single command. The module, a more complex programming structure, allows you to associate related procedures and data.
A key feature of modules is that they export variables. This means that the variables are available outside the module in which they are created. Most Maple packages are implemented as modules. The package commands are exports of the module.
For more information on modules, refer to the module help page.


Objects


Objects take the idea of associating data and procedures beyond what modules provide. With objects, multiple instances of a class of objects can be created. Each individual object can have its own data, yet share other values and procedures with the entire class objects. A well implemented class of objects can be used in Maple as naturally as a builtin Maple type.
For more information on objects, refer to the object help page.



9.5 Programming in Documents


To write Maple code, you could simply open a Maple worksheet and start typing. However, if you want to create a readable document with the code interspersed or hidden, there are several options available: code edit regions and start up code. Both these features use a code editor which has features such as syntax highlighting and line numbers.

Code Edit Region


The code edit region allows you to program in one contained region, in a natural way. Features include the ability to press Enter for line breaking and indentation preservation. Figure 9.1 shows the expanded code edit region.
To insert a new code edit region into your worksheet:
•

From the Insert menu, select Code Edit Region.


Figure 9.1: Code Edit Region



To execute the code within this region, click anywhere in this region, then click the Execute Selection icon (
) on the toolbar. (Alternatively, from the Evaluate menu, select Execute Code.)
You can hide the code in a code edit region by minimizing the region. To minimize, select View → Collapse Code Edit Region. When the region is minimized, an icon appears with the first line of the code written next to it. It is recommended that you make the first line a comment describing the program or programs contained in the region. See Figure 9.2.

Figure 9.2: Collapsed Code Edit Region



To reexecute the code in the region while it is collapsed, click this icon.
For more information, refer to the CodeEditRegion help page.


Startup Code


Startup code allows you to define commands and procedures that are executed each time the document is opened and after restart is called. This code is completely hidden to others reading the document. For example, use this region to define procedures that will be used throughout the document code but that would take up space and distract readers from the message of the document.
To enter startup code for a document:
1.

From the Edit menu, select Startup Code. Alternatively, click the startup code icon in the toolbar,
.

2.

Enter commands to be run each time the worksheet is opened or restart is called.

3.

To check the syntax of the entered code while entering your Maple commands or before closing the editor, from the Edit menu. select Check Syntax Now.

Note: You can also check the Check Syntax Automatically option to enable continuous syntax checking. It is recommended that you check the syntax before saving so that your startup code does not prevent Maple from opening successfully.
4.

To save the contents, from the File menu, select Save Code. Alternatively, click the save icon,
.


Figure 9.3: Startup Code Editor



For more information, refer to the startupcode help page.



9.6 Additional Information


The Maple Programming Guide provides an indepth reference for programming in Maple. Topics include statements, data structures, procedures, packages, and debugging your code.
•

Access via the help system. From the Table of Contents, select Manuals>Programming Guide.

The Programming Guide is also available as a PDF on the Maplesoft website.
http://www.maplesoft.com/documentation_center


Contents Previous Next Index
