Contents Previous Next Index
|
5 Maple Statements
|
|
|
5.2 Introduction
|
|
A statement is a single complete piece of code that Maple can execute. There are many types of statements in Maple, including expression statements, assignment statements, selection statements (if ... then), repetition statements (loops), and program instructions (quit, save, read).
A statement differs from an expression in that it is normally evaluated for effect, rather than for its value. Most statements that do not consist of a single expression are formed so as to have a side effect.
|
|
5.3 Statement Separators
|
|
Statements in Maple must be terminated with a semicolon (;) or a colon (:).
Statements can be run in Maple one at a time, or multiple statements can be run on one line. If multiple statements are run on one line, the statements must be separated by a statement separator, either a semicolon (;) or a colon (:).
At the top level, the output of a statement that ends with a colon is hidden.
| (1) |
Note: In the standard interface, for input in 2-D math, the semicolon at the end of a statement can be omitted.
|
|
5.4 Expression Statements
|
|
The simplest kind of statement in Maple is the expression statement. It consists of an arbitrary expression, whose evaluation constitutes the effect of the statement.
| (2) |
| (3) |
>
|
int( sin( Pi - x ), x );
|
| (4) |
|
|
5.5 Assignments
|
|
Assignment statements allow you to associate a value or expression with a name. The assignment statement has the general form
Evaluating the assignment associates the value on the right-hand side of the assignment with the name on the left-hand side. After the assignment has taken effect, the result is the associated value when the assigned name is evaluated.
Here, the name a has no assigned value, so it evaluates to itself.
| (5) |
The following assignment statement associates the value 2 / 3 with the name a.
| (6) |
Subsequent evaluation of the name a results in the assigned value 2 / 3.
| (7) |
| (8) |
Associate the symbolic expression Pi / 2 with the name b by executing the following assignment statement.
| (9) |
Subsequently, the assigned value of b is used whenever the name b appears in an expression.
| (10) |
In this expression, the assigned value Pi / 2 of the name b is substituted to yield the expression sin( Pi / 2 ), and then the value of the procedure sin at this expression is computed, resulting in the overall value 1 for the expression.
|
Multiple Assignment
|
|
You can perform several assignments in a single statement, known as a multiple assignment. This has the general form
(lhs1, lhs2, ..., lhsN) := (rhs1, rhs2, ..., rhsN)
|
|
|
The parentheses on the right- and left-hand sides of the assignment are not required, but are considered good practice.
For example, the multiple assignment statement
>
|
(x, y, z) := ( sin( t ), cos( t ), tan( t ) );
|
| (11) |
establishes assigned values for all three names x, y, and z.
| (12) |
| (13) |
| (14) |
The number of components on each side of the assignment operator := must be the same.
A common idiom is to use a multiple (double) assignment to swap the values of two variables without introducing an additional temporary variable.
| (15) |
| (16) |
| (17) |
| (18) |
Note that using the swap idiom with unassigned names will lead to an infinite recursion.
| (19) |
Evaluating u or v (full evaluation) produces an error. If you evaluate one level at a time using eval(u, i), you can see what happens.
>
|
seq( eval( u, i ), i = 1 .. 10 );
|
| (20) |
>
|
seq( eval( v, i ), i = 1 .. 10 );
|
| (21) |
|
|
|
5.6 Flow Control
|
|
A number of Maple statements are used to direct the flow of control in a program; that is, the sequence in which the various statements of the program are run.
|
Sequencing
|
|
The simplest form of a Maple program is a sequence of zero or more statements, separated either by semicolons or colons. A sequence of statements is run in the order in which they are entered.
For example, running these three statements
| (22) |
| (23) |
| (24) |
executes the assignment to the name a, then the assignment to the name b is executed and, finally, the value of the expression sin( a + b ) is computed.
The flow of control in a Maple program consisting of a sequence of statements moves from one statement to the next, in order.
Many Maple statements are compound statements that contain statement sequences as constituents.
|
|
Branching
|
|
The simplest form of flow control is a branching, or if statement. Basically, an if statement has the syntax
if condition then
|
statseq
|
end if
|
|
|
in which condition is a Boolean-valued expression (that is, one which evaluates to one of the values true, FAIL, or false), and statseq is a (possibly empty) sequence of Maple statements, often called the body of the if statement.
The effect of an if statement is to divert the flow of control, under the right conditions, to the body of the statement. If the condition expression evaluates to true, the flow of control moves into the body of the if statement. Otherwise, if the condition expression evaluates to FAIL or false, Maple exits the if statement and the flow of control continues at the statement (if any) following the if statement.
>
|
if 2 < 3 then
print( "HELLO" )
end if;
|
| (25) |
>
|
if 2 > 3 then
print( "GOODBYE" )
end if;
|
More generally, an if statement has the syntax
if condition then
|
consequent
|
else
|
alternative
|
end if
|
|
|
Here, consequent and alternative are statement sequences. If the condition expression evaluates to true, the consequent branch of the if statement is executed. Otherwise, the alternative branch is executed.
>
|
if 2 < 3 then
print( "CONSEQUENT" )
else
print( "ALTERNATIVE" )
end if;
|
| (26) |
>
|
if 2 > 3 then
print( "CONSEQUENT" )
else
print( "ALTERNATIVE" )
end if;
|
| (27) |
The most general form of an if statement can have several conditions, corresponding consequents, and an optional alternative branch. This general form has the syntax:
if condition1 then
|
consequent1
|
elif condition2 then
|
consequent2
|
....
|
else
|
alternative
|
end if
|
|
|
in which there can be any number of branches preceded by elif. The effect of this general form of the if statement is to divert the flow of control into the first branch whose conditional expression evaluates to true. This means that the order of the elif branches can affect the behavior of the if statement.
The branch introduced by else is optional. If it is present, and none of the earlier condition expressions evaluates to true, then control flows into the else branch. If it is not present, and none of the earlier condition expressions evaluates to true, then the flow of execution continues with the first statement following the entire if statement.
>
|
if 2 > 3 then
print( "CONSEQUENT1" )
elif 3 > 4 then
print( "CONSEQUENT2" )
elif 1 < 5 then
print( "CONSEQUENT3" )
elif 2 < 5 then
print( "CONSEQUENT4" )
else
print( "ALTERNATIVE" )
end if;
|
| (28) |
>
|
if 2 > 3 then
print( "CONSEQUENT1" )
elif 3 > 4 then
print( "CONSEQUENT2" )
elif 1 > 5 then
print( "CONSEQUENT3" )
elif 2 > 5 then
print( "CONSEQUENT4" )
else
print( "ALTERNATIVE" )
end if;
|
| (29) |
The else branch, if present, must appear last.
An if statement can appear at the top level, as in the examples shown above, but is most commonly used within a procedure or module definition.
A typical use of the if statement is to control the flow of execution inside a procedure, depending on information coming from the arguments passed to it.
>
|
p := proc( expr )
if type( expr, 'numeric' ) then
sin( 2 * expr )
elif type( expr, { '`+`', '`*`' } ) then
map( thisproc, _passed )
else
'procname'( _passed )
end if
end proc:
|
| (30) |
| (31) |
| (32) |
In this example, the procedure p uses the type command to examine its argument expr. If the argument is numeric, then it computes the value as sin( 2 * expr ). Otherwise, if the argument is either a sum or a product, the procedure maps itself over the operands of the expression. Otherwise, the procedure returns unevaluated.
|
The `if` Command
|
|
There is an operator form of branching that can be used within an expression. In this form, if is always called with three arguments. The if operator has the following syntax:
`if`( condition, consequent, alternative )
|
|
|
The first argument condition is a Boolean-valued expression. The second argument consequent is an expression to evaluate if the first argument evaluates to the value true. The third argument is an expression to evaluate if the first argument evaluates to either false or FAIL.
| (33) |
| (34) |
Note that the name if must be enclosed in name (left) quotes in this form.
The if command evaluates only one of its second and third arguments, determined based on the value of the first argument. The other argument is not evaluated.
The value of the if command (as opposed to the statement form) is that you can embed it within a larger expression.
>
|
sin( `if`( a > 0, Pi / 2, -Pi / 2 ) );
|
| (35) |
However, the if command is much more limited than the if statement. The consequent and alternative must be single expressions, and there is nothing corresponding to the elif parts of the statement form.
|
|
|
Loops
|
|
To cause a statement, or sequence of statements, to be run more than once, use a loop statement. Maple has a general and flexible loop statement.
The simplest loop has the form do end do. This loop does not perform any tasks.
A loop statement has one of the following general forms.
for var from start to finish by increment while condition do
|
statseq
|
end do
|
|
|
for var in container while condition do
|
statseq
|
end do
|
|
|
The first line in each of these forms is called the loop header or, more formally, the loop control clause. The statseq part of the loop is a (possibly empty) sequence of statements, referred to as the body of the loop.
Each clause that occurs before the keyword do in the loop header is optional.
Since most of the examples below are infinite loops; you must interrupt the Maple computation to terminate the loop. For more information, see Interrupting a Maple Computation.
If more than one of the optional clauses appears in the loop header, they may appear in any order.
|
While Loops
|
|
One simple kind of terminating loop is the while loop.
while condition do
|
statseq
|
end do;
|
|
|
The loop header of a while loop involves only a single termination condition introduced by the keyword while. The loop repeats the statement sequence statseq until the Boolean-valued expression condition does not hold.
In this example, a loop counts the number of primes whose square is less than 1000.
>
|
count := 0:
p := 2:
while p^2 < 1000 do
count := 1 + count;
p := nextprime( p )
end do:
count;
|
| (36) |
This example uses the nextprime command, which returns the least prime greater than its argument. The name count is given the initial value 0, and the name p, which is used to store the current prime, is initially set to 2. The loop condition is the expression p^2 < 1000, appearing after the keyword while and before the keyword do. This condition is evaluated at the beginning of each iteration of the loop. If the condition evaluates to true, the body of the loop is executed. If the condition evaluates to false or FAIL, the code continues to execute at the next statement following the loop statement.
If the condition expression evaluates to a value other than true, false or FAIL, an exception is raised.
For more information on Boolean expressions, see Boolean and Relational Expressions.
|
|
Counted Loops
|
|
You can use a loop to repeatedly execute a sequence of statements a fixed number of times. These loops use the from and to clauses.
>
|
from 1 to 3 do print( "HELLO" ) end do;
|
| (37) |
or equivalently
>
|
to 3 do print( "HELLO" ) end do;
|
| (38) |
If the from clause is omitted, the default value of 1 is used.
|
|
Inductive Loops
|
|
The most common kind of loop is an inductive loop which is similar to a counted loop, but uses an induction variable whose value changes at each iteration of the loop. This is a particular kind of for loop with the general form
for var from start to finish by increment do
|
statseq
|
end do;
|
|
|
The default value for start is 1, for finish is infinity, and for increment is 1.
>
|
for i to 3 do
print( i )
end do;
|
| (39) |
This loop performs the following tasks:
Maple assigns i the (default) value 1 since a starting value was not specified.
Because 1 is less than 3, Maple executes the statement in the body of the loop, in this case, printing the value of i.
Then i is incremented by 1 and tested again.
The loop executes until i>3. In this case, when the loop terminates, the final value of i is 4.
| (40) |
>
|
for i from 7 to 2 by -2 do
print( i )
end do;
|
| (41) |
Loop control parameters (start, finish, and increment) do not need to be integers.
>
|
for i from 0.2 to 0.7 by 0.25 do
print( i )
end do;
|
| (42) |
In addition to iterating over a numeric range, you can iterate over a range of characters. In this case, you must specify both the initial value start and the final value finish for the induction variable. Furthermore, the value of increment must be an integer.
>
|
for i from "a" to "g" by 2 do
print( i )
end do;
|
| (43) |
|
|
Iterating over a Data Structure
|
|
An alternative form of the loop statement allows you to iterate over the operands of an expression (often, a data structure such as a set or list).
for var in expr do
|
statseq
|
end do;
|
|
|
The induction variable var takes on, successively, the operands of the expression expr. There are a few exceptions. First, if expr is an expression sequence, it does not have operands as such, but the induction variable var iterates over the operands of the list [ expr ]. If expr is a table, the loop iterates over [entries]( expr ). (For more information on entries, see Extracting Data.) The order in which these entries are visited is not specified and may vary from one session to another. Finally, if expr is an rtable, the loop iterates over the entries of expr, but the order of the iteration is not specified.
>
|
for i in [ 1, 2, 3 ] do
print( i )
end do;
|
| (44) |
Note that there is a difference between the loop above and the seemingly equivalent loop
>
|
for i from 1 to 3 do
print( i )
end do;
|
| (45) |
The difference is the value of the induction variable i at the end of the loop. To see this, evaluate the induction variable i immediately after running the loop to display its value.
>
|
for i in [ 1, 2, 3 ] do end do: i;
|
| (46) |
>
|
for i from 1 to 3 do end do: i;
|
| (47) |
|
|
|
Looping Commands
|
|
Maple provides commands to create some commonly used types of loops. These commands are generally meant to build expressions without creating many intermediate expressions.
|
The map Command
|
|
The map command applies a function to every element of an aggregate object. The simplest form of the map command is
where f is a function and x is an expression. The map command replaces each operand elem of the expression x with f(elem).
For tables and Arrays (or other rtables), the function is applied to each entry.
| (48) |
Given a list of integers, you can create a list of their absolute values and of their squares by using the map command.
>
|
L := [ -1, 2, -3, -4, 5 ];
|
| (49) |
| (50) |
| (51) |
The general syntax of the map command is
where f is a function, x is any expression, and y1, ..., yn are expressions. The action of map is to replace each operand of x such that the ith operand of x is replaced by f(op(i,x), y1, ..., yn).
| (52) |
>
|
map( (x,y) -> x^2+y, L, 1);
|
| (53) |
For more information and examples, see Basic Data Structures.
|
|
The select, remove, and selectremove Commands
|
|
The select, remove, and selectremove commands also operate on the operands of an expression. The select command returns the operands for which the specified Boolean-valued function returns true. The remove command returns the operands for which the specified Boolean-valued function returns false. The selectremove command returns two objects: the operands for which the specified Boolean-valued function returns true and the operands for which the specified Boolean-valued function returns false. The select, remove, and selectremove commands have the same syntax as the map command.
>
|
X := 2*x*y^2 - 3*y^4*z + 3*z*w + 2*y^3 - z^2*w*y;
|
| (54) |
| (55) |
>
|
remove( x -> degree(x)>3, X );
|
| (56) |
For more information on these commands, see Other Data Structure Operations or refer to the select help page.
|
|
The zip Command
|
|
The zip command merges two lists or Arrays and then applies a binary function. The zip command has two forms
zip(f, u, v)
|
zip(f, u, v, d)
|
|
|
where f is a binary function, u and v are both lists or rtables, and d is any value. The zip command takes each pair of operands u[i], v[i], and creates a new list or vector from f(u[i], v[i]).
>
|
zip( (x,y) -> x || y, [a,b,c,d,e,f], [1,2,3,4,5,6] );
|
| (57) |
If the lists or vectors are not the same length, the length of the result depends on whether you provide the argument d.
If you do not specify d, the length of the result is the same as the length of the smaller list or vector.
>
|
zip( (x,y) -> x+y, [a,b,c,d,e,f], [1,2,3] );
|
| (58) |
If d is specified, the length of the result of the zip command is the same as the length of the longer list or vector. Maple replaces the missing value(s) with d.
>
|
zip( (x,y) -> x+y, [a,b,c,d,e,f], [1,2,3], xi );
|
| (59) |
|
|
|
Non-Local Flow Control
|
|
There are a couple of statements that are generally used in procedures to control how execution of the procedure ends: return and error. For more information on these statements, see Procedures.
|
The return Statement
|
|
The return statement causes an immediate return to the point where the current procedure was invoked.
In Command-line Maple, the return statement causes an error if it is run at the top level: Error, return out of context. In the Standard worksheet interface, return can be used at the top level in conjunction with DocumentTools:-RunWorksheet.
|
|
The error Statement and Exception Handling
|
|
The error statement raises an exception and interrupts the execution of the current statement. If the exception is not caught (see the following section), a message is printed indicating that an error occurred.
error string
|
error string, parameter1, parameter2, ...
|
|
|
In the first case, an error message is given as a string.
In the second case, string contains several placeholders of the form %n or %-n, where n is a positive integer, to include the provided parameters in the message.
The placeholder %n is replaced by the nth parameter given. The placeholder %-n is replaced by the ordinal form of the nth parameter, which should evaluate to an integer. The special placeholder %0 is replaced with the sequence of all parameters separated by commas and spaces.
| (60) |
>
|
error "my error in %1 of the %-2 kind", x, n;
|
|
Trapping Errors
|
|
The try statement is a mechanism for executing procedure statements in a controlled environment so that if an error occurs, it does not immediately terminate the procedure. The try statement has the following syntax
try tryStatSeq
|
catch catchStrings : catchStatSeq
|
finally finalStatSeq
|
end try
|
|
|
This statement can include several catch clauses. The finally clause is optional.
If procedure execution enters a try...catch block, the tryStatSeq is executed. If no exceptions occur during the execution of tryStatSeq, the finalStatSeq in the finally clause (if present) is executed. Execution then continues with the statement after end try.
If an exception occurs during the execution of tryStatSeq, execution of tryStatSeq terminates immediately. The exception object corresponding to the exception is compared against each catchString. Any number of catch clauses can be provided, and each can have any number of catchStrings separated by commas. Alternatively, a catch clause does not need to have a catch string. Any given catchString (or a catch clause without one) can appear only once in a try...end try construct.
If a matching catch clause is found, or the catch clause contains no catchStrings, the catchStatSeq of that catch clause is executed, and the exception is considered to have been caught. If no matching catch clause is found, the exception is considered not caught, and is re-raised outside of the try block.
When Maple searches for a matching catch clause, the following definition of "matching" is used.
•
|
Neither the exception object nor the catchStrings are evaluated (the exception object has already been evaluated by the error statement that produced it).
|
•
|
The catchStrings are considered to be prefixes of the exception object's msgString. If a catchString has n characters, only the first n characters of the msgString need to match the catchString. This permits the definition of classes of exceptions.
|
•
|
A catch clause without a catchString matches any exception.
|
•
|
The "result" of a try statement (the value that % returns if it is evaluated immediately after execution of the try statement) is the result of the last statement executed in the try statement.
|
A catchStatSeq can contain an error statement with no arguments, which also re-raises the exception. When an exception is re-raised, a new exception object is created that records the current procedure name, and the message and parameters from the original exception.
Normally, the finalStatSeq of the finally clause, if there is one, is always executed before control leaves the try statement. This is true in the case that an exception occurs, independent of whether it is caught or whether another exception occurs in the catch clause.
This is true even if a catchStatSeq re-raises the exception, raises a new one, or executes a return, break, or next statement.
Under certain abnormal circumstances, the finalStatSeq is not executed:
•
|
If an exception is caught in an interactive debugger session and you exit the debugger
|
•
|
If one of the following untrappable exceptions occurs, the exception is not caught, and finalStatSeq is not executed:
|
1.
|
A computation timed out. This exception can only be caught by the timelimit command, which raises a "time expired" exception that can be caught. For more information on the timelimit command, refer to the timelimit help page.
|
2.
|
A computation has been interrupted. In other words, you pressed Ctrl+C, Break, or equivalent.
|
3.
|
Internal system error. This exception indicates a bug in Maple itself.
|
4.
|
ASSERT or local variable type assertion failure. Assertion failures cannot be trapped because they indicate a coding error, not an algorithmic failure.
|
5.
|
Stack overflow. If a stack overflow occurs, there is generally not enough stack space to perform tasks such as running cleanup code.
|
If an exception occurs during the execution of a catchStatSeq or the finalStatSeq, it is treated in the same way as if it occurred outside the try...end try statement.
|
|
Example 1
|
|
A useful application of the try and error statements is to stop a large computation as quickly and cleanly as possible. For example, suppose that you are trying to compute an integral by using one of several methods, and in the middle of the first method, you determine that it will not succeed. You want to stop that method and try another one. The following code implements this example.
>
|
try
result := MethodA(f,x)
catch "FAIL":
result := MethodB(f,x)
end try:
|
MethodA can stop its computation at any time by executing the statement error "FAIL". The catch clause catches that exception, and proceeds to try MethodB. If any other error occurs during the execution of MethodA, or if an error occurs during the execution of MethodB, it is not caught.
|
|
Example 2
|
|
Another useful application of the try statement is to ensure that certain resources are made available when you are done with them, regardless of whether anything went wrong while you were using them.
Use the following code to access the Maple I/O facilities to read the lines of a file and process them in some way.
>
|
f := fopen("myfile",READ,TEXT):
try
line := readline(f);
while line < 0 do
ProcessContentsOfLine(line);
line := readline(f)
end do
finally
fclose(f)
end try:
|
In this example, if any exception occurs while reading or processing the lines of the file, it is not caught because there is no catch clause. However, fclose(f) is executed before execution leaves the try statement, regardless of whether there was an exception.
The next example uses both catch and finally clauses to write to a file instead of reading from one.
>
|
f := fopen("myfile",WRITE,TEXT):
try
for i to 100 do
fprintf(f,"Result %d is %q\n",i,ComputeSomething(i))
end do
catch:
fprintf(f,"Something went wrong: %q\n",lastexception);
error
finally
fclose(f)
end try:
|
If an exception occurs, it is caught with the catch clause that has no catchString, and the exception object is written into the file. The exception is re-raised by executing the error statement with no msgString. In all cases, the file is closed by executing fclose(f) in the finally clause.
|
|
|
|
|
5.7 The use Statement
|
|
The use statement specifies local bindings of names, module exports, and operator overriding. It has the following syntax:
use exprseq in
|
stateseq
|
end use
|
|
|
where stateseq is a sequence of statements and exprseq is a sequence of expressions.
The expressions can be any of the following.
•
|
equation of the form name = expression
|
•
|
module member selection m:-e, which is equivalent to the equation e = m:-e
|
•
|
module expression m, which is equivalent to the equations e = m:-e for all exports e of m.
|
For more information about modules and member selection, see Programming with Modules.
Running a use statement executes the body of the statement. Each occurrence of a name that appears on the left side of any of the binding equations is replaced by the right side of the corresponding equation.
For example,
>
|
use f = sin, g = cos in
f( x )^2 + g( x )^2
end use;
|
| (61) |
The following example establishes local bindings for all of the exports of the StringTools package.
>
|
use StringTools in
s := Random( 10, 'lower' );
Reverse( s )
end use;
|
| (62) |
Among these are the names Random and Reverse. Without the use statement enclosing them, the two statements would have to be written using fully qualified names.
>
|
s := StringTools:-Random( 10, 'lower' );
|
| (63) |
>
|
StringTools:-Reverse( s );
|
| (64) |
You can employ the use statement to establish general name bindings.
>
|
use a = 2, b = 3, c = 4 in
a + b + c
end use;
|
| (65) |
(This is useful when the names bound are used many times within the body of the use statement.)
The use statement is unique in Maple. It is the only Maple statement that is resolved during the automatic simplification process rather than during the evaluation process. To see this, consider the following simple procedure.
>
|
p := proc( x, y )
use a = x + y, b = x * y in
a / b
end use
end proc;
|
| (66) |
Note that there is no use statement in the procedure after it has been processed, and a and b in the body have been replaced by the values on the right-hand side of the binding equations. To see that this processing occurred during automatic simplification (of the procedure definition), enclose the procedure definition in unevaluation quotes.
>
|
p := 'proc( x, y )
use a = x + y, b = x * y in
a / b
end use
end proc';
|
| (67) |
use statements can be nested.
>
|
use a = 2, b = 4 in
use x = 3, y = 5 in
a * x + b * y
end use
end use;
|
| (68) |
If a name is bound in use statements at two different levels of nesting, the innermost binding visible at the level of an expression is used.
>
|
use a = 2 in
a^2;
use a = 3 in
a^2
end use
end use;
|
| (69) |
In the following example, the inner binding of the value 3 to the name a takes precedence, so the value of the expression a + b (and therefore the entire statement) is the number 6. The inner binding of 3 to a has an effect only within the body of the inner use statement. Once the execution has exited the inner use statement, the binding of 2 to a is restored.
>
|
use a = 2, b = 3 in
# here a is bound to 2 and b to 3
use a = 3 in
# here, b is still bound to 3, but a is bound to 3
a + b
end use;
# binding of a to 2 is restored
a + b
end use;
|
| (70) |
The use statement also allows you to rebind Maple operators to override their default behavior. The following is an example in which addition and multiplication are replaced by nonstandard meanings.
>
|
use `+` = ((x,y) -> (x+y) mod 3), `*` = ((x,y) -> (x*y) mod 3) in
1 + 2 * 4
end use;
|
| (71) |
The following operators can have their default behavior overridden by a use statement.
Table 5.1: Operators That Can Be Rebound |
@
|
@@
|
%
|
%%
|
%%%
|
.
|
+
|
*
|
-
|
/
|
mod
|
^
|
!
|
union
|
minus
|
intersect
|
subset
|
in
|
$
|
and
|
or
|
not
|
xor
|
implies
|
=
|
<>
|
<
|
<=
|
assuming
|
<|>
|
<,>
|
[]
|
{}
|
?()
|
?[]
|
~
|
|
Notes:
•
|
The following operators cannot be rebound: concatenation operator (||), member selection operator (:-), type operator (::), range (..), comma (,), functional operator (->), and the assignment operator (:=). The relational operators > and >= can be rebound, but not independently of < and <=, respectively.
|
•
|
All of the element-wise operators are processed through the element-wise operator (~).
|
•
|
The operators - and / are treated as unary operators (that represent negation and inversion, respectively). Subtraction is represented internally in Maple by forming addition and negation: a - b = a + (-b). Division is formed in a similar way. Therefore, it is not necessary to override the binary infix operators - and /.
|
Note also that an expression such as a + b + c + d is treated as though it were parenthesized as ((a + b) + c) + d, so that each + operator is binary. For example,
>
|
use `+` = F in
a + b + c + d;
a + ( ( b + c ) + d )
end use;
|
| (72) |
|
|
5.8 Other Statements
|
|
|
The quit Statement
|
|
The Maple keywords quit, done, and stop perform the same task and, when entered in the command-line interface, cause the Maple process to terminate.
Note: The quit statement cannot be used in the Maple standard interface. In the standard interface, use File > Close Document to end your Maple session.
quit, stop, and done are available as command names if quoted using name quotes. These forms allow you to exit Maple while passing an integer in the range 0 .. 255 as an argument to be returned to the calling process as exit status.
|
|
The save Statement
|
|
You can save Maple expressions to a file by using the save statement. It has the general form
save name1, name2, ..., nameN, file
|
|
|
The names namei are names (that have assigned values) to be saved to the file file.
Normally, the file name file is a string.
For example, make the following three assignments and run the subsequent save statement.
>
|
a := proc( x ) sin( x / 2 ) end proc:
|
>
|
save a, b, c, "myfile.txt";
|
The file myfile.txt is created in the current directory (assuming adequate file permissions) containing the following Maple assignment statements.
a := proc (x) sin(1/2\*x) end proc;
b := 42;
c := "some text";
|
|
|
|
|
The read Statement
|
|
The read statement takes the following form.
where filename is a string.
The file named by filename must consist of valid Maple language statements. The statements in the file are executed as they are read, as though they were input into the Maple session in which the read statement was entered. Maple displays the results of executing each statement. However, the input statements are not echoed to the interface, by default. To change this, set the interface variable echo to a value of or higher.
>
|
interface( 'echo' = 2 ):
|
|
|
|
5.9 Exercises
|
|
1.
|
Find the product of the square root of all prime numbers less than 100.
|
Hint: The function isprime determines the primality of an integer.
2.
|
Find the sum of all odd composite numbers less than 150.
|
3.
|
Find the sum of the first 30 powers of 2.
|
4.
|
Write a looping structure that finds the four substrings (of a string assigned to the name MyString) containing only lowercase letters, uppercase letters, decimal digits, and special characters.
|
Hint: You can use relational operators to compare characters.
5.
|
Write a procedure, SPLIT, that, upon input of a product f and a variable x, returns a list of two values. The first item in the list should be the product of the factors in f that are independent of x, and the second item should be the product of the factors that contain an x.
|
Hint: Use the has, select, remove, and selectremove commands.
|
|
Contents Previous Next Index
|