y >_< i y 一,一一,一 i maple's control statements 教授:蔡桂宏 博士...

30
Y >_< I Y , I Maple's Control Statements 教教 教教教 教教 教教 教教教 95503 95503 教教教 教教教教 教教教 教教教教

Post on 20-Dec-2015

231 views

Category:

Documents


0 download

TRANSCRIPT

Y

>_<

IY

一 , 一

I

Maple's Control Statements

教授:蔡桂宏 博士 學生:張杰楷

• 9550395503 統資軟體課程講義統資軟體課程講義

Y

>_<IY

一 , 一

I

內容綱要• 5.1 Introduction

• 5.2 Repetition statements• 5.3 More loop examples

• 5.4 Conditional statements

• 5.5 Boolean expressions

• 5.6 For-loop like commands

• 5.7 Statements vs. expressions

Y

>_<IY

一 , 一

I

內容綱要• 5.8 Print levels, printlevel, and print

commands

• 5.9 Procedures that return unevaluated

or return NULL

• 5.10 Online help for control statements

Y

>_<IY

一 , 一

I

5.1 Introduction

• 為了寫些有趣的程式,需要學習兩種新的 Maple commands :

1. Repetition statement

2. Conditional statement

合稱為 control statements.

Y

>_<IY

一 , 一

I

5.2 Repetition statements

• For-loop 語法: for index_variable from initial_value to final_value by ste

p_size do

sequence_of_Maple_commands

od

• 一些例子

Y

>_<IY

一 , 一

I

5.2 Repetition statements

• 注意: 1. by step_size 可以省略,當 initial_value 小於 final_value

時,且省略 step_size ,則內定 step_size 值為 1 。

2. 當 index_variable 遞增 by step_size 到達一個大於 final_value 的值時,或 step_size 為負,則當 index_variable 遞減 by step_size 到達一個小於 final_value 的值時,則 for-loop 將會停止。

3. for-loop 不承認 index_variable 在之前所設定的值。 Ex

Y

>_<IY

一 , 一

I

5.2 Repetition statements

• 注意: 4. 當 for-loop 停止, index_variable 值都會大 ( 小 ) 於 final_value ,但是確切的大 ( 小 ) 多少是由 initial_valu

e,

final_value, 與 step_size 所決定。 Ex

5. from initial_value 與 to final_value 也都可以省略,當 from initial_value 省略時,則內定 initial_value 值為 1 , Ex;不過當你省略 to final_value ,此 for-loop 將不停 止。

Y

>_<IY

一 , 一

I

5.2 Repetition statements

• 產生“ Tables” : for-loop 常被使用產生“ Tables” of results : Ex:某些整數的10進位、2進位與16進位互換 Ex:Table of polynomials and their factorizations

• 注意: 1. Print 的結尾 2. Od的結尾

Y

>_<IY

一 , 一

I

5.2 Repetition statements

• 對 for-loop 的結果命名: 我們可以利用 for-loop 快速的得到運算結果 Ex,可是當

我們想給每次 for-loop 的結果命名,該如何做?可以利用前幾章所提過的方法:

1. Concatenated names

2. Indexed names

Ex:找出前十個質數

Y

>_<IY

一 , 一

I

5.2 Repetition statements

• For-in-loop 語法: for index_variable in data_structure do

sequence_of_Maple_commands

od

• 一些例子

Y

>_<IY

一 , 一

I

5.2 Repetition statements

• While-loop 語法: while boolean_expression do

sequence_of_Maple_commands

od

只要 boolean_expression 為真, while-loop 會反覆執行。 Ex :判定 1~10 是否為質數

• 注意: 當我們不知迴圈確切要做幾次時,使用 while-loop 較佳。 Ex:找最接近1000的兩個質數

Y

>_<IY

一 , 一

I

5.2 Repetition statements

• Nest for-loop : 利用 Ex 說明。 Ex:計算多少質數小於2^i,且i從2到18。

• A loop that combines : Ex:找出第一個有1000位數的階層 Ex:For-in-while-loop

Y

>_<IY

一 , 一

I

5.3 More loop examples

• 5.3.1 Example 1: Riemann sums• 5.3.2 Example 2: Pascal‘s triangle• 5.3.3 Example 3: Periodic extensions• 5.3.4 Example 4: Drawing graphs• 5.3.5 Example 5: Butterfly curve• 5.3.6 Example 6: Animations

Y

>_<IY

一 , 一

I

5.4 Conditional statements

• If-then-else-fi 語法: if boolean_expression then

sequence_of_Maple_commands

else

sequence_of_Maple_commands

fi

可簡化為: if boolean_expression then

sequence_of_Maple_statements

fi

Y

>_<IY

一 , 一

I

5.4 Conditional statements

• Ex : Ex:一個簡單例子 Ex:隨機產生0或1,如果是0則輸出heads,不是則輸出

tails.

• If 與 for-in-loop 的結合: Conditional statements 單獨使用其作用相當有限,因此

其常與程序或 loop 結合,為其 body 的一部份。 Ex:What percentage of the random integers were 10's

Y

>_<IY

一 , 一

I

5.4 Conditional statements• Conditional statements 與數學: Ex:

• Some exercises : Ex

• Nest conditional statements : Ex:

Ex : Other nested conditional statements and nested procedures

2 1 x<0( )

sin( ) 0

xf x

x x

,,

2

2 2

x 0

( ) sin( ) x 3

6 9 3 3 x

x x

g x x

x x x

,,

Y

>_<IY

一 , 一

I

5.4 Conditional statements

• If-then-elif-then-else-fi : if boolean_expression then

sequence_of_Maple_commands

elif boolean expression then

sequence_of_Maple_commands

else

sequence_of_Maple_commands

fi

Y

>_<IY

一 , 一

I

5.5 Boolean expressions

• Boolean expression 常被使用於 while-loops 與 conditional statements 。 Ex

• Boolean expressions are made up of the three logical operators (and, or, not), the relational operators (<, <=, >, >=, =, <>), and functions that return true or false (i.e., boolean functions, ex : isprime ).

• We call and and or binary boolean operators and we call not a unary boolean operator.

Y

>_<IY

一 , 一

I

5.5 Boolean expressions

• and, or, not 真值表:

• 一些例子

X Y X and Y X or Y

false false false false

false true false true

true false false true

true true true true

X not X

false true

true false

Y

>_<IY

一 , 一

I

5.5 Boolean expressions

• The following is not a boolean expression in Maple, though it would be a true statement in a mathematics book. Ex

• There is a whole algebra to boolean expressions that specifies the order of precedence for all of the boolean operations, associativity rules for each boolean operation, and algebraic identities for boolean expressions. Ex

Y

>_<IY

一 , 一

I

5.6 For-loop like commands

• The three commands seq, add, and mul act very much like for-loops. In a sense they are abbreviations of special purpose for-loops.

• Seq : The seq command is used to create expression sequenc

es. Ex

Y

>_<IY

一 , 一

I

5.6 For-loop like commands• Add : The add command is a direct analogue in Maple to sigma

notation. An add command of the formadd( f(n), n=a..b )

means exactly the same thing as

and their for-loop equivalent is result := 0: for n from a to b do result := result + f(n) od: result; Ex

( )b

n a

f n

Y

>_<IY

一 , 一

I

5.6 For-loop like commands• Mul : The mul command is a direct analogue in Maple to mathe

matical product notation. A mul command of the formmul( f(n), n=a..b )

means exactly the same thing as

and their for-loop equivalent is result := 1: for n from a to b do result := result * f(n) od: result; Ex

( )b

n a

f n

Y

>_<IY

一 , 一

I

5.6 For-loop like commands

• Sum and product commands : They are two Maple commands that are related to the ad

d and mul command but they are not abbreviations for for-loops. Ex

• 注意: Here is one tricky difference between add, mul on the on

e hand and sum, product. The index variable in an add or a mul command is local variables. The index variable in the sum and product commands are global variables. Ex

Y

>_<IY

一 , 一

I

5.7 Statements vs. expressions

• In Maple, we cannot use statements where you are supposed to use expressions. The most common kinds of statements in Maple are repetition statements, conditional statements, assignment statements, and the restart statement. Almost everything else in Maple is an expression (and every expression is also a statement). Ex

• Maple allowed a conditional statement after the arrow but not a repetition statement. Ex

Y

>_<IY

一 , 一

I

5.8 Print levels, printlevel, and print commands

• 當你將 for-loops 放在 if-statements 中,或將 if-statements 放在 for-loops 中,將不會產生你所預期的結果 Ex ,這是因為 Maple 的 print levels 概念。系統預設為印出 the first print level 的結果,不過上述情況的 print levels 常常不是 1 ,因此可用下列兩種方式解決:

1. Use the print command, which always prints no matter what print level it is at. Ex

2. Change the value of the printlevel variable. Ex

• 雖然此兩種方式產生的結果,看來是相同地,不過事實上它們是有差異的,請看以下說明。

Y

>_<IY

一 , 一

I

5.8 Print levels, printlevel, and print commands

• The print command returns a special result called NULL, which is actually a name for the empty expression sequence. The last result variable, %, has the property that it always ignores the result NULL. This is why the print command did not have any effect on the last result variable. Ex

• So putting print commands in a procedure can change what the return value of the procedure is. On the other hand, changing the value of printlevel does not have any effect on the return value of a procedure. So there is a difference between using print commands in a procedure and changing printlevel. Ex

Y

>_<IY

一 , 一

I

5.9. Procedures that return unevaluated or return NULL

• We defined procedures that used if-then-else-fi statements to implement piecewise defined functions. But there were some subtle problems with those procedures that we left unresolved in that section. Ex

• Here is another, slightly different example of a function returning unevaluated. Ex

• Here is another example of a command that returns NULL. Ex

Y

>_<IY

一 , 一

I

5.10 Online help for control statements

• Repetition statements : ?for• Conditional statements : ?if• Boolean expressions : ?boolean• Seq, Add, and Mul : ?seq, ?add• Sum and Product commands : ?sum, ?product• Printlevel variable : ?printlevel• Print command : print• NULL represents : ?NULL• “Fail return" : ?RETURN, ?procname• All of Maple's statements : ?statement

Y

>_<

IY

一 , 一

I

~The End~