• No results found

A multiple structural substitution is one in which a collection of structural substitutions is applied to an expression with a single operation. Since the individual substitutions may not be independent (i.e., one substitution may affect the action of another one), both the order of the substitutions and the mechanics of the process may affect the result. We describe below two models for multiple substitution, sequential substitution and concurrent substitution.

Definition 3.31. Let u be an expression and let L be a list of equations L = [t1= r1, t2= r2, . . . , tn= rn]

where the targets tiare distinct. The sequential structural substitution operator has the form

Sequential substitute(u, L).

The operator returns the expression un that is defined by the sequence of structural substitutions

u1 := Substitute(u, t1= r1);

u2 := Substitute(u1, t2= r2);

...

un := Substitute(un−1, tn= rn);

Example 3.32.

Sequential substitute(x + y, [x = a + 1, y = b + 2])→ a + b + 3, Sequential substitute(x + y, [x = a + 1, a = b + 2])→ b + 3 + y, Sequential substitute(f (x) = a∗ x + b, [f(x) = 2, x = 3])

→ 2 = 3 ∗ a + b, (3.12)

Sequential substitute(f (x) = a∗ x + b, [x = 3, f(x) = 2])

→ f(3) = 3 ∗ a + b. (3.13)

The operations (3.12) and (3.13) show that sequential substitution is de-pendent on the order of the substitutions. (Implementation: Maple(mws),

Mathematica(nb),MuPAD(mnb).) 

Example 3.33. Consider the three polynomials

u(x) = x2+ x + 2, v(x) = x2+ 3∗ x − 7, w(x) = x2− 5 ∗ x + 4.

I n Figure3.26we give an MPL dialogue that obtains the functional com-positions u(v(w(x))) and u(w(v(x))). Since the composition operation is a not commutative, we use sequential substitution to determine the order of

the compositions. 

Definition 3.34. Let u be an expression and let S be the set of equations S ={t1= r1, t2= r2, . . . , tn= rn},

where the targets t1, t2, . . . , tn are distinct. The concurrent structural sub-stitution operator has the form

Concurrent substitute(u, S).

The operator returns a new expression defined in the following way: recur-sively search through the expression tree of u and compare each complete sub-expression v to each of the (distinct) targets t1, t2, . . . , tn. If v is iden-tical to some ti, substitute the corresponding replacement ri for v.

Since each complete sub-expression of u is identical to at most one target, the order of the substitutions is not significant, and so concurrent substitution is defined in terms of a set S rather than a list.

<1> u := x2+ x + 2;

u := x2+ x + 2

<2> v := x2+ 3∗ x − 7;

v := x2+ 3∗ x − 7;

<3> w := x2− 5 ∗ x + 4;

w := x2− 5 x + 4

<4> Algebraic expand(Sequential substitute(u, [x = v, x = w]));

x8− 20 x7+ 172 x6− 830 x5+ 2439 x4− 4390 x3+ 4573 x2− 2365 x + 464

<5> Algebraic expand(Sequential substitute(u, [x = w, x = v]));

x8+ 12 x7+ 16 x6− 234 x5− 407 x4+ 2202 x3+ 1479 x2− 10089 x + 7834

Figure 3.26. An MPL dialogue that obtains a composition of polynomials us-ing sequential substitution. (Implementation: Maple(mws),Mathematica (nb), MuPAD(mnb).)

Example 3.35.

Concurrent substitute((a + b)∗ x, {a + b = x + c, x = d}) → (x + c) ∗ d.

In this case, the complete sub-expression a + b is replaced by x + c and the complete sub-expression x is replaced by d. Notice since the replacement x + c is not part of the original expression, its x is not replaced by d. I f this additional substitution is intended, it is obtained with

Sequential substitute((a + b)∗ x, [a + b = x + c, x = d]) → (d + c) ∗ d.

Another example is

Concurrent substitute(f (x) = a∗ x+ b, {x = 3, f(x) = 2}) → 2 = 3 ∗ a + b.

In this case, the substitution x = 3 does not affect the substitution f (x) = 2 as it does with sequential substitution. (Compare this with Expression (3.13) where the order of the substitutions affects the result). (Implemen-tation: Maple(mws), Mathematica(nb),MuPAD(mnb).) 

Most computer algebra systems allow some form of multiple structural substitution (see Figure3.22). In Exercise10on page195we describe MPL procedures for these operators.

Since structural substitution is obtained by simply comparing a tar-get expression to the complete sub-expressions of an expression, it cannot obtain all substitutions which occur in symbolic calculations. For more information about algorithms for general substitution operations based on polynomial division, the reader may consult Cohen [24], Sections 4.1 and 6.2.

Exercises

1. For each of the following, give the set of complete sub-expressions of the automatically simplified form of the expression:

(a) a∗ b/c.

(b) (a + b) + (c− d).

(c) 1/(2∗ x).

(d) ((x− y) ∗ y/2)2.

(e) x = −b + (b2− 4 ∗ a ∗ c)1/2

2∗ a .

2. (a) Explain why the operation Free of(a∗ (b/c), b/c) retu rns true.

(b) Explore the capacity of the Free of operator in a CAS (see Fig-ure3.22). Does the operator have the same capacity as theFree of operator in the text?

(c) One extension of the Free of operator is to allow a target to be a function name or an algebraic operator. Experiment with a CAS to see if theFree of operator in that system has this capability.

3. (a) Explore the capacity of the substitution operator in a CAS (see Fig-ure3.22). Does the operator have the same capacity as theSubstitute operator in the text?

(b) One extension of theSubstituteoperator is to allow a target to be an algebraic operator or a function name. For example, in this case

Substitute(a + b, ” + ” = ”∗ ”) → a ∗ b.

Can a CAS do this with its substitution operator?

(c) Perform each of the following substitutions with a single application of theSubstitute operator. (In each case, it is necessary to find a

“clever” substitution.)

i. Replace a∗ b by x in a ∗ b ∗ c to get x ∗ c.

ii. Replace u + 1 by x in (u + 1)2+ u + 1 to get x2+ x.

iii. Replace a + b by 1 in a∗ (a + b) + b to get 1.

(d) Is it possible to replace all occurrences of the tan function in an expression with its representation in terms of sin and cos with a sin-gle application of the Substituteoperator? For example, is it possi-ble to obtain the transformation tan(x) + tan(y)→ sin(x)/ cos(x) + sin(y)/ cos(y) with a single substitution? (Don’t use a multiple sub-stitution here.)

4. Can the Solve operator in a CAS solve an equation for a complete sub-expression? Can theSolveoperator solve an equation for an expression that is not a complete sub-expression? In Maple use solve, in Mathematica use Solve, and in Mu PAD u se solve.

5. Evaluate each of the following:

(a) Sequential substitute(x∗ (x + y), [x = 2, x + y = 3]).

(b) Concurrent substitute(x∗ (x + y), {x = 2, x + y = 3}).

(c) Sequential substitute(x + y2, [x = y, y = x]).

(d) Concurrent substitute(x + y2, {x = y, y = x}).

(e) Sequential substitute(a + b + c, [a = b, b = c, c = a]).

(f) Concurrent substitute(a + b + c, {a = b, b = c, c = a}).

6. (a) Let u be an algebraic expression. Give a sequence of statements that gives a new expression with each occurrence of x replaced by y and each occurrence of y replaced by x. For example, x2+ 2∗ y is transformed to y2+ 2∗ x. (Don’t use a multiple substitution here.) (b) Is it possible to do the operation in part (a) with a single statement

that involves a multiple substitution?

7. Let u be a mathematical expression and suppose t1, r1, t2, r2 are distinct symbols. Prove or disprove:

Sequential substitute(u, [t1= r1, t2= r2]) = Sequential substitute(u, [t2= r2, t1= r1]).

Further Reading

3.1 Recursive Definitions and Algorithms. Recursion for algorithms is discussed in Maurer and Ralston [65]. An interesting popular account of recursion is found in Hofstadter [48].

3.2 Expression Trees and Primitive Operations. Expression trees in a conventional programming context are discussed in most books on data structures and algorithms. For example, see Weiss [98]. Expression trees in the Mathematica system are discussed in Wolfram [102].

4

Elementary Mathematical