{VERSION 3 0 "SGI MIPS UNIX" "3.0" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 }{CSTYLE "2D Math" -1 2 "Times" 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 }{CSTYLE "2D Output" 2 20 "" 0 1 0 0 255 1 0 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 256 "" 1 24 0 0 0 0 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 257 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 }{CSTYLE "" -1 258 "" 1 24 0 0 0 0 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 259 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 }{PSTYLE "Normal" -1 0 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Text Output" -1 2 1 {CSTYLE "" -1 -1 "Courier" 1 10 0 0 255 1 0 0 0 0 0 1 3 0 3 }1 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Heading 1" 0 3 1 {CSTYLE "" -1 -1 "" 1 18 0 0 0 0 0 1 0 0 0 0 0 0 0 }1 0 0 0 8 4 0 0 0 0 0 0 -1 0 } {PSTYLE "Warning" 2 7 1 {CSTYLE "" -1 -1 "" 0 1 0 0 255 1 0 0 0 0 0 0 1 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Error" 7 8 1 {CSTYLE "" -1 -1 "" 0 1 255 0 255 1 0 0 0 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Diagnostic" 7 9 1 {CSTYLE "" -1 -1 "" 0 1 64 128 64 1 0 0 0 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Mapl e Output" 0 11 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 }3 3 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "" 11 12 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 }1 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }} {SECT 0 {EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" }{TEXT 256 21 "Programmin g in Maple." }}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 15 "The if command." }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 56 "In Maple, the structure of the i f command is as follows:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT -1 17 "if boolean1 then" }}{PARA 0 "" 0 "" {TEXT -1 59 " one or more commands seperated by colons or semi-colons" }}{PARA 0 "" 0 "" {TEXT -1 18 "elif boolean2 then" }}{PARA 0 "" 0 "" {TEXT -1 59 " one or more commands seperated by colons or semi-colons" }} {PARA 0 "" 0 "" {TEXT -1 8 "elif ..." }}{PARA 0 "" 0 "" {TEXT -1 5 " \+ ..." }}{PARA 0 "" 0 "" {TEXT -1 4 "else" }}{PARA 0 "" 0 "" {TEXT -1 58 " one or more commands seperated by colons or semi-colons" }} {PARA 0 "" 0 "" {TEXT -1 6 "end if" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 194 "This entire construction is considered a s one Maple command (even though there can be many commands inside). T o run the command, you have to place a colon or semi-colon after it, a nd hit return." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 201 "There can be as many \"elif boolean then ...\" as you wa nt (there can also be 0). The output of the \"if\" command is the last evaluated expression. For example, if you give Maple the following if -command:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 11 "if x=0 then" }}{PARA 0 "" 0 "" {TEXT -1 12 " a:=3+5;" }}{PARA 0 "" 0 "" {TEXT -1 11 " b:=a+7" }}{PARA 0 "" 0 "" {TEXT -1 13 "eli f x=1 then" }}{PARA 0 "" 0 "" {TEXT -1 5 " 5" }}{PARA 0 "" 0 "" {TEXT -1 4 "else" }}{PARA 0 "" 0 "" {TEXT -1 6 " x^2" }}{PARA 0 "" 0 "" {TEXT -1 7 "end if;" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT -1 303 "then, if x=0 the commands a:=3+5;b:=a+7 will be e xecuted, the last expression was a+7 which is 3+5+7=15 so the output o f this if-command will be 15 if x=0. If x=1 then the \"command\" 5 is \+ the only thing that gets \"executed\", the output will be 5. In all ot her cases we reach \"else\" and the output is x^2." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 77 "Note that \"fi\" (that's \+ \"if\" in reverse order) in Maple is short for \"end if\"." }}{PARA 0 "" 0 "" {TEXT 257 29 "Remark for Maple 5 and older:" }{TEXT -1 265 " I n Maple 5 you can only end the \"if\" statement with \"fi\" and not wi th \"end if\". In Maple 6 and 7, you can choose between \"fi\" and \"e nd if\". So I'm typing \"fi\" now because that way it works in Maple 5 , 6 and 7, whereas typing \"end if\" would work with Maple 6 and 7." } }{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 8 "Example: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "x:=-3;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"xG!\"$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "if x < 0 then -x else x fi;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6# \"\"$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "x:='x';" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"xGF$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 27 "if x < 0 then -x else x fi;" }}{PARA 8 "" 1 "" {TEXT -1 30 "Error, cannot evaluate boolean" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 93 "Here an error is produced because the boolean \"x < 0\" c an not be simplified to true or false." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 26 "if false then 0 else 1 fi;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 227 "The com mand evalb (evaluate boolean) will try to determine if the input is t rue or false. If it can, the output is true or false. If it can not de termine that, then the output is just the input (or is equivalent to t he input)." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "evalb( 4 < 5) ;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#%%trueG" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "evalb(10! - 10^7 > 0);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#%&falseG" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 78 " evalb( 17 ); # 17 can never become either \"true\" or \"false\" and th us an error:" }}{PARA 8 "" 1 "" {TEXT -1 33 "Error, invalid boolean ex pression" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 193 "evalb( y^2+y<0 ); # right now we can not determine \"true\" or \"false\", but perhap s later in the future we can if some value would be assigned to y, so \+ no error message, it just returns the input" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#2,&*$)%\"yG\"\"#\"\"\"\"\"\"F'F*\"\"!" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 30 "if y^2+y < 0 then 0 else 1 fi;" }}{PARA 8 "" 1 "" {TEXT -1 30 "Error, cannot evaluate boolean" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 25 "The following command is:" }}{PARA 0 "" 0 "" {TEXT -1 93 "if \"we can evaluate if y^2+y<0 or not\" and \"y^2+y is indeed < 0\" then the output is 0 else 1." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 44 "if evalb(y^2+y < 0) = true then 0 else 1 fi;" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 47 "Note that the \"then\" part is not needed either." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "x:=-3;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"xG!\"$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 37 "if evalb(x < 0)=true then x:=-x fi;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"xG\"\"$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "x:='x';" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"xGF$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 37 "if evalb(x < 0)=true then x:=-x f i;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 121 "now evalb(x<0) was just x< 0 and was not equal to true, so the if-command executed no other comma nds, so nothing happened." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT -1 507 "When you have many elif's or if you have many Map le commands then for readability you might want to type the input over more than 1 line. There is one problem though, whenever you hit RETUR N, Maple will think it will have to start computing. If you want to mo ve to the next line but you have not yet finished typing the if comman d, then use shift-RETURN. So to enter an if command like the following , you need shift-returns to go to the next line. Only when the entire \+ command is entered can you use RETURN." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 359 "if 3^3 < 2^4 then # type shift return to get to the \+ next line\n a := 2^4 - 3^3; \n b := a^2;\n a + b # Note : a (semi)-colon is not needed here (*)\nelif 1 > 2 then\n print( \" this can not possibly be right\" );\n a := 11;\n b := 7+5 # N ote: a (semi)-colon is not needed here\nelse\n x^2 # Note: a (semi)-colon is not needed here\nfi;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#*$)%\"xG\"\"#\"\"\"" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 158 "(*) a (semi)-colon is not needed here because there is no command after \+ a+b. However, it is not an error if you do use one anyway, you could w rite a+b; here." }}}}{SECT 0 {PARA 3 "" 0 "" {TEXT -1 15 "The do comma nd." }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 62 "In Maple, the basic structu re of the do command is as follows:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" } }{PARA 0 "" 0 "" {TEXT -1 2 "do" }}{PARA 0 "" 0 "" {TEXT -1 37 " comm ands seperated by (semi)-colons" }}{PARA 0 "" 0 "" {TEXT -1 6 "end do " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 209 "Note : \"od\" (which is \"do\" in reverse order) is the same as \"end do\". In Maple 6 and 7 you can use both \"od\" and \"end do\", but in Maple 5 you can only use \"od\", which is what I will use in this Maple wor ksheet." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 184 "The entire construction is considered as one Maple command (even \+ though there can be many commands inside). To run it, end it with : or ; and hit return. In the following construction:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 2 "do" }}{PARA 0 "" 0 "" {TEXT -1 11 " command1;" }}{PARA 0 "" 0 "" {TEXT -1 11 " command2;" }}{PARA 0 "" 0 "" {TEXT -1 10 " command3" }}{PARA 0 "" 0 "" {TEXT -1 7 "end do;" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 85 "what will happen is: command1; command2; command3; command1; co mmand2; command3;....." }}{PARA 0 "" 0 "" {TEXT -1 334 "So that's an i nfinite loop. Now in interactive computations that's inconvenient but \+ sometimes acceptable because you can always click on \"STOP\" (in the \+ graphical version of Maple) or press control-c (in the text version of Maple) to interrupt the computation. But infinite loops are of course terrible inside non-interactive programs." }}{PARA 0 "" 0 "" {TEXT -1 122 "You can get out of a do-od (do .. end do) loop by the \"break \" command. The break command will exit the loop. For example:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 163 "i := 1;\nS := 0;\ndo\n if i < 4 then\n i:=i+1\n elif i < 30 then\n i:=i+10\n else \n print( \"i =\", i);\n break\n fi;\n S := S + i^2\nod; \nT := S^2;" }{TEXT -1 0 "" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"iG\" \"\"" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"SG\"\"!" }}{PARA 11 "" 1 " " {XPPMATH 20 "6#>%\"iG\"\"#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"SG \"\"%" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"iG\"\"$" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"SG\"#8" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"iG \"\"%" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"SG\"#H" }}{PARA 11 "" 1 " " {XPPMATH 20 "6#>%\"iG\"#9" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"SG \"$D#" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"iG\"#C" }}{PARA 11 "" 1 " " {XPPMATH 20 "6#>%\"SG\"$,)" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"iG \"#M" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"SG\"%d>" }}{PARA 11 "" 1 " " {XPPMATH 20 "6$Q$i~=6\"\"#M" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"T G\"(\\)HQ" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 344 "By the time we reac h \"else\", when i is no longer <30, the break command is executed. Wh en Maple sees \"break\", it will search for the next \"od\" or \"end d o\" and continue after that. So the commands between \"break\" and \"o d\" will be skipped. As you can see, after we hit \"break\" the comman d S:=S+i^2 was skipped and we proceeded straight to T:=S^2;" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 156 "The command \" next\" will also skip the rest of the loop, but will not break the loo p. So the next command brings you right back to the beginning of the l oop." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 87 "T here are two other ways to use do-od without having an infinite loop. \+ One is by using:" }}{PARA 0 "" 0 "" {TEXT -1 33 " for ... do\n \+ ...\n od;" }}{PARA 0 "" 0 "" {TEXT -1 21 "and the other way is:" }}{PARA 0 "" 0 "" {TEXT -1 43 " while boolean do\n ...\n \+ od;" }}{PARA 0 "" 0 "" {TEXT -1 58 "Note that for and while can als o be combined. For example:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 41 "S := \{seq(seq(i^2+j^2,i=j+1..4),j=1..4)\};" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"SG<(\"\"&\"#5\"#8\"#<\"#?\"#D" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "S:=sort([op(S)]);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"SG7(\"\"&\"#5\"#8\"#<\"#?\"#D" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 67 "T:=0;\nfor i in S while i<>17 do\n print( \+ \"i =\",i);\n T := T+i\nod;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"T G\"\"!" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"iG\"\"&" }}{PARA 11 "" 1 "" {XPPMATH 20 "6$Q$i~=6\"\"\"&" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#> %\"TG\"\"&" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"iG\"#5" }}{PARA 11 " " 1 "" {XPPMATH 20 "6$Q$i~=6\"\"#5" }}{PARA 11 "" 1 "" {XPPMATH 20 "6# >%\"TG\"#:" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"iG\"#8" }}{PARA 11 " " 1 "" {XPPMATH 20 "6$Q$i~=6\"\"#8" }}{PARA 11 "" 1 "" {XPPMATH 20 "6# >%\"TG\"#G" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"iG\"#<" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 106 "The \"for i in S\" will cause i to go th rough S. But the \"while i<>17\" will cause the loop to stop whenever: " }}{PARA 0 "" 0 "" {TEXT -1 86 " \"we are back at the beginning of t he loop, and the boolean i<>17 evaluates to false\"" }}{PARA 0 "" 0 " " {TEXT -1 76 "Half-way trough our list the boolean i<>17 becomes fals e and the loop stops." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 79 "See the help pages for \"for\" and \"while\" for other examples. Do this by typing:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "?for" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "?while" }}}} {SECT 0 {PARA 3 "" 0 "" {TEXT -1 10 "Procedures" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }{TEXT 258 0 "" }{TEXT -1 33 "In Maple, a procedure look s like:" }}{PARA 0 "" 0 "" {TEXT -1 21 " proc(a1,a2,...,an)" }} {PARA 0 "" 0 "" {TEXT -1 25 " local v1,v2,..,vk;" }}{PARA 0 "" 0 "" {TEXT -1 26 " global w1,w2,..,wl;" }}{PARA 0 "" 0 "" {TEXT -1 26 " options o1,o2,..os;" }}{PARA 0 "" 0 "" {TEXT -1 28 " \+ body of the procedure" }}{PARA 0 "" 0 "" {TEXT -1 11 " end proc" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 135 "In Map le 5 you must end the procedure with \"end\" and not with \"end proc\" . In Maple 6 and 7 you can use both \"end\" as well as \"end proc\"" } }{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 77 "Each of \+ the three lines \"local .. \", \"global ...\", \"options ...\" is opti onal." }}{PARA 0 "" 0 "" {TEXT -1 520 "local ...; specifies the loc al variables. A local variable is different from any variable outside \+ of the procedure even if the name is the same. In most procedures, mos t or all of the variables used inside the procedure are local. This wa y one prevents messing up global variables (the variables outside of t he procedure). Using global variables can cause some unpleasant surpri ses for the users, it causes that some variables change value when the procedure is called, which is in most cases not what a user expects. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 225 "Neve rtheless, it is sometimes convenient to use global variables. To indic ate to Maple that a variable is global, so that it does change variabl es that a user might be using, use global and specify the names of the variables." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 227 "It is allowed to use a variable inside a procedure without spe cifying if it is local or global. In that case Maple will guess if it \+ is local or global, and print its guess on the screen. This is conveni ent for short procedures." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT -1 496 "The body of the procedure consists of one or more commands, that are seperated by a colon or semicolon. It makes no dif ference if you use colon or semicolon. Note that the purpose of these \+ (semi)-colons is to seperate commands. Therefore it is not necessary t o use a (semi)-colon after the last command. However, if you use one a nyway even though it was not necessary, then Maple will not complain. \+ However, if you don't use a (semi)-colon where one is needed, then you will get an error message." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 145 "In the line \"options ...\" some options can b e specified. The most commonly used option is \"options remember;\" of which we will see examples soon." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 344 "The input of the procedure (in Maple tha t is called the \"arguments\") are specified after proc between ( ). N ote that n could be 0, one could have procedures with no arguments. Ev en if n=0 so you do something like proc() ... then you can still give arguments to that procedure, and those arguments are visibible inside the procedure as follows:" }}{PARA 0 "" 0 "" {TEXT -1 30 " nargs = \+ number of arguments" }}{PARA 0 "" 0 "" {TEXT -1 27 " args[i] = i'th \+ argument." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 158 "In most situations, if you want to use the procedure in a conveni ent way, you will have to give the procedure a name. In Maple, this is done by an assignment:" }}{PARA 0 "" 0 "" {TEXT -1 2 " " }{TEXT 259 54 "Defining a procedure is done by one single assignment." }}{PARA 0 "" 0 "" {TEXT -1 254 "And since an assignment is a Maple command, you \+ have to end this with a colon (nothing printed to the screen) or semi- colon (Maple will print the procedure). So if we want to define a proc edure and give it a name, we have to do something that looks like:" }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 45 "name_of_t he_procedure := proc(a1,a2,...,an)" }}{PARA 0 "" 0 "" {TEXT -1 25 " \+ local v1,v2,..,vk;" }}{PARA 0 "" 0 "" {TEXT -1 26 " global w1,w2,..,wl;" }}{PARA 0 "" 0 "" {TEXT -1 26 " options o1,o2,..o s;" }}{PARA 0 "" 0 "" {TEXT -1 21 " first_command;" }}{PARA 0 " " 0 "" {TEXT -1 22 " second_command;" }}{PARA 0 "" 0 "" {TEXT -1 10 " ..." }}{PARA 0 "" 0 "" {TEXT -1 19 " last_command " }}{PARA 0 "" 0 "" {TEXT -1 12 " end proc:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 133 "The output if the proced ure is always the same as the output of the last command that was exec uted inside the procedure. For example:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 66 "F := proc(x)\n if x=0 then\n infinity\n else\n \+ 1/x\n fi\nend;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"FGR6#%\"xG6 \"F(F(@%/9$\"\"!%)infinityG*&\"\"\"F/F+!\"\"F(F(F(" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "seq(F(i), i=-2..4);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6)#!\"\"\"\"#F$%)infinityG\"\"\"#F'F%#F'\"\"$#F'\"\"%" }} }{EXCHG {PARA 0 "" 0 "" {TEXT -1 88 "If x was 0 then the last \"comman d\" was infinity, and otherwise the last command was 1/x." }}{PARA 0 " " 0 "" {TEXT -1 48 "A procedure like this would not make much sense:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 73 "F := proc(x)\n if x=0 th en\n infinity\n else\n 1/x\n fi;\n x^2\nend;" }}{PARA 11 " " 1 "" {XPPMATH 20 "6#>%\"FGR6#%\"xG6\"F(F(C$@%/9$\"\"!%)infinityG*&\" \"\"F0F,!\"\"*$)F,\"\"#F0F(F(F(" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 125 "because the if-command is executed but that has no impact on the \+ output, the last evaluated expression is always x^2 anyways." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 222 "The \"return\" command ends a procedure right away. You can give an argument to retu rn. If you do, that will be the output. If you don't give an argument \+ to return, then the output of the procedure is NULL, which looks like: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 41 "NULL; # this prints not hing to the screen" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 5 "Note:" }} {PARA 0 "" 0 "" {TEXT -1 25 "In Maple 5 the syntax is:" }}{PARA 0 "" 0 "" {TEXT -1 18 " RETURN( output )" }}{PARA 0 "" 0 "" {TEXT -1 2 "or " }}{PARA 0 "" 0 "" {TEXT -1 11 " RETURN() " }}{PARA 0 "" 0 "" {TEXT -1 49 "if you want to return \"nothing\" i.e. return NULL." }}{PARA 0 "" 0 "" {TEXT -1 66 "This also works in Maple 6 and 7, however, in 6 a nd 7 you can use:" }}{PARA 0 "" 0 "" {TEXT -1 16 " return output" }} {PARA 0 "" 0 "" {TEXT -1 8 "or just:" }}{PARA 0 "" 0 "" {TEXT -1 9 " \+ return" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 8 "Example:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 139 "F := proc(x) \n if x=0 then RETURN(infinity) fi;\n # from now on we may assume th at x<>0 so the following will not give an error:\n 1/x\nend;" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}{PARA 11 "" 1 "" {XPPMATH 20 "6# >%\"FGR6#%\"xG6\"F(F(C$@$/9$\"\"!-%'RETURNG6#%)infinityG*&\"\"\"F3F,! \"\"F(F(F(" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "seq(F(i), i=- 1..2);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6&!\"\"%)infinityG\"\"\"#F%\" \"#" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 39 "Now for some more interest ing examples." }}{PARA 0 "" 0 "" {TEXT -1 17 "Example \"subsets\"" }} {PARA 0 "" 0 "" {TEXT -1 15 "Input: a set S." }}{PARA 0 "" 0 "" {TEXT -1 63 "Output: a set T such that every subset of S is an element of T. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 94 "Note \+ 1: If S has n elements, then there are precisely 2^n subsets of S. So \+ T has 2^n elements." }}{PARA 0 "" 0 "" {TEXT -1 113 "Note 2: If S = \{ \} is the empty set, then S has 1 subset, namely \{\}. So then T must \+ have \{\} as element. So T=\{\{\}\}." }}{PARA 0 "" 0 "" {TEXT -1 64 "N ote 3: S is always a subset of S, so S must be an element of T." }} {PARA 0 "" 0 "" {TEXT -1 89 "Note 4: If e is an element of S, and s is a subset of S then there are two possibilities:" }}{PARA 0 "" 0 "" {TEXT -1 40 " a) e is not in s\n b) e is in s." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 222 "To write an algorit hm for determining all subsets of S, it is sufficient to know only \"N ote 2\" and \"Note 4\". Notes 1 and 3 were completely unnecessary inf ormation. Using just note 2 and 4 gives us the following algorithm:" } }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 1287 "subsets := proc(S)\n l ocal e,U,T,i; # Note: if you replace \"local\" by \"global\" then\n \+ # the procedure will give wrong answers.\n if S = \{ \} then\n T := \{\{\}\} # because of Note 2.\n else\n \+ # At this point we may assume that S has an element.\n # This im plies that the following command will not\n # lead to an error: \n e := S[1];\n # Now e is some element of S. The rest of \+ S is:\n U := S minus \{e\};\n # We can now calculate all s ubsets of U by recursion.\n # Recursion means: call this procedu re from itself. This\n # is OK as long as U is \"smaller\" in so me sense as S,\n # because then the recursion must end at some p oint.\n T := subsets(U);\n # Notice that the elements of T are precisely those\n # subsets of S that do not have the eleme nt e.\n # Now we will use Note 4:\n T := T union \{seq(i u nion \{e\}, i=T)\}\n fi;\n # to check that our procedure is correc t we can now do\n # the following:\n if nops(T) <> 2^nops(S) then \n # the following command will interrupt the computation\n \+ # and will print an error message to the screen:\n ERROR( \"our p rocedure is completely wrong\" )\n fi;\n # Now that the output T i s sufficiently tested, lets return it:\n T\nend; " }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%(subsetsGR6#%\"SG6&%\"eG%\"UG%\"TG%\"iG6\"F-C %@%/9$<\">8&<#F2C&>8$&F16#\"\"\">8%-%&minusG6$F1<#F8>F4-F$6#F=>F4-%&un ionG6$F4<#-%$seqG6$-FG6$8'FA/FOF4@$0-%%nopsG6#F4)\"\"#-FT6#F1-%&ERRORG 6#QBour~procedure~is~completely~wrong6\"F4F-F-F-" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "S:=\{x,y,2\};" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"SG<%\"\"#%\"xG%\"yG" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "subsets(S);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#<*<\"<%\"\"#%\"xG %\"yG<#F&<$F'F(<#F'<#F(<$F&F'<$F&F(" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 52 "Same procedure but now shorter and without the test:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 199 "subsets := proc(S)\n loca l e,U,T,i;\n if S = \{\} then\n \{\{\}\}\n else\n e := S[1];\n U := S minus \{e\};\n T := procname(U);\n T union \{seq(i union \{e\}, i=T)\}\n fi\nend; " }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%(subsetsGR6#%\"SG6&%\"eG%\"UG%\"TG%\"iG6\"F-@%/9 $<\"<#F1C&>8$&F06#\"\"\">8%-%&minusG6$F0<#F5>8&-9!6#F:-%&unionG6$F@<#- %$seqG6$-FE6$8'F>/FMF@F-F-F-" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 245 " Note: the name \"procname\" refers to \"name of this procedure\". It i s convenient to call \"procname\" instead of \"subsets\" if you later \+ decide to use a different name for this procedure (then you don't have to make any changes inside the procedure)." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 3 "\{\};" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#<\"" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "subsets(%);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#<#<\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "subsets(%);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#<$<\"<#F$" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "subsets(%);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#<&<\"<#F$<#F%<$F$F%" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 16 "Another example:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 20 "Input: an integer n." }}{PARA 0 "" 0 "" {TEXT -1 40 "Output: the set of all lists, such that:" }}{PARA 0 "" 0 "" {TEXT -1 48 "1) the elements of lists are positive integers" }} {PARA 0 "" 0 "" {TEXT -1 47 "2) the sum of the elements of each list is n." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 8 " Remarks:" }}{PARA 0 "" 0 "" {TEXT -1 28 " n < 0 --> output is \{ \}" }}{PARA 0 "" 0 "" {TEXT -1 69 "because no such lists exists, so th e solution set is the empty set \{\}" }}{PARA 0 "" 0 "" {TEXT -1 33 " \+ n = 0 --> output is \{ [] \}" }}{PARA 0 "" 0 "" {TEXT -1 207 " b ecause only the empty list has sum 0, so the empty list is the only el ement of the solution set.\n n > 0. If n>0 and if L is a list in t he output, then L[1] must be one of the following numbers: 1,2..,n" }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 82 "These thr ee remarks are sufficient to write a complete algorithm for this probl em!" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 264 "sum_is_n := proc(n) \n local i,j;\n if n<0 then\n \{\}\n elif n=0 then\n \+ \{ [] \}\n else\n # Note: if i is a number and j is a list j=[a ,b,c,..]\n # then [i,op(j)] = [i,a,b,c,...]\n \{seq( seq( [i,op(j)], j = procname(n-i)), i=1..n)\}\n fi\nend;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%)sum_is_nGR6#%\"nG6$%\"iG%\"jG6\"F+@'29$\"\"!<\" /F.F/<#7\"<#-%$seqG6$-F66$7$8$-%#opG6#8%/F?-9!6#,&F.\"\"\"F;!\"\"/F;;F EF.F+F+F+" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "sum_is_n(1);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#<#7#\"\"\"" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "sum_is_n(2);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6# <$7$\"\"\"F%7#\"\"#" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "sum_ is_n(3);" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#<&7%\"\"\"F%F%7$F%\"\"#7$F 'F%7#\"\"$" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "sum_is_n(4); " }}{PARA 11 "" 1 "" {XPPMATH 20 "6#<*7&\"\"\"F%F%F%7%F%F%\"\"#7%F%F'F %7$F%\"\"$7%F'F%F%7$F'F'7$F*F%7#\"\"%" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 27 "Another example:\nFibonnaci:" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 11 "F(0)=F(1)=1" }}{PARA 0 "" 0 "" {TEXT -1 29 "n>1 -> F(n) = F(n-1) + F(n-2)" }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 149 "F := proc(n)\n if n=0 or n=1 then\n 1\n elif n>1 then\n F(n-1) + F(n-2)\n else\n ERROR( \"input should be a non-negative integer\" )\n fi\nend;" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#>%\"FGR6#%\"nG6\"F(F(@'5/9$\"\"!/F,\"\"\"F/2F/F,,&-F$6#,&F,F/!\" \"F/F/-F$6#,&F,F/!\"#F/F/-%&ERRORG6#QGinput~should~be~a~non-negative~i nteger6\"F(F(F(" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "F(5);" }} {PARA 11 "" 1 "" {XPPMATH 20 "6#\"\")" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 30 "F(24); # This takes some time." }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"&D](" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 121 "This pr ocedure is slow because many things are re-computed over and over agai n. To see this, issue the following command:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 61 "trace(F); # this will cause Maple to display th e computation" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#%\"FG" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "F(4);" }}{PARA 9 "" 1 "" {TEXT -1 22 "\{--> enter F, args = 4" }}{PARA 9 "" 1 "" {TEXT -1 22 "\{--> ente r F, args = 3" }}{PARA 9 "" 1 "" {TEXT -1 22 "\{--> enter F, args = 2 " }}{PARA 9 "" 1 "" {TEXT -1 22 "\{--> enter F, args = 1" }}{PARA 11 " " 1 "" {XPPMATH 20 "6#\"\"\"" }}{PARA 9 "" 1 "" {TEXT -1 26 "<-- exit \+ F (now in F) = 1\}" }}{PARA 9 "" 1 "" {TEXT -1 22 "\{--> enter F, args = 0" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}{PARA 9 "" 1 "" {TEXT -1 26 "<-- exit F (now in F) = 1\}" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"#" }}{PARA 9 "" 1 "" {TEXT -1 26 "<-- exit F (now in F) = 2 \}" }}{PARA 9 "" 1 "" {TEXT -1 22 "\{--> enter F, args = 1" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}{PARA 9 "" 1 "" {TEXT -1 26 "<-- e xit F (now in F) = 1\}" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"$" }} {PARA 9 "" 1 "" {TEXT -1 26 "<-- exit F (now in F) = 3\}" }}{PARA 9 " " 1 "" {TEXT -1 22 "\{--> enter F, args = 2" }}{PARA 9 "" 1 "" {TEXT -1 22 "\{--> enter F, args = 1" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\" \"" }}{PARA 9 "" 1 "" {TEXT -1 26 "<-- exit F (now in F) = 1\}" }} {PARA 9 "" 1 "" {TEXT -1 22 "\{--> enter F, args = 0" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"\"" }}{PARA 9 "" 1 "" {TEXT -1 26 "<-- exit F (n ow in F) = 1\}" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"#" }}{PARA 9 "" 1 "" {TEXT -1 26 "<-- exit F (now in F) = 2\}" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"&" }}{PARA 9 "" 1 "" {TEXT -1 34 "<-- exit F (now a t top level) = 5\}" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"\"&" }}} {EXCHG {PARA 0 "" 0 "" {TEXT -1 121 "As you can see (especially if you type F(6) or F(7) or so), the procedure F is entered several times wi th the same input." }}{PARA 0 "" 0 "" {TEXT -1 12 "If you type:" }} {PARA 0 "" 0 "" {TEXT -1 7 " F(30)" }}{PARA 0 "" 0 "" {TEXT -1 191 "t hen F(10) will be computed thousands of times. And F(5) will be called even more often. You can prevent Maple from recomputing the same resu lt by putting \"options remember\" in the procedure." }}{PARA 0 "" 0 " " {TEXT -1 181 "With \"options remember\", whenever an input is encoun tered that has already been calculated before, Maple will not run the \+ procedure but simply give the same output as it had before." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 148 "So if you type F(30) with the following F, then F(10) will not be computed thousands of times, but only once, which should of course be much faster." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 169 "F := proc(n)\n options rem ember;\n if n=0 or n=1 then\n 1\n elif n>1 then\n F(n-1) + F (n-2)\n else\n ERROR( \"input should be a non-negative integer\" \+ )\n fi\nend;" }}{PARA 12 "" 1 "" {XPPMATH 20 "6#>%\"FGR6#%\"nG6\"6#%) rememberGF(@'5/9$\"\"!/F.\"\"\"F12F1F.,&-F$6#,&F.F1!\"\"F1F1-F$6#,&F.F 1!\"#F1F1-%&ERRORG6#QGinput~should~be~a~non-negative~integer6\"F(F(F( " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 30 "F(40); # takes almost n o time:" }}{PARA 11 "" 1 "" {XPPMATH 20 "6#\"*T,el\"" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 240 "With the previous procedure F, computing F(40) would have taken \"forever\" (not really forever, it would finish in \+ a finite amount of time but it would take longer than we are willing t o wait, which in practise is the same thing as forever)." }}}{PARA 0 " " 0 "" {TEXT -1 0 "" }}}}{MARK "3 78 0" 0 }{VIEWOPTS 1 1 0 1 1 1803 }