# # 7.1 The MAPLE procedure # # # # MAPLE SESSION 1 # > f2c := proc(x) > evalf(5/9*(x - 32)); > end proc; # # # MAPLE SESSION 2 # > f2c := proc(x) > evalf(5/9*(x - 32)); > end; # # # MAPLE SESSION 3 # > fc2(32); > fc2(100); > f2c(60); > f2c(70); # # # MAPLE SESSION 4 # > c2f := > ; # # # MAPLE SESSION 5 # > c2f(0); > c2f(37.8); > c2f(100); > c2f(f2c(100)); # # 7.1.1 Local and global variables # # # # MAPLE SESSION 6 # > g := proc() > local a; > a := exp(2); > evalf(a); > end; # # # MAPLE SESSION 7 # > g(); > a; # # # MAPLE SESSION 8 # > h := proc() > global a; > a := exp(2); > evalf(a); > end; # # # MAPLE SESSION 9 # > h(); > a; # # # MAPLE SESSION 10 # > dist := proc(x1,y1,x2,y2) > > > > end proc; # # # MAPLE SESSION 11 # > dist(1,3,13,-4); # # 7.2 Conditional statements # # # # MAPLE SESSION 12 # > x:=1; > if x>0 then > y:=x+1 > else > y:=x-1 > end if: > y; # # # MAPLE SESSION 13 # > f := proc(x) > if x > 1 then > x^2; > else > (1-x^3); > end if; > end proc; # # # MAPLE SESSION 14 # > f(2); > f(-3); # # 7.2.1 Boolean expressions # # # # MAPLE SESSION 15 # > f := proc(x) > if 0 x; > else > 0; > end if; > end proc: # # # MAPLE SESSION 16 # > f(-1/2), f(0), f(1/2), f(1), f(3/2); # # # MAPLE SESSION 17 # > g := proc(x) > if x>2 then > x^2-3*x+2; > else > if 0 1 - x^3; > else > x^3; > end if; > end if; > end proc; # # # MAPLE SESSION 18 # > g := proc(x) > if x > 2 then x^2 - 3*x + 2; > elif x>0 and x<=2 then 1-x^3; > else x^3; > end if; > end proc; # # # MAPLE SESSION 19 # > plot(g, -1..3, discont=true); # # # MAPLE SESSION 20 # > plot(g(x), x=-1..3, discont=true); # # 7.3 The ``for'' loop # # # # MAPLE SESSION 21 # > SUM := proc(n) > local i, tot; > tot := 0; > for i from 1 to n do > tot := tot + i; > end do; > tot; > end proc; # # # MAPLE SESSION 22 # > 1+2+3+4+5+6+7+8+9+10; > SUM(10); # # # MAPLE SESSION 23 # > printlevel; # # # MAPLE SESSION 24 # > printlevel := 20; > SUM(10); # # # MAPLE SESSION 25 # > printlevel := 1; # # # MAPLE SESSION 26 # > SUM := proc(n) > local i, tot; > tot := 0; > for i from 1 to n do > tot := tot + i; > print(`i=`,i,` tot=`,tot); > end do; > tot; > end proc; > SUM(10); # # # MAPLE SESSION 27 # > for i from 2 by 5 to 24 do > print(i); > end do; # # # MAPLE SESSION 28 # > SUM := proc(a,b) > > > > > > > end proc; # # # MAPLE SESSION 29 # > SUM(1,10); > SUM(15,29); # # # MAPLE SESSION 30 # > ODDSUM := proc(n) > > > > > > > > end proc; # # # MAPLE SESSION 31 # > 1+3+5+7+9+11+13+15+17+19; > ODDSUM(19); # # 7.4 Type declaration # # # # MAPLE SESSION 32 # > SUM := proc(n::posint) > local i, tot; > tot := 0; > for i from 1 to n do > tot := tot + i; > end do; > tot; > end proc; # # # MAPLE SESSION 33 # > SUM(10); > SUM(sqrt(2)); # # # MAPLE SESSION 34 # > find2s := proc(p::.......) > local ................ > find := 0; > for a from 1 to trunc(sqrt(p/2)) do > c := p - a^2: > if issqr(c) then > print(`p = a^2 + b^2 where a=`,a,......); > find := 1: > end if; > end do; > if find=0 then > .....................; > end if; > end proc; # # # MAPLE SESSION 35 # > find2s(3); > find2s(13); > find2s(17); # # 7.5 The ``while'' loop # # # # MAPLE SESSION 36 # > binpow := proc(n::posint) > local x,m: > x:=0: > m:=n: > while m>=1 do > m := m/2: > x:= x + 1: > end do: > x - 1; > end: # # # MAPLE SESSION 37 # > for n from 1 to 8 do > n, binpow(n); > od; # # # MAPLE SESSION 38 # > euclid := proc(m::posint,n::posint) > local a,b,r: > a:=m: > b:=n: > r:=irem(a,b): > while r>0 do > a:=b: > b:=r: > r:=irem(a,b): > end do: > b: > end proc: # # # MAPLE SESSION 39 # > a:=281474439315457: > b:=33685115929: > euclid(a,b); > igcd(a,b); # # # MAPLE SESSION 40 # > a:=53; r:=irem(a,2); > a:=iquo(a,2); r:=irem(a,2); > a:=iquo(a,2); r:=irem(a,2); > a:=iquo(a,2); r:=irem(a,2); > a:=iquo(a,2); r:=irem(a,2); > a:=iquo(a,2); r:=irem(a,2); > a:=iquo(a,2); r:=irem(a,2); # # # MAPLE SESSION 41 # > convert(53,binary); # # 7.6 Recursive procedures # # # # MAPLE SESSION 42 # > restart; > FIB := proc(n::nonnegint) > if n<2 then > n; > else > FIB(n-1) + FIB(n-2); > end if; > end proc; # # # MAPLE SESSION 43 # > seq(FIB(n), n=0..15); # # # MAPLE SESSION 44 # > time(FIB(20)); # # # MAPLE SESSION 45 # > FIB := proc(n::nonnegint) > option remember; > if n<2 then > n; > else > FIB(n-1) + FIB(n-2); > end if; > end proc; # # # MAPLE SESSION 46 # > time(FIB(20)); > time(FIB(200)); # # # MAPLE SESSION 47 # > MYBINOM := proc(n::nonnegint,m::nonnegint) > if n >= 1 and m <= n-1 and ..... then > ............................. ; > else > ..... ; > end if: > end proc; # # 7.7 Explicit return # # # # MAPLE SESSION 48 # > f := proc(x) > if x < 0 then > return x; > end if; > if x >= 0 and x <= 1 then > return 1 - x; > end if; > if x > 1 then > return x^2; > end if; > end proc; # # # MAPLE SESSION 49 # > f(-0.4), f(0.3), f(2); # # 7.8 Error statement # # # # MAPLE SESSION 50 # > SCORETOT := proc(L::list) > local num, TOT, k; > num := nops(L): > if num = 5 then > TOT := sum(L[k],k=1..5); > return TOT; > else > error "L must have have 5 entries. Your L had %1.", > num; > end if; > end proc; # # # MAPLE SESSION 51 # > L := [48,36.5,24,43,36]; > SCORETOT(L); # # # MAPLE SESSION 52 # > L:=[48,36.5,24,43]; > SCORETOT(L); # # # MAPLE SESSION 53 # > SCORETOT := proc(L::list,M::list) > local num, TOT, k, numlabs, numtots, A, j, TOTPOSS, > TOTPER; > numlabs:=nops(L); > numtots:=nops(M): > lprint(`LAB GRADE COMPUTATION`); > lprint(`ASSUMPTIONS: Overall grade is computed by `); > lprint(`summing all lab scores, then dividing by`); > lprint(`the total possible and then converting to a`); > lprint(`percentage.`); > A:=matrix(numlabs+1,4); > A[1,1]:=`LAB`: A[1,2]:=`Raw Score`: > A[1,3]:=`Total Possible`: > A[1,4]:=`Score as percentage`: > if numlabs=numtots then > for j from 1 to numlabs do > A[j+1,1]:=j: A[j+1,2]:=L[j]: > A[j+1,3]:=M[j]: > A[j+1,4]:=evalf(L[j]/M[j]*100,4): > end do; > print(A); > TOT:=sum(L[k],k=1..numlabs); > TOTPOSS:=sum(M[k],k=1..numlabs): > TOTPER:=evalf(TOT/TOTPOSS*100,4); > lprint(`Total of lab scores = `,TOT); > lprint(`Total possible score = `,TOTPOSS); > lprint(`TOTAL score as a percentage = `,TOTPER); > else > error "numlabs must equal numtots but here" > "numlabs=%1 and numtots=%2.",numlabs,numtots; > end if; > end proc; # # # MAPLE SESSION 54 # > L:=[18,20,14.5];M:=[20,30,15]; > SCORETOT(L,M); # # # MAPLE SESSION 55 # > WSCORETOT := proc(L::list,M::list) > local num, TOT, k, numlabs, numtots, A, j, TOTPOSS, > TOTPER; > numlabs:=nops(L); > numtots:=nops(M): > lprint(`LAB GRADE COMPUTATION`); > lprint(`ASSUMPTIONS: Overall grade is computed by `); > lprint(`.........................................`); > lprint(`............................`); > A:=matrix(numlabs+1,4); > A[1,1]:=`LAB`: A[1,2]:=`Raw Score`: > A[1,3]:=`Total Possible`: > A[1,4]:=`Score as percentage`: > if numlabs=numtots then > for j from 1 to numlabs do > if ......... then > error "Score for LAB %1 = %2. This is too big" > "since the total possible for this lab is ...", > .,....,....; > end if; > A[j+1,1]:=j: A[j+1,2]:=L[j]: > A[j+1,3]:=M[j]: > A[j+1,4]:=evalf(L[j]/M[j]*100,4): > end do; > print(A); > LABSUM:=..............................); > LBS:=evalf(LABSUM,4); > WSCORE:=evalf(..............,4); > lprint(`The sum of the lab scores`); > lprint(`as percentages = `,...); > lprint(`Overall score as a percentage = `,......); > else > error "numlabs must equal numtots but here" > "numlabs=%1 and numtots=%2.",numlabs,numtots; > end if; > end proc; # # # MAPLE SESSION 56 # > L:=[18,20,14.5];M:=[20,30,15]; > WSCORETOT(L,M); # # 7.9 args and nargs # # # # MAPLE SESSION 57 # > zol := proc() > print("The args of this proc are",args); > print("The first arg is",args[1]); > print("The number of args is",nargs); > end; > zol(a,b); > zol(b,a,a); # # # MAPLE SESSION 58 # > myevalf:=proc() > if nargs=0 or nargs>2 then > error "This proc only takes 1 or 2 arguments." > "Number of args supplied = %1", nargs; > else > if nargs=2 then > evalf(args[1],args[2]): > else > evalf(args[1],4): > end if; > end if; > end proc; > myevalf(Pi); > myevalf(Pi,10); # # 7.10 Input and output # 7.10.1 Formatted Output # # # # MAPLE SESSION 59 # > x:=2^12; > printf("%d",x); # # # MAPLE SESSION 60 # > printf("%d\n",x); > printf("x=%d\n",x); > printf("x=%10d\n",x); > printf("x=%010d\n",x); # # # MAPLE SESSION 61 # > y:=1/x; > evalf(y); > printf("y=%e\n",y); > printf("y=%20e\n",y); > printf("y=%20.9e\n",y); > printf("x=%d and y=%e.\n",x,y); > printf("x=%d and \ny=%e.\n",x,y); # # # MAPLE SESSION 62 # > printf("Integral is %a.\n",int(1/t,t)); # # 7.10.2 Interactive input # # # # MAPLE SESSION 63 # > methodget:=proc() > local p: > printf("There are two available methods:\n"); > printf("1. Undetermined coefficients\n"); > printf("2. Laplace transforms\n"); > printf("ENTER 1 or 2: "); > p:=readline(terminal); > if p="1" or p="2" then > return p; > else > error "You must enter 1 or 2." > end if; > end; > methodget(); > whattype(%); # # # MAPLE SESSION 64 # > numget:=proc() > local p: > p:=readstat("Enter an integer: "); > if type(p,integer) then > return p; > else > error "You must enter an integer." > end if; > end; > numget(); # # # MAPLE SESSION 65 # > s:="sin(x^3+2*x)"; > whattype(%); > parse(s); > whattype(%); # # # MAPLE SESSION 66 # > varget(); > varget(); # # 7.10.3 Reading commands from a file # # # # MAPLE SESSION 67 # > read "c:\\Program Files\\Maple 6\\myprogs\\stot"; # # 7.10.4 Reading data from a file # # # # MAPLE SESSION 68 # > M:=ImportMatrix("mydata.txt",delimiter=" "); > whattype(%); > datapts:=[seq([M[i,1],M[i,2]],i=1..7)]; > plot(datapts,style=point,symbol=circle,color=black); # # # MAPLE SESSION 69 # > L:=readdata("mydata.txt",2); > whattype(L); > plot(L,style=point); # # 7.10.5 Writing data to a file # # # # MAPLE SESSION 70 # > M:=Matrix(7,2); > for i from 0 to 6 do > x:=sin(Pi*i/12)^3: > y:=cos(Pi*i/12)^3: > M[i+1,1]:=evalf(x,3): > M[i+1,2]:=evalf(y,3): > od: > ExportMatrix("mydata2.txt", M); # # 7.10.6 Writing and saving to a file # # # # MAPLE SESSION 71 # > restart; > y:=int(1/x,x); > writeto("myoutput"); > y; > writeto(terminal); > y; # # # MAPLE SESSION 72 # > x:=5; > y:=7; > z:=int(1/u,u); > save "first.m"; > save x,y,part1; > quit # # # MAPLE SESSION 73 # > read part1; > anames(); > read "first.m"; > z; # # 7.11 Generating C and Fortran code # # # # MAPLE SESSION 74 # > restart: > with(codegen): # # # MAPLE SESSION 75 # > s:=series(BesselJ(0,x),x=1,6): > p:=convert(s,polynom): > ep:=evalf(p); # # # MAPLE SESSION 76 # > f:=makeproc(ep,[x]); # # # MAPLE SESSION 77 # > fortran(f,optimized); # # # MAPLE SESSION 78 # > C(f); # # 7.12 Viewing built-in MAPLE code # # # # MAPLE SESSION 79 # > interface(verboseproc=2); > op(GAMMA); # # 7.13 The MAPLE interactive debugger # # # # MAPLE SESSION 80 # > euclid := proc(m::posint,n::posint) > local a,b,r: > a:=m: > b:=n: > r:=irem(a,b): > while r>0 do > a:=b: > b:=r: > r:=irem(a,b): > end do: > r: > end proc: > euclid(45,51); # # # MAPLE SESSION 81 # > showstat(euclid); # # # MAPLE SESSION 82 # > stopat(euclid); > euclid(45,51); # # # MAPLE SESSION 83 # # # # MAPLE SESSION 84 # > # # # MAPLE SESSION 85 # # # # MAPLE SESSION 86 # # # # MAPLE SESSION 87 # > unstopwhen(); # # 7.14 Writing your own packages # 7.14.1 Packages as tables # # # # MAPLE SESSION 88 # > qprod:=table(); > qprod[aqprod]:=proc(a,q,n) > local x,i: > if type(n,nonnegint) then > x:=1: > for i from 1 to n do > x := x * (1-a*q^(i-1)): > end do: > else > x:=``(a,q)[n]; > end if: > return x: > end proc: > qprod[etaq]:=proc(q,i,trunk) > # This proc returns (q^ i:q^ i)\_inf up to q^ trunk > local k,x,z1,z,w: > z1:=(i + sqrt( i*i + 24*trunk*i ) )/(6*i): > z:=1+trunc( evalf(z1) ): > x:=0: > for k from -z to z do > w:=i*k*(3*k-1)/2: > if w<=trunk then > x:=x+ q^( w )*(-1)^ k: > end if: > end do; > return x: > end proc: > qprod[qbin]:=proc(q,m,n) > if whattype(m)=integer and whattype(n)=integer then > if m>=0 and m<=n then > return normal(aqprod(q,q,n)/aqprod(q,q,m) > /aqprod(q,q,n-m)); > else > return 0; > end if: > else > error "m and n must be integers."; > end if: > end proc: > save( qprod, > "c:\\Program Files\\Maple 6\\mylib\\qprod.m"); # # # MAPLE SESSION 89 # > with(qprod); > qbin(q,3,6); # # 7.14.2 Modules for packages # # # # MAPLE SESSION 90 # > read "etaprod.txt"; > etaproduct:-cuspmake(20); # # # MAPLE SESSION 91 # > ep:=(eta(5*tau)/eta(tau))^6; > etaproduct:-etaprodcuspord(ep,20); # # # MAPLE SESSION 92 # > libname; # # # MAPLE SESSION 93 # > read "etaprod.txt"; > march('create', mylib, 100); # # # MAPLE SESSION 94 # > savelibname := mylib; > savelib('etaproduct'); > quit # # # MAPLE SESSION 95 # > with(etaproduct); > ep:=(eta(5*tau)/eta(tau))^6; > GPmake(ep); # # 7.15 Answers to programming exercises # # # # MAPLE SESSION 96 # > c2f := proc(x) > evalf(9/5*x + 32); > end; # # # MAPLE SESSION 97 # > h:=proc(x) > if x>3 then 0; > elif x>0 and x<=1 then x; > elif x>1 and x<=3 then 1; > else > -1; > end if; > end proc; > plot(h, -1..4, discont=true); # # # MAPLE SESSION 98 # > SUM := proc(a,b) > local i, tot; > tot := 0; > for i from a to b do > tot := tot + i; > end do; > tot; > end proc; > SUM(1,10); > $ > 55 > $ > SUM(15,19); > $ > 85 > $ # # # MAPLE SESSION 99 # > ODDSUM := proc(n) > local i, tot; > tot := 0; > for i from 1 by 2 to n do > tot := tot + i; > end do; > tot; > end proc; > ODDSUM(19); > $ > 100 > $ > for k from 1 by 2 to 19 do > print(`k=`,k,` ODDSUM=`,ODDSUM(k)); > end do; # # # MAPLE SESSION 100 # > find2s := proc(p::prime) > local a,c,find; > find := 0; > for a from 1 to trunc(sqrt(p/2)) do > c := p - a^2: > if issqr(c) then > print(`p = a^2 + b^2 where a=`,a, > ` b=`,sqrt(c)); > find := 1: > end if; > end do; > if find=0 then > print(`p is not the sum of two squares`); > end if; > end proc; # # # MAPLE SESSION 101 # > conbin:=proc(n::posint) > local m,x,y,r; > m:=n: > x:=0: > y:=1: > while m>0 do > r:=irem(m,2): > m:=iquo(m,2): > x:=x + r*y: > y:=10*y: > end do: > x; > end; # # # MAPLE SESSION 102 # > MYBINOM := proc(n::nonnegint,m::nonnegint) > if n >= 1 and m <= n-1 and m >= 1 then > MYBINOM(n-1,m-1) + MYBINOM(n-1,m); > else > 1 ; > end if: > end; > MYBINOM(20,8); > 20!/8!/12!; # # # MAPLE SESSION 103 # > GRADE := proc(x) > if x >= 90 then return `A`; end if; > if x >= 85 and x < 90 then return `B+`; end if; > if x >= 80 and x < 85 then return `B`; end if; > if x >= 75 and x < 80 then return `C+`; end if; > if x >= 70 and x < 75 then return `C`; end if; > if x >= 60 and x < 70 then return `D`; end if; > if x < 60 then return `E`; end if; > end proc; # # # MAPLE SESSION 104 # > WSCORETOT := proc(L::list,M::list) > local num, TOT, k, numlabs, numtots, A, j, TOTPOSS, > TOTPER; > numlabs:=nops(L); > numtots:=nops(M): > lprint(`LAB GRADE COMPUTATION`); > lprint(`ASSUMPTIONS: Overall grade is computed by `); > lprint(`converting each lab score to a percentage`); > lprint(`and then taking the average.`); > A:=matrix(numlabs+1,4); > A[1,1]:=`LAB`: A[1,2]:=`Raw Score`: > A[1,3]:=`Total Possible`: > A[1,4]:=`Score as percentage`: > if numlabs=numtots then > for j from 1 to numlabs do > if L[j]>M[j] then > error "Score for LAB %1 = %2. This is too big" > "since the total possible for this lab is %3.", > j,L[j],M[j]; > end if; > A[j+1,1]:=j: A[j+1,2]:=L[j]: > A[j+1,3]:=M[j]: > A[j+1,4]:=evalf(L[j]/M[j]*100,4): > end do; > print(A); > LABSUM:=sum(L[k]/M[k]*100,k=1..numlabs); > LBS:=evalf(LABSUM,4); > WSCORE:=evalf(LABSUM/numlabs,4); > lprint(`The sum of the lab scores`); > lprint(`as percentages = `,LBS); > lprint(`Overall score as a percentage = `,WSCORE); > else > error "numlabs must equal numtots but here" > "numlabs=%1 and numtots=%2.",numlabs,numtots; > end if; > end proc; # # # MAPLE SESSION 105 # > varget:=proc() > local p,q: > printf("ENTER an independent variable: "); > p:=readline(terminal); > q:=parse(p); > if not type(q,name) then > error "You must enter a variable." > "You entered a %1.",whattype(q); > else > return q; > end if; > end;