• No results found

Decision structures provide a way to control the flow in an algorithm.

MPL provides three decision structures. The simplest one is the if struc-ture which has the general form shown in Figure4.12-(a). The expression

if condition then T1;

T2; ... Tm;

(a) The if structure.

if condition then T1;

T2; ... Tm

else F1; F2; ... Fn;

(b) The if-else structure.

Figure 4.12. The general form of the MPL if and if-else decision structures.

condition is a logical (or relational) expression that evaluates to one of the logical constants true or false. Each Ti is either a mathematical expres-sion, an assignment statement, another decision structure, or an iteration structure (described below).

The if structure usually operates in the following way: when condition evaluates to true, the indented2 statements T1, T2, . . . , Tm are evaluated, and when condition evaluates to false these statements are skipped. The exception to this scheme arises when the if statement is included in a procedure, and one of the indented statements includes a Return. I n this case, when condition is true, the statements controlled by the if are evaluated until the Return is encountered, at which point the procedure terminates, and the evaluated form of the argument toReturnis returned by the procedure. This exception also applies to the other decision and iteration structures described below.

A more general decision structure is the if-else structure which al-lows for two alternatives. It has the general form3 shown in Figure 4.12-(b). When the expression condition evaluates to true, the statements T1, T2, . . . , Tm are evaluated, and when condition evaluates to false, the statements F1, F2, . . . , Fn are evaluated.

Example 4.5. Here is a simple example of an if-else structure:

if 0≤ x and x ≤ 1 then

f := x2+ 4 (4.15)

else

f := x2− 1;

(Implementation: Maple(mws),Mathematica(nb),MuPAD(mnb).)  The most general MPL decision structure is the multi-branch decision structure which allows for a sequence of conditions. It has the general form shown in Figure4.13. In this generality, the structure contains zero or more elseif sections and an optional else section. Upon evaluation, the logical expressions condition1, condition2, . . . are evaluated in sequence. If conditioni is the first one that evaluates to true, then the statements in that section Si1, . . . , Simi are evaluated while all the other statements are

2Some computer algebra languages require a termination symbol (such as end if, fi, or ]) to indicate the extent of statements controlled by the if structure. In MPL, these statements are indicated by indentation without a termination symbol.

3 As is common practice in some programming languages, in MPL we omit the semicolon at the end of a statement that precedes an else, an elseif (defined below), and an End.

if condition1 then S11;

S12; ...

S1m1

elseifcondition2 then S21;

S22; ...

S2m2

...

elseifconditionn then Sn1;

Sn2; ...

Snmn

else F1; F2; ...

Fr;

Figure 4.13. The MPL multi-branch structure that provides for a sequence of alternatives.

skipped. I f none of the tests evaluate to true, the statements in the else section (if included) are evaluated.

All computer algebra languages provide if structures and if-else struc-tures, and some languages provide a version of the multi-branch decision structure4.

The procedure in the next example utilizes a multi-branch structure.

4 In Maple and MuPAD, use the if statement to implement MPL’s if, if-else, and multi-branch structures. In Mathematica, use the If statement to implement MPL’s if and if-else structures and the Which statement to implement MPL’s multi-branch structure.

Example 4.6. Recall that a mathematical function u(x) is even if u(x)− u(−x) = 0

and odd if

u(x) + u(−x) = 0.

For example, u(x) = x2− 1 is even, u(x) = x3is odd, while u(x) = x2+ x3 is neither even nor odd.

A procedure that tries to determine if an algebraic expression u is even or odd is given in Figure4.14. The procedure is interesting for both what it can do and what it cannot do. Observe that the procedure operates in the simplification context of automatic simplification, and in this context it can determine the nature (even or odd) of the first two examples given above.

Notice that when the procedure is unable to determine that u is even or odd, it returns the symbol Unknown, rather than a symbol indicating that the expression is neither even nor odd. We do this because automatic simplification applied at lines 2 and 4 may not simplify an expression to zero even though the expression simplifies to zero in a mathematical sense.

For example, suppose that u is the even expression (x + 1) (x− 1), and

Procedure Even odd(u, x);

Input

u : analgebraic expression;

x : a symbol;

Output

one of the global symbols Even, Odd , or Unknown ; Local Variables

v;

Begin

1 v :=Substitute(u, x =−x);

2 if u− v = 0 then

3 Return(Even)

4 elseif u + v = 0 then 5 Return(Odd )

6 else

7 Return(Unknown ) End

Figure 4.14. An MPL procedure that attempts to determine if u is even or odd.

(Implementation: Maple(txt),Mathematica(txt),MuPAD(txt).)

let’s assume that algebraic expansion is not included in automatic simpli-fication5. I n this case, v is the expression (−x + 1) (−x − 1), and u − v is the expression (x + 1) (x− 1) − (−x + 1) (−x − 1), which does not sim-plify to 0 with automatic simplification. Although we can remedy this by applying the Algebraic expand operator at lines 2 and 4, there are other expressions that are not handled in this simplification context. For ex-ample, 1/(x− 1) − 1/(x + 1) is even, but this cannot be determined by algebraic expansion and automatic simplification. In this case, rational simplification (with Rational simplify) is required at lines 2 and 4. But then, sin(x/(x + 1)) + sin(x/(x− 1)) is even, but this is not handled by rational simplification.

While it is possible to increase the simplification power at lines 2 and 4 to handle all of the above expressions, it is theoretically impossible to increase the simplification power to a level that the procedure can always determine if an algebraic expression is even or odd6.