• Nem Talált Eredményt

The Components of Objects, Substitution

In document Solving Math Problems with Maple (Pldal 58-64)

What Have You Learn About Maple?

3.3 The Components of Objects, Substitution

In the previous chapters we became familiar with several Maple objects, such as numbers, sequences, lists, sets, expressions, equations etc. Although in this book we are not going to deal with the inner representation of each object, we still have to get to know the tools Maple offers for exploring the components of different objects. What do we mean by this? For example, in the case of the lists and sets, the number of the elements of the list and set might be useful to know. This is also true in the case of the expressions: it is worth knowing the number of the sub-expressions and the sub-expressions themselves.

This may seem useless because if we look at a given expression then we can immediately see what kinds of sub-expressions it has. Although this is true, Maple objects are usually created as the result of calculations and in these cases sometimes it might be difficult to determine the components. On the other hand, it is critical to understand that no matter how hard Maple strives for its inner representation to clearly represent the well-known properties of the different

mathematical objects, small differences can come up. And in this phase things can take us by surprise.

The nops procedure gives the numbers of the components of the Maple objects restart

nops karakterlánc ,nops 12 ,nops K12 1, 1, 1 nops 1

2 ,nops 1.2 ,nops 1C2 I

2, 2, 2

So the character sequence and the integers have one component, while each of the rational, real (floating-point) and complex number has two. In most cases these components are obvious. If not, the op procedure can come in handy.

op karakterlánc ,op 12 ,op K12

karakterlánc, 12,K12 op 1

2 ; 1op 1.2

1, 2 1 12,K1

The result of the first command is easily interpretable. The second one shows that the two components of the rational number are its numerator and denominator. In the case of the real number the components are the mantissa and the characteristic (12$101).

Now let’s look at more interesting examples.

nops 1, 2, 3, 4

Error, invalid input: nops expects 1 argument, but received 4 halmaz:= gamma, alpha, beta, delta

halmaz:= α,β,δ,γ

(3.3.6) nops halmaz ;op halmaz

4 α,β,δ,γ lista:= egy,kettő,három,kettő,egy

lista:= egy,kettő,három,kettő,egy nops lista ; op lista

5

egy,kettő,három,kettő,egy

Although the nops procedure gives an error message for the sequence, it operates reliably for the set and the list. We can tell the op procedure which components of the given Maple object we want to see. Namely, the op(i,e); gives the ith component of the e object and the op(i..j,e);

determines the sequence of the ii, i+1, ...,jth components of the e object op 2,halmaz ; op 2 ..4,lista

β

kettő,három,kettő

Please remember that you can refer to the elements of a list by its index closed between square brackets or in the case of 2-D Math by the subscript. According to this, the op(i, list) and listi and the op(i..j, list); and the listi..j; references are equivalent.

Finally, let’s look at an example for an expression and an equation.

kifejezés:=x4 yK2 Cx yK2 zK4 x;nops kifejezés ; 1op kifejezés kifejezés:=x4 yK2 Cx yK2 zK4 x

3 1

x4 yK2 ,x yK2 z,K4 x

részkifejezés:=op 1,kifejezés ;nops kifejezés ; op részkifejezés részkifejezés:=x4 yK2

3 x4,yK2

részkifejezés:=op 2,kifejezés ; nops részkifejezés ; op részkifejezés részkifejezés:=x yK2 z

3 x,yK2,z

részkifejezés:=op 3,kifejezés ; nops részkifejezés ; op részkifejezés részkifejezés:=K4 x

2 K4,x

egyenlet:=x3C1 = sin x y ;nops egyenlet ;op egyenlet egyenlet:=x3C1 = sin x y

(3.3.14) baloldal:=op 1,egyenlet; op baloldal

baloldal:=x3C1

Everything happened the usual way. In the case of the sum of the products and powers the

components are the members of the sum. The same is true in the case of the products: the number of the components is equal to the number of the factors of the product and the components

themselves are the multiplier factors. Finally, the two components of the equation are its right and left sides.

Notice that the components themselves are Maple objects thus they also have their own

components. It is obvious to consider the components of the components as the components of the original object. For example, x3, which is the component of the left side of the x3, ami az

x3C1 = sin x y equation, is the component of the equation as well. This observation allows us to discuss the levels of the components. The first level components of the e object are the terms of the op(e) sequence. The components of the first level components are the second level

components and so on. The following instruction sequence creates the first and second level components of the polynomial above.

`második szintű összetevők`:=NULL:

for i in op(polinom) do

`második szintű összetevők`:=`második szintű összetevők`,op (i)od:

polinom;

'`Level 1.`'=op(polinom);

'`Level 2.`'= `második szintű összetevők`;

4 x3K3 π xC8

Let’s continue with the substitution. The subs procedure is the perfect tool for the manipulation of the components of the Maple objects. So far we have used it mainly to calculate the substitution

(3.3.22)

value of expressions. But according to its definition, it is predestined to much more. The subs minta=kifejezs,e instruction substitutes all the occurrences of pattern in the e with the expression.

lista; subs kettő=KETTŐ,lista

egy,kettő,három,kettő,egy egy,KETTŐ,három,KETTŐ,egy kifejezés; subs yK2 = cos T ,kifejezés

x4 yK2 Cx yK2 zK4 x x4 cos T Cx cos T zK4 x egyenlet; subs x= 3,egyenlet

x3C1 = sin x y

What we have seen so far can suggest that everything happens just like we expected. But be careful!

polinom;subs 3 =zC1,polinom

4 x3K3 π xC8 K3 π xC8C4 xzC1

In this case we wanted to substitute 3 with the (z+1) but the system substituted the 3 located in the exponent and it did not substitute the 3 in the -3Pix. Why?

Because we would think, and we are right in an algebraic sense, that the -3Pix can be considered as

3 Pi x so plus 3 appears in this sub-expression. But Maple is doing something different.

op 1,polinom ; has %, 3

Now everything is clear. The 3 in the exponent is a component but the 3 of the -3Pix sub-expression is not a component of the polynomial. Keep this in mind. The

subs minta=kifejezs,e instruction changes only those appearances in which cases the pattern is a component and not a sub-expression or substring of the e expression. In other words, the subs procedure executes a syntactical substitution.

According to this we cannot substitute neither the aCbCc character sequence nor the `b+`

sub->

expression in the a+b+c expression with the subs procedure. Feel free to try it. You can recognize that the latter function is missing.

Maybe this crossed the mind of the developers of Maple because they developed the algsub procedure which is able to substitute the sub-sums of a sum consisting of more terms. And in this case it does not matter if the certain sub-sum is the component of the expression or not.

aCbCc

Notice the global behaviour of the algsubs and the subs. It is all the same if the sub-expression to be substituted (in the case of the subs the component) is located on the first, second or higher level, all of its occurrences are going to be substituted. Naturally this is not always an advantage thus Maple provides the subsop procedure as an alternative solution.

The subsop confines its operation to the first-level components (and not to the sub-expressions).

According to this its syntax is the following:

subsop i1=kifejezs1,i2=kifejezs2,...,e .

As a result the ith component of the e expression is substituted with the expression1, and its i2nd component is substituted with the expression2 and so on.

xC2 2 y2Cy3Kx3 y2C9

xC2 2 y2Cy3Kx3 y2C9 subsop 1 = Omega, 3 = Delta5,%

5Cy3CΩC9

With the subsop procedure we can substitute those elements of a list which are first-level components of the list. We can also delete these elements by the substitution of the NULL constant.

lista:= 1, 2, 3, 4 ; subsop 3 = 3333,lista ; subsop 1 =NULL, 3 =NULL,lista lista:= 1, 2, 3, 4

1, 2, 3333, 4 2, 4

The subsop procedure is particularly useful when we want to do the conversion not on the whole expression but only on one of its components, for example at the following expression in which

>

case we want the a3-b3 to appear in the argument of the sin. We have to do the multiplication in hand.

p:= sin aKb a2Ca bCb2 Ccos a3Kb3

p:= sin aKb a2Ca bCb2 Ccos a3Kb3 expand p

sin a3 cos b3 Kcos a3 sin b3 Ccos a3 cos b3 Csin a3 sin b3

Well, we did not expect this. The expand procedure did not execute the specific operations only in the argument of the sin but it also applied the trigonometric identities concerning the sine and cosine of the sum then it executed some combinations. In reality, the expand procedure should be applied only for the argument of the sin which is the first component of the op(1,p).

op 1,p

As we can see, at first we executed the task with three instructions then with one instruction. But now we can ponder about the connections between the clarity of the task and the conciseness of the solution.

The syntax of the subs instruction – similarly to the subsop procedure – allows us to specify more substitutions within an instruction thus we execute the specific substitutions sequentially. But this interpretation can raise problems. It is questionable if we execute the second substitution for the expression received as the solution of the first substitution or for the original expression.

First the

subs e1=f1,e2=f2,...,e instruction makes the system execute the e1=f1 substitution so it

substitutes all the occurrences of the e1 with the f1 in the e expression. Then it continues with the e2=f2 substitution which it executes on the expression received after the execution of the first substitution. This form of substitution is called the sequential substitution.

p:=x2 yCy3Kx y2

The result of the first instruction is clear. We substituted the a to the place of the x and the b to the place of the y. But the second result is surprising because we would expect that the instruction transposes the variables x and z. And what happened contrary to this? In the first part of the

> sequential substitution the system executed the x=y substitution and got the y3 expression, then it did the y=x substitution and received the x3 expression.

The transposition of the variables can be done by simultaneous substitution. Its syntax is

subs e1=f1,e2=f2,... ,e , so the difference is that the specific substitutions are written between

We also have to mention the powsub procedure located in the student package which executes the substitution not on the basis of the inner representation but for the sub-expressions in an algebraic sense

Naturally the powsubs has its limits. For example, it does not recognize the x+y factor in the x2-y2 expression. We can help it by writing the expression in the form of a product.

sin xCy 2 Cx2Ky2

In document Solving Math Problems with Maple (Pldal 58-64)