#Homework 4 #Sunday February 9, 2013 #HA LUU ############################################################# #Note: I worked with Sowmya on this homework# #2) #MatMultRG(A,B): Multiply two square matrix using MatMultR(A,B) MatMultRG:=proc(A,B) local i,j,n1,C,Apad,Bpad: if nops({seq(nops(A[i]),i=1..nops(A))})<>1 then print(`Rows do not all the same lengths, hence`, A, `is not a matrix `): RETURN(FAIL): fi: if nops({seq(nops(B[i]),i=1..nops(B))})<>1 then print(`Rows do not all the same lengths, hence`, B, `is not a matrix `): RETURN(FAIL): fi: if nops(A[1])<> nops(B) then print (`number of column of`,A,`is not the same as the number of rows of`,B): print(`can't multiply these two matrices`): RETURN(FAIL): fi: n1:= nops(A): Apad:= PadZeros(A) : Bpad:= PadZeros(B) : C:= MatMultR(Apad,Bpad): [seq([seq(C[i][j],j=1..n1)],i=1..n1)] end: ###################################################################### ###################################################################### #3 #MatMinus(A,B): Subtract two matrices of the same size given as lists of #lists MatMinus:=proc(A,B) local i,j: if nops(A)<>nops(B) then print(`Not the same number of rows`): RETURN(FAIL): fi: if nops({seq(nops(A[i]),i=1..nops(A))})<>1 then print(`Rows do not all the same lengths, hence`, A, `is not a matrix `): RETURN(FAIL): fi: if nops({seq(nops(B[i]),i=1..nops(B))})<>1 then print(`Rows do not all the same lengths, hence`, B, `is not a matrix `): RETURN(FAIL): fi: if nops(A)>0 and nops(A[1])<>nops(B[1]) then print(`Not the same number of columns`): RETURN(FAIL): fi: [seq([seq(A[i][j]-B[i][j],j=1..nops(A[i]))],i=1..nops(A))] end: ####################################################################### #3 #StrassenR(A,B): multiplies two square matrices A and B whose #dimension is multiple of 2 using Strassen Method StrassenR:=proc(A,B) local n,i,A11,A12,A21,A22,B11,B12,B21,B22, M1,M2,M3,M4,M5,M6,M7,C11,C12,C21,C22: n:=nops(A): if nops(B)<>n then RETURN(FAIL): fi: if not type(log[2](n),integer) then RETURN(FAIL): fi: if n=1 then RETURN([[A[1][1]*B[1][1]]]): fi: A11:=[seq([op(1..n/2,A[i])],i=1..n/2)]: A12:=[seq([op(n/2+1..n,A[i])],i=1..n/2)]: A21:=[seq([op(1..n/2,A[i])],i=n/2+1..n)]: A22:=[seq([op(n/2+1..n,A[i])],i=n/2+1..n)]: B11:=[seq([op(1..n/2,B[i])],i=1..n/2)]: B12:=[seq([op(n/2+1..n,B[i])],i=1..n/2)]: B21:=[seq([op(1..n/2,B[i])],i=n/2+1..n)]: B22:=[seq([op(n/2+1..n,B[i])],i=n/2+1..n)]: M1:=StrassenR(MatAdd(A11,A22),MatAdd(B11,B22)): M2:=StrassenR(MatAdd(A21,A22),B11): M3:=StrassenR(A11,MatMinus(B12,B22)): M4:=StrassenR(A22,MatMinus(B21,B11)): M5:=StrassenR(MatAdd(A11,A12),B22): M6:=StrassenR(MatMinus(A21,A11),MatAdd(B11,B12)): M7:=StrassenR(MatMinus(A12,A22),MatAdd(B21,B22)): C11:=MatAdd(MatMinus(MatAdd(M1,M4),M5),M7): C12:=MatAdd(M3,M5): C21:=MatAdd(M2,M4): C22:=MatAdd(MatAdd(MatMinus(M1,M2),M3),M6): [seq([op(C11[i]),op(C12[i])],i=1..nops(C11)), seq([op(C21[i]),op(C22[i])],i=1..nops(C21))]: end: ######################################################################### #4 #Strassen(A,B): Multiply two square matrix using StrassenR(A,B) Strassen:=proc(A,B) local i,j,n1,C,Apad,Bpad: if nops({seq(nops(A[i]),i=1..nops(A))})<>1 then print(`Rows do not all the same lengths, hence`, A, `is not a matrix `): RETURN(FAIL): fi: if nops({seq(nops(B[i]),i=1..nops(B))})<>1 then print(`Rows do not all the same lengths, hence`, B, `is not a matrix `): RETURN(FAIL): fi: if nops(A[1])<> nops(B) then print (`number of column of`,A,`is not the same as the number of rows of`,B): print(`can't multiply these two matrices`): RETURN(FAIL): fi: n1:= nops(A): Apad:= PadZeros(A) : Bpad:= PadZeros(B) : C:= StrassenR(Apad,Bpad): [seq([seq(C[i][j],j=1..n1)],i=1..n1)] end: ####################################################################### #5 #RandMatS(n,R): a random m by n matrix with entries between 0 and R #given as a list of lists RandMatS:=proc(n,R) local ra,i,j: ra:=rand(0..R): [seq([seq(ra(),j=1..n)],i=1..n)]: end: ######################################################################## #6 #A:=RandMatS(64,100): #B:=RandMatS(64,100): #time(Strassen(A,B)); #4.406 #time(MatMultR(A,B)); #4.750 # This clearly show a difference in efficiency(time) between the two ways of matrix multiplication. ####################################################################### #1 #A:=matrix(3,4,[1,1,3,-3,5,5,13,-7,3,1,7,-11]); #A := matrix([[1, 1, 3, -3], [5, 5, 13, -7], [3, 1, 7, -11]]) #A1:=addrow(A,1,2,-5); #A1 := matrix([[1, 1, 3, -3], [-5, -5, -17, 23], [3, 1, 7, -11]]) #A2:=addrow(A1,1,3,-3); #A2 := matrix([[1, 1, 3, -3], [-5, -5, -17, 23], [0, -2, -2, -2]])# #A3:=mulrow(A2,3,-1/2); #A3 := matrix([[1, 1, 3, -3], [-5, -5, -17, 23], [0, 1, 1, 1]]) #A4:=swaprow(A3,2,3); #A4 := matrix([[1, 1, 3, -3], [0, 1, 1, 1], [-5, -5, -17, 23]]) #gausselim(A); #matrix([[1, 1, 3, -3], [0, -2, -2, -2], [0, 0, -2, 8]]) #gaussjord(A); #matrix([[1, 0, 0, 4], [0, 1, 0, 5], [0, 0, 1, -4]]) #A:=matrix(3,3,[1,1,3,5,5,13,3,1,7]); #A := matrix([[1, 1, 3], [5, 5, 13], [3, 1, 7]]) #det(A); #-4 #B:=inverse(A); #B := matrix([[-11/2, 1, 1/2], [-1, 1/2, -1/2], [5/2, -1/2, 0]]) #evalm(B&*A); #matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) #if x>0 then #y:=x+1 #else #y:=x-1 #fi: #y: #for i from 1 to 10 do #print(i); #od: #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #sum('i','i'=1..10); #55 #f:=proc(x) local z; if 0<=x and x<=1 then z:=x^2: else z:=1-x: fi: RETURN#(z); end; #f := proc (x) local z; if 0 <= x and x <= 1 then z := x^2 else z := 1-x end if; RETURN(z) end proc #f(1/2); #1/4 #plot(f,-2..2);