Seven Matrix Vector Multiplies

MatVecMul1[A_,x_] := Module[
{i,j,n,m},
n=Length[x];
m=Length[A];
Table[
Sum[ A[[i,j]] x[[j]],{j,1,n}],
{i,1,m}]
]

MatVecMul2[A_,x_] := Module[
{i,j,n,m,b},
{m,n}=Dimensions[A];
If[ n!=Length[x], Return[Print["Dimensions Do Not Match"]]];
b=Table[ 0,{m}];
For[i=1,i≤m,i=i+1,
For[j=1,j≤n,j=j+1,
b[[i]]=b[[i]]+A[[i,j]] x[[j]]
]
];
b
]

MatVecMul3[A_,x_] := Module[
{i,j,n,m,b},
{m,n}=Dimensions[A];
If[ n!=Length[x], Return[Print["Dimensions Do Not Match"]]];
b=Table[ 0,{m}];
For[i=1,i≤m,i+=1,
For[j=1,j≤n,j+=1,
b[[i]]+=A[[i,j]] x[[j]]
]
];
b
]

MatVecMul4[A_,x_] := Module[
{i,j,n,m,b},
{m,n}=Dimensions[A];
If[ n!=Length[x], Return[Print["Dimensions Do Not Match"]]];
b=Table[ 0,{m}];
Do[
Do[
b[[i]]+=A[[i,j]] x[[j]],
{j,1,n}],
{i,1,m}
];
b
]

MatVecMul5[A_,x_] := Module[
{i,j,n,m,b,sum},
{m,n}=Dimensions[A];
If[ n!=Length[x], Return[Print["Dimensions Do Not Match"]]];
b=Table[ 0,{m}];
Do[
sum=0;
Do[
sum+=A[[i,j]] x[[j]],
{j,1,n}];
b[[i]]=sum,
{i,1,m}
];
b
]

MatVecMul6[A_,x_] := Module[
{i,j,n,m,b,sum},
{m,n}=Dimensions[A];
If[ n!=Length[x], Return[Print["Dimensions Do Not Match"]]];
b=Table[ 0,{m}];
For[i=1,i≤m,i+=1,
sum=0;
For[j=1,j≤n,j+=1,
sum+=A[[i,j]] x[[j]]
];
b[[i]]=sum
];
b
]

MatVecMul7[A_,x_] := Module[
{i,j,n,m,b,sum},
{m,n}=Dimensions[A];
If[ n!=Length[x], Return[Print["Dimensions Do Not Match"]]];
b=Table[ 0,{m}];
For[i=1,i≤m,i+=1,
b[[i]]=A[[i]].x
];
b
]

Five Matrix Matrix Multiplies

MatMatMul1[A_,B_] := Module[
{i,j,k,mA,nA, mB,nB, C},
{mA,nA}=Dimensions[A];
{mB,nB}=Dimensions[B];
If[ nA≠mB, Return[Print["Dimensions Do Not Match"]]];
C=Table[ 0,{mA},{nB}];
Print[C];
For[i=1,i≤mA,i=i+1,
For[j=1,j≤nB,j=j+1,
For[k=1,k≤nA,k=k+1,
C[[i,j]]=C[[i,j]]+A[[i,k]] B[[k,j]]
]
]
];
C
]

MatMatMul2[A_,B_] := Module[
{i,j,k,mA,nA, mB,nB, C},
{mA,nA}=Dimensions[A];
{mB,nB}=Dimensions[B];
If[ nA≠mB, Return[Print["Dimensions Do Not Match"]]];
Table[
Sum[A[[i,k]] B[[k,j]],{k, 1, nA}],
{i,1,mA},
{j,1,nB}
]
]

MatMatMul3[A_,B_] := Module[
{i,j,k,mA,nA, mB,nB, C},
{mA,nA}=Dimensions[A];
{mB,nB}=Dimensions[B];
If[ nA≠mB, Return[Print["Dimensions Do Not Match"]]];
Table[
A[[i]].B[[All,j]],
{i,1,mA},
{j,1,nB}
]
]

MatMatMul4[A_,B_] := Module[
{i,j,k,mA,nA, mB,nB, C},
{mA,nA}=Dimensions[A];
{mB,nB}=Dimensions[B];
If[ nA≠mB, Return[Print["Dimensions Do Not Match"]]];
Table[
A[[i]].B,
{i,1,mA}
]
]

MatMatMul5[A_,B_] := Module[
{i,j,k,mA,nA, mB,nB, C},
{mA,nA}=Dimensions[A];
{mB,nB}=Dimensions[B];
If[ nA≠mB, Return[Print["Dimensions Do Not Match"]]];
Transpose[
Table[
A.B[[All,j]],
{j,1,nB}
]
]
]

Testing Multiplies

A= {
{1,2,3},
{3,-4,5},
{2,3, 4}
};
B= {
{1,2,3,4},
{1,2,3,-4},
{-1,2,3,5}
};
x = {-1,2,-3};
{A.x,
MatVecMul1[A,x],
MatVecMul2[A,x],
MatVecMul3[A,x],
MatVecMul4[A,x],
MatVecMul5[A,x],
MatVecMul6[A,x],
MatVecMul7[A,x]
}

{A.B,
MatMatMul1[A,B],
MatMatMul2[A,B],
MatMatMul3[A,B],
MatMatMul4[A,B],
MatMatMul5[A,B]
}

BuildingQ

QFun[v_]:= Module[
{m,w,Q},
m=Length[v];
w=v;
w[[1]] +=  Norm[v]; 
w =w/Norm[v];
Q = IdentityMatrix[m]-(2/(w.Conjugate[w]))Outer[Times,w, Conjugate[w]]
]

Testing!

A= RandomReal[ {-1.0,1.0},{4,4}];
v= A[[All,1]];
Q=QFun[v];
MatrixForm[Q.A]
MatrixForm[Q.Q]

OK so Q zeros the first column of A (except the first entry) and it is real easy to invert. One more cool thing is all of Q's eigenvalues have magnitude 1

Eigenvalues[Q]

Putting the Qs together for HW2

Here is a simple example of the procedure for a 4x4 matrix.

QFun[v_,i_]:= Module[
{m,w,Q},
m=Length[v];
w=v;
w[[i]] +=  Norm[v]; 
w =w/Norm[v];
Q = IdentityMatrix[m]-(2/(w.Conjugate[w]))Outer[Times,w, Conjugate[w]]
]

A= RandomReal[ {-1.0,1.0},{4,4}];
v= A[[All,1]];
Q1=QFun[v,1];
A1=Q1.A
v=A1[[All,2]];
v[[1]]=0.0;
Q2=QFun[v,2];
A2=Q2.A1
v=A2[[All,3]];
v[[1]]=0;
v[[2]]=0;
Q3=QFun[v,3];
A3=Q3.A2