• No results found

The term automatic (or default) simplification refers to the mathematical simplification rules that are applied to an expression during the evaluation process. In computer algebra systems, this usually involves the “obvious”

simplification rules from algebra and trigonometry that remove extraneous symbols from an expression and transform it to a standard form.

7Procedures in a CAS language are like procedures or functions in a conventional programming language. A procedure in the Maple language is given in Figure1.3 on page6. We consider procedures in Chapter4.

<1> 2 + 3/4 + 5/6;

43

12

<2> x + y + 2∗ x;

3x + y

<3> x∗ y ∗ x2;

x3y

<4> 1∗ x3+ a∗ x0+ b∗ x1+ 0∗ x2;

a + b x + x3

<5> x∗ y + 3 ∗ y ∗ x;

4 x y

<6> sin(π/2);

1

<7> ln(e2);

2

<8> arctan(1);

π/4

<9> ı2;

− 1

<10> e(−ı∗π);

− 1

<11> 0≤ 1 and 1 ≤ 2;

true

<12> P and P and Q;

P and Q

Figure 2.15. An MPL dialogue that shows some examples of automatic simplifi-cation. (Implementation: Maple(mws),Mathematica(nb),MuPAD(mnb).)

The MPL dialogue in Figure2.15illustrates some of these obvious sim-plifications. Example <1> shows a simplification that involves the sum of rational numbers. Example <2> shows that automatic simplification combines numerical coefficients of like terms. The next example <3> illus-trates a similar simplification in which integer exponents of the common base x are combined. Example <4> illustrates some simplification rules that involve the integers 0 and 1. Notice that after evaluation, the x3 term appears at the right end of the expression. This reordering, which is an

application of the commutative law of addition, serves to put the result in a more readable form and, in some cases, contributes to the simplification process8. The next example <5> illustrates this point. To simplify this expression, the term 3∗ y ∗ x is first reordered (using the commutative law for multiplication) to 3∗ x ∗ y after which the coefficients of the two like terms are combined. Examples <6>, <7>, and <8> illustrate automatic simplification rules that involve known functions, while Examples <9> and

<10> illustrate simplification rules that involve reserved symbols.

Examples <11> and <12> illustrate the automatic simplification rules that are applied in some systems to logical expressions as data objects9. I n Example <12>, P and Q are unassigned identifiers and the simplification follows from the general logical rule P and P → P.

The examples in Figure2.15are roughly similar to what happens in a real computer algebra system. However, since there is no consensus about which simplification rules should be included in automatic simplification, the process can vary somewhat from system to system.

Figure2.16 shows an interactive dialogue with the Macsyma system that shows what happens when automatic simplification is suppressed. At the prompt (c1) we assign an expression to u and at (c2) turn off the automatic simplifier by assigning the value false to the variable simp. At (c3) we differentiate u and obtain an expression that is so involved it is difficult to interpret10. At (c4) we turn the automatic simplifier back on and at (c5) obtain a much more reasonable form for the derivative.

In MPL (as in a CAS), all expressions in dialogues and computer pro-grams operate in the context of automatic simplification. This means:

• All input operands to mathematical operators are automatically sim-plified before the operators are applied.

• The result obtained by evaluating an expression is in automatically simplified form.

Since automatic simplification is so central to the programming process, it is a good idea to understand which simplification rules are applied by the process and which are not. For now, the exercises in this section can be used to explore the automatic simplification process in a CAS.

8The reordering process in Mathematica and MuPAD is similar to what is described here. The reordering process in Maple is handled in a different way (see Cohen [24], Section 3.1).

9Maple obtains <11> and <12>. Mathematica obtains <11>, but not <12>. Mu-PAD obtains <12>, but not <11>.

10Notice that Macsyma uses logarithmic differentiation to differentiate ex2. Logarith-mic differentiation provides a way to differentiate general powers of the form f (x)g(x).

(c1) u : a*x + x*exp(x∧2);

(d1) x ex2+ a x

(c2) simp : false;

(d2) false

(c3) diff(u,x);

(d3) 1 a + 0 x + ex2

e−1x20 + log(e) (2x)

x + 1 ex2 (c4) simp : true;

(d4) true

(c5) diff(u,x);

(d5) 2 x2ex2+ ex2+ a

Figure 2.16. An interactive dialogue with the Macsyma system that shows what happens when automatic simplification is suppressed.

In Chapter3, we show how automatic simplification modifies the struc-ture of expressions, which in turn leads to simpler algorithms and programs.

I n Cohen [24], Chapter 3, we give the formal algebraic properties of au-tomatically simplified expressions and describe an algorithm that obtains the simplified form.

Exercises

1. (a) Consider the following transformations of powers11: i. x2x3→ x5.

ii. x1/2x1/3→ x5/6. iii. xaxb→ xa+b.

11Some of the power transformations in this problem are only valid in certain (real or complex) contexts.

iv. (x2)3→ x6.

Which of these transformations is obtained with automatic simplifi-cation?

(b) Based on the data obtained in part (a), give a summary of how the power translations are applied in automatic simplification.

2. The algebraic operations addition and multiplication obey the following distributive laws:

a· (b + c) = a · b + a · c, (a + b)· c = a · c + b · c

(a) Consider the following transformations which are based on these laws:

i. 2 x + 3 x→ 5 x.

Which of the these transformations are obtained with automatic sim-plification?

(b) Based on the data obtained in part (a), give a summary of how the distributive laws are applied in automatic simplification.

3. (a) Consider the following transformations of the sin function:

i. sin(0)→ 0.

viii. sin(x + π/2)→ cos(x) . ix. sin(x + 2 π)→ sin(x).

x. sin(a + b)→ sin(a) cos(b) + cos(a) sin(b).

xi. sin(a) cos(b) + cos(a) sin(b)→ sin(a + b).

xii. sin2(x) + cos2(x)→ 1.

Which of the these transformations is obtained with automatic sim-plification?

(b) Based on the data obtained in part (a), give a summary of the trans-formation rules for the sin function which are obtained with automatic simplification.

4. In this problem we ask youto explore how the indeterminate forms 0/0 and 00 are handled in automatic simplification.

(a) Enter each of the following expressions in the interactive mode of a CAS.

i. 0/0.

ii. 00.

iii. (a (x + y)− a x − a y)/(x − x).

iv. (x− x)/(a (x + y) − a x − a y).

v. (x− x)a(x+y)−a x−a y. vi. (a (x + y)− a x − a y)x−x.

(b) Based on the data obtained in part (a), give a summary of how inde-terminate forms are handled by automatic simplification.

5. Enter each of the following expressions in the interactive mode of a CAS:

−b, a− 2 ∗ b, 1/a2, a/b.

Although each of the expressions is returned in the form it was entered, some “hidden”transformations have been applied. In other words, the internal form used by the CAS is different from the displayed form. Use the operand selection operator in a CAS to determine the internal form.

(Use op in Maple and MuPAD, and Part in Mathematica.)

6. This exercise refers to the Macsyma dialogue in Figure2.16 on page 55.

What simplification rules are used to obtain (d5) instead of (d3)?

7. Consider a CAS such as Mathematica or MuPAD where terms in a sum or factors in a product are reordered as part of automatic simplification.

Experiment with the CAS to determine how it carries out the ordering pro-cess. Try polynomials such as <4> in Figure2.15as well as more involved expressions. For example, are any terms or factors in

1 + z y2+ (a + 1) b + c (a + 1) reordered by automatic simplification?

2.3 Mathematical Programs

Simply put, an MPL mathematical program (or mathematical algorithm) is a sequence of statements in the MPL language that can be implemented in terms of the operations and control structures available in a computer algebra programming language. The design and implementation of math-ematical programs is a major theme of this book.

In a sense, the MPL dialogue in Figure2.8 is an example of a simple interactive program. What we really have in mind, however, are more involved programs that have the following features:

1. The statements in the program are viewed collectively as a unit which either is entered at a single prompt or input region in the interactive mode or, for larger programs, is contained in a text file that is loaded into the system.

2. The program statements include mathematical expressions, assign-ment stateassign-ments, decision stateassign-ments, iteration stateassign-ments, and func-tion and procedure definifunc-tions12.

3. As with conventional programs, some statements serve as input state-ments, some statements are for intermediate calculations for which the output is not displayed, and some statements serve as output statements that display the result of a computation.

4. The program is designed in a general way so that it performs a cal-culation for a class of problems rather than for a single problem.

For an example of a program that incorporates some of these points, let’s consider again the computation of the first and second derivatives of an implicitly defined function such as

exp(x) + y3= 4 x2+ y.

This problem, which was considered in Section 2.1, involves the manip-ulations in Equations (2.8) through (2.12), and an MPL dialogue that performs the calculations is given in Figrue2.8 on page42. This dialogue assumes that x is the independent variable, y is the dependent variable, and requires that y be expressed as the function form y(x).

In this section we modify the program to permit a choice of mathe-matical variable names and do not require that the dependent variable be

12Decision statements, iteration statements, and function and procedure definitions are described in Chapter4.

1 u in := exp(s) + t4= 4∗ s2+ t : 2 x var := s :

3 y var := t :

4 u new:=Substitute(u in, y var = y var(x var)) : 5 u p:=Derivative(u new, x var):

6 F irst derivative :=Solve(u p,Derivative(y var(x var), x var);

7 u pp:=Derivative(F irst derivative, x var) :

8 S econd derivative :=Substitute(u pp, First derivative );

Figure 2.17. An MPL mathematical program that obtains the first and second derivatives of an implicit function. (Implementation: Maple(mws),Mathematica (nb),MuPAD(mnb).)

expressed as a function form. By simply modifying the input statements, we can obtain

or, for that matter, if the input expression is expressed in terms of the variables s and t as exp(s) + t3= 4 s2+ t, the derivatives

An MPL program that performs these calculations is given in Fig-ure2.17. Observe that some statements are terminated by a colon (lines 1, 2, 3, 4, 5, and 7) and some by a semicolon (lines 6 and 8). This notation is interpreted as follows: statements that end with a colon suppress the display of the output, while those that end with a semicolon display the output. Most computer algebra systems allow control of output display, although the termination symbols vary from system to system13.

Lines 1 through 3 serve as input statements for the program. Since the program is designed to allow a choice of mathematical variable names, we have chosen programming variable names (u in, x var , y var , etc.) that are unlikely to be used as mathematical variables. At line 1 we assign an input expression, and at lines 2 and 3, we initialize two programing variables x var and y var which contain the mathematical variables (s and t for

13In both Maple and MuPAD, statements that are terminated with a colon suppress the output, while those that are terminated with a semicolon display the output. In Mathematica, statements that are terminated with a semicolon suppress the output, while those without a terminating symbol display the output.

this input) which serve as the independent and dependent variables. With these two assignments, the output of the program is the derivatives

dt

ds and d2t ds2.

The derivative operations at lines 5 and 7 require that the dependent vari-able t be expressed as the function form t(s). Since this is not done in line 1, we account for this at line 4 with a substitution that replaces each t in u inby t(s). Except for changes in notation, lines 5 through 8 are similar to those in Figure2.8. With the choice of input, the outputs from lines 6 and 8 are

dt(s)

ds = −exp(s) + 8 s

4 t(s)3− 1 , (2.21)

d2t(s)

ds2 = −exp(s) + 8

4 t(s)3− 1 − 12(−exp(s) + 8 s)2t(s)2

(4 t(s)3− 1)3 . (2.22) Observe that the dependent variable t is expressed in function notation t(s), even though this is not done in the input at line 1. In Exercise1we describe a modification of the program that removes this function notation from the output.