• No results found

MPL contains two iteration structures that allow for repeated evaluation of a sequence of statements. The first iteration structure is the while structure which has the general form

while condition do

S1; (4.16)

S2; ... Sn;

where condition is a logical (or relational) expression. This structure is evaluated by first evaluating condition , and if it is to true, the indented statements S1, S2, . . . , Sm are evaluated. Once this is done, the process repeats, and again if the logical condition is true, the indented statements are evaluated. The process continues in this way checking if condition is true and if so, evaluating the indented statements. On the other hand once

5In Maple, Mathematica, and MuPAD, algebraic expansion is not part of automatic simplification.

6 The problem to determine if an expression simplifies to 0 is known as the zero equivalence problem. D. Richardson has shown that for the class of algebraic expres-sions constructed with rational numbers, the symbol x, the real numbers π and ln(2), the sin, exp, and absolute value functions, and sums, products, and powers with in-teger exponents, it is impossible to give an algorithm that can always determine if an expression simplifies to 0 (see Richardson [84]).

condition evaluates to false, the indented statements are not evaluated, and the structure terminates.

Example 4.7. The sum of the first n + 1 terms of a Taylor series for a function u(x) about x = a is given by

n i=0

u(i)(a)

i! (x− a)i (4.17)

where u(i) is the ith derivative of u(x), and u(0) = u(x). When n is a non-negative integer, the sum (4.17) is obtained with the following MPL statements:

1 i := 1;

2 s :=Substitute(u, x = a);

3 while i≤ n do

4 u :=Derivative(u, x);

5 s := s +Substitute(u, x = a)/i!∗ (x − a)i; 6 i := i + 1;

The substitution in line 2 initializes s to u(0)(a) = u(a), and each traversal through the while loop adds one additional term of the Taylor series to s and increases the counter i by 1. Eventually i = n + 1, and so the condition i≤ n is false, and the while structure terminates.

For example, if u = sin(x), n = 3, and a = 0, after executing the loop we obtain s = x− x3/6. (Implementation: Maple (mws), Mathematica

(nb),MuPAD(mnb).) 

The second iteration structure is the for structure which has the general form

for i := start to finish do

S1; (4.18)

S2; ... Sn;

where i is a variable and start and finish are expressions that evaluate to integer values. When start≤ finish, the indented statements are evaluated

for each integer value of i = start, start + 1, . . . , f inish. I f start > f inish, the indented statements are not evaluated7

Example 4.8. The sum of the first n + 1 terms of the Taylor series can also be obtained using a for structure:

1 s :=Substitute(u, x = a);

2 for i := 1 to n do 3 u :=Derivative(u, x);

4 s := s +Substitute(u, x = a)/i!∗ (x − a)i; (Implementation: Maple(mws), Mathematica(nb),MuPAD(mnb).) 

All computer algebra languages provide iteration structures similar to while and for8.

Evaluation of Logical Expressions. I n MPL, the value (true or false) of a logical expression with main operator and or main operator or is obtained by evaluating each of the operators in a left to right manner until the value of the entire expression is determined. In some cases this value is obtained without evaluating all the operands of the logical expression. For example, consider the following decision structure which tests if n is a positive integer:

if Kind(n) = integer and n > 0 then (4.19) ...

Observe that the second relational expression only evaluates to true or false when n has a numerical value. When n is not an integer, however, the value of the entire logical expression (false) is determined by the test Kind(n) = integer, and there is no need to evaluate the expression n > 0.

Most computer algebra systems evaluate logical expressions in decision and iteration structures in a similar way9.

7 Some of our procedures contain For loops that include aReturnstatement. (For example, see lines 5-6 in the procedure Polynomial svin Figure6.2on page 218.) In this case, we intend that both the loop and the current procedure terminate when the Return is encountered, and that the value returned by the procedure is the value of the operand of the Return statement. The for statements in both Maple and MuPAD work in this way. However, in Mathematica, a Return in a For statement will only work in this way if the upper limit contains a relational operator (e.g., i<=N). (Implementation:

Mathematica(nb).)

8In Maple and MuPAD, use the while and for statements. In Mathematica, use the While and For statements.

9Maple, Mathematica, and MuPAD use this approach to evaluate logical expressions in decision and iteration structures.

The procedure in the next example uses the concepts described in this section.

Example 4.9. It is often necessary to separate the operands of a product into two classes, those that depend on an expression (say x) and those that do not. For example, this operation is needed when we use the linear property of the integral to move the factors of a product that do not depend on the integration variable x outside of the integral sign:

 c x sin(x) 2 dx = c

2



x sin(x)dx. (4.20)

A procedure Separate factors that performs the separation operation is given in Figure4.15. The procedure takes two algebraic expressions u

Procedure Separate factors(u, x);

Input

u, x :algebraic expressions;

Output

a list with twoalgebraic expressions;

Local Variables

f, free of part , dependent part , i;

Begin

1 if Kind(u) = ”∗ ” then 2 free of part := 1;

3 dependent part := 1;

4 for i := 1 to Number of operands(u) do 5 f :=Operand(u, i);

6 if Free of(f, x) then

7 free of part := f∗ free of part

8 else

9 dependent part := f∗ dependent part;

10 Return([free of part, dependent part ])

11 else

12 if Free of(u, x) then 13 Return([u, 1])

14 else

15 Return([1, u]) End

Figure 4.15. An MPL procedure that separates factors in a product that depend on x from those that do not. (Implementation: Maple(txt),Mathematica(txt), MuPAD(txt).)

and x as input and returns a two-element list. The first member of the list contains the product of the factors of u that are free of x, while the second member contains the product of the remaining factors. If there are no factors in a category, the integer 1 is returned for that category.

The procedure can be applied to both products and non-products.

When u is a product, the Free ofoperator is applied to each factor which is then placed in the appropriate category (lines 6-9). When u is not a product, it is reasonable to apply Free of to the entire expression which is then placed in the appropriate category (lines 12-15). The procedure is invoked with an expression such as

Separate factors

c x sin(x)

2 , x



→ [c/2, x sin(x)].