#Kafung Mok #HW4.txt: Feb. 3 2014. Help:=proc(): print(` PadZeros(A), MatMultRG(A,B), RandMat(n,R) `) : end: # # # # # # # # # Question 1 # # # # # # # # # # sum('i', 'i' = 1 .. 10) # f := proc (x) local z; # if 0 < x and x <= 1 then # z := x^2 # else # z := 1-x # fi: # RETURN(z) # end: #f(1/2); #f(2); # plot(f,-2,2); #v := Int(4*Pi*x*sqrt(-x^2+1), x = 0 .. r) #v := value(v); #rrs := solve(v = 2*Pi*(1/3), r) #convert(rrs[2],radical); #with(plots); #animate3d(cos(t*y+x), x = 0 .. Pi, y = -Pi .. Pi, t = 1 .. 2); # # # # # # # # # Question 3 # # # # # # # # # # PadZeros(A): given a square matrix A pads it with zero to make its # dimension a powr of 2 PadZeros:=proc(A) local i,n,n1: if nops(A)<>nops(A[1]) then print(`not a square matrix`): RETURN(FAIL): fi: n:=nops(A): if type(log[2](n),integer) then A: else n1:=2^ceil(log[2](n)): [seq([op(A[i]),0\$(n1-n)],i=1..nops(A)),[0\$n1]\$(n1-n)]: fi: end: # MatMultRG(A,B): multiplies two square matrices, A and B, # of the dimension, not necessarily a power of 2. MatMultRG:= proc(A,B) local n, Anew, Bnew, i, j: n:= nops(A): Anew:= PadZeros(A): Bnew:= PadZeros(B): [seq([seq(MatMultRG(Anew,Bnew)[i][j], j = 1 .. n)], i = 1 .. n)]: end: # # # # # # # # # Question 4 # # # # # # # # # # RandMat(n,R): a random m by n matrix with entries between 0 and R # given as a list of lists. RandMat:=proc(m,n,R) local ra,i,j: ra:=rand(0..R): [seq([seq(ra(),j=1..n)],i=1..n)]: end: