#hw4.txt. February 5, 2014. Katie McKeon read`C4.txt`: Help:=proc(): print(`MatrixValidate(A,B), MatMulRG(A,B) `): print(`Strassen1(A,B), Strassen(A,B) `): print(`MatSubt(A,B), RandMat(n,R) `): end: #Garvan's Book Examples #Linear Algebra with(linalg): A:=matrix(3,5,[1,4,-10,3,-3,10,41,-102,30,-31,-9,-19,56,-27,10]): rank(A); nullspace(A); A:=matrix(3,3,[1,2,3,4,5,6,7,8,9]); eigenvects(A); eigenvals(A); #Programming x:=0: for i from 1 to 10 do x:=x+i: od: x; sum('i','i'=1..10); g:=proc(x,y) local z,i; global v,w; if x*y>1 then v:=x+y: else w:=x-y: fi: RETURN(x*y): end: g(2,3); v,w; g(1/2,1/3); v,w; #Saving r:='r': x:='x': v:=Int(4*Pi*x*sqrt(1-x^2),x=0..r); %latex(v); #MatrixValidate(A,B) ensures that the two matrices A and B are valid and of the same dimensions MatrixValidate:=proc(A,B) 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: end: #MatMultRG(A,B) inputs two square matrices and multiplies them using MatMultR(A,B) MatMultRG:=proc(A,B) local i,j,A1, B1, C: MatrixValidate(A,B): A1:=PadZeros(A): B1:=PadZeros(B): C:=MatMultR(A1,B1): [seq([seq(C[i][j],j=1..nops(A))],i=1..nops(A))]: end: #Strassen(A,B) is the initial call to Strassens method Strassen:=proc(A,B) local C: MatrixValidate(A,B): C:=Strassen1(PadZeros(A),PadZeros(B)): [seq([seq(C[i][j],j=1..nops(A))],i=1..nops(A))]: end: #Strassen1(A,B) inputs two square 2^n matrices A and B and multiplies them using Strassen's algorithm Strassen1:=proc(A,B) local n, A1, B1, A11, A12, A21, A22, B11, B12, B21, B22, C11, C12, C21, C22, M1, M2, M3, M4, M5, M6, M7,i: n:=nops(A): 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:=Strassen1(MatAdd(A11,A22),MatAdd(B11,B22)): M2:=Strassen1(MatAdd(A21,A22),B11): M3:=Strassen1(A11,MatSubt(B12,B22)): M4:=Strassen1(A22,MatSubt(B21,B11)): M5:=Strassen1(MatAdd(A11,A12),B22): M6:=Strassen1(MatSubt(A21,A11),MatAdd(B11,B12)): M7:=Strassen1(MatSubt(A12,A22),MatAdd(B21,B22)): C11:=MatAdd(MatAdd(M1,M4),MatSubt(M7,M5)): C12:=MatAdd(M3,M5): C21:=MatAdd(M2,M4): C22:=MatAdd(MatSubt(M1,M2),MatAdd(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: #MatSubt(A,B): Subtracts matrix B from matrix A MatSubt:=proc(A,B) local i,j: [seq([seq(A[i][j]-B[i][j],j=1..nops(A[i]))],i=1..nops(A))] end: #RandMat(n,R) returns an nxn matrix with integer entries chosen randomly from [1,R] RandMat:=proc(n,R) local i,j: [seq( [seq(rand(1..R)() ,i=1..n)],j=1..n)]: end: