# QR factorization Lkhagva

## 2292 days ago by bigdata2016

A=matrix(RDF, [[1,2,3,4,4],[1,1,2,2,3],[-1,-2,2,1,-3],[0,1,0,2,0],[3,4,-2,0,1]]) B=A H1=identity_matrix(A.nrows()) A1=A for i in range(0,A.nrows()-1): print "Step", i+1 x=A1.matrix_from_columns([0]) html('$A_%s=%s$ and $x=%s$' %(i,latex(A.round(3)), latex(x.n(digits=3))) ) print x1_=(-sgn(x[0,0])+((-1)^sgn(x[0,0])+1)/2)*sqrt((x.transpose()*x)[0,0]) u=matrix(RDF,x.nrows(),1) u[0,0]=sqrt((x1_-x[0,0])/(2*x1_)) for j in range(1,x.nrows()): u[j]=x[j,0]/(-2*u[0,0]*x1_) H=matrix(RDF,x.nrows(),x.nrows(),1)-2*u*u.transpose() k=identity_matrix(RDF,i) H=k.block_sum(H) H1=H*H1 A=H*A html(' $H_%s=%s$' %(i+1,latex(H.round(3))) ) A1=A.submatrix(i+1,i+1,A.nrows()-i-1,A.ncols()-i-1) print html(' $Q=%s$ and $R=%s$' %(latex(H1.round(3)),latex(A.round(3))) )
 Step 1 $A_0=\left(\begin{array}{rrrrr} 1.0 & 2.0 & 3.0 & 4.0 & 4.0 \\ 1.0 & 1.0 & 2.0 & 2.0 & 3.0 \\ -1.0 & -2.0 & 2.0 & 1.0 & -3.0 \\ 0.0 & 1.0 & 0.0 & 2.0 & 0.0 \\ 3.0 & 4.0 & -2.0 & 0.0 & 1.0 \end{array}\right)$ and $x=\left(\begin{array}{r} 1.00 \\ 1.00 \\ -1.00 \\ 0.000 \\ 3.00 \end{array}\right)$ $H_1=\left(\begin{array}{rrrrr} -0.289 & -0.289 & 0.289 & 0.0 & -0.866 \\ -0.289 & 0.935 & 0.065 & 0.0 & -0.194 \\ 0.289 & 0.065 & 0.935 & 0.0 & 0.194 \\ 0.0 & 0.0 & 0.0 & 1.0 & 0.0 \\ -0.866 & -0.194 & 0.194 & 0.0 & 0.418 \end{array}\right)$ Step 2 $A_1=\left(\begin{array}{rrrrr} -3.464 & -4.907 & 0.866 & -1.443 & -3.753 \\ -0.0 & -0.547 & 1.522 & 0.781 & 1.263 \\ 0.0 & -0.453 & 2.478 & 2.219 & -1.263 \\ 0.0 & 1.0 & 0.0 & 2.0 & 0.0 \\ -0.0 & -0.642 & -3.434 & -3.658 & -4.21 \end{array}\right)$ and $x=\left(\begin{array}{r} -0.547 \\ -0.453 \\ 1.00 \\ -0.642 \end{array}\right)$ $H_2=\left(\begin{array}{rrrrr} 1.0 & 0.0 & 0.0 & 0.0 & 0.0 \\ 0.0 & -0.395 & -0.327 & 0.722 & -0.464 \\ 0.0 & -0.327 & 0.923 & 0.169 & -0.109 \\ 0.0 & 0.722 & 0.169 & 0.626 & 0.24 \\ 0.0 & -0.464 & -0.109 & 0.24 & 0.846 \end{array}\right)$ Step 3 $A_2=\left(\begin{array}{rrrrr} -3.464 & -4.907 & 0.866 & -1.443 & -3.753 \\ 0.0 & 1.384 & 0.181 & 2.107 & 1.866 \\ 0.0 & 0.0 & 2.164 & 2.53 & -1.122 \\ -0.0 & 0.0 & 0.694 & 1.314 & -0.312 \\ -0.0 & -0.0 & -3.88 & -3.217 & -4.01 \end{array}\right)$ and $x=\left(\begin{array}{r} 2.16 \\ 0.694 \\ -3.88 \end{array}\right)$ $H_3=\left(\begin{array}{rrrrr} 1.0 & 0.0 & 0.0 & 0.0 & 0.0 \\ 0.0 & 1.0 & 0.0 & 0.0 & 0.0 \\ 0.0 & 0.0 & -0.481 & -0.154 & 0.863 \\ 0.0 & 0.0 & -0.154 & 0.984 & 0.09 \\ 0.0 & 0.0 & 0.863 & 0.09 & 0.497 \end{array}\right)$ Step 4 $A_3=\left(\begin{array}{rrrrr} -3.464 & -4.907 & 0.866 & -1.443 & -3.753 \\ 0.0 & 1.384 & 0.181 & 2.107 & 1.866 \\ -0.0 & -0.0 & -4.496 & -4.197 & -2.872 \\ -0.0 & 0.0 & -0.0 & 0.612 & -0.494 \\ -0.0 & -0.0 & 0.0 & 0.701 & -2.99 \end{array}\right)$ and $x=\left(\begin{array}{r} 0.612 \\ 0.701 \end{array}\right)$ $H_4=\left(\begin{array}{rrrrr} 1.0 & 0.0 & 0.0 & 0.0 & 0.0 \\ 0.0 & 1.0 & 0.0 & 0.0 & 0.0 \\ 0.0 & 0.0 & 1.0 & 0.0 & 0.0 \\ 0.0 & 0.0 & 0.0 & -0.658 & -0.753 \\ 0.0 & 0.0 & 0.0 & -0.753 & 0.658 \end{array}\right)$ $Q=\left(\begin{array}{rrrrr} -0.289 & -0.289 & 0.289 & 0.0 & -0.866 \\ 0.421 & -0.301 & -0.421 & 0.722 & -0.181 \\ -0.706 & -0.512 & -0.406 & 0.029 & 0.271 \\ 0.286 & -0.072 & -0.645 & -0.645 & -0.286 \\ 0.399 & -0.747 & 0.399 & -0.249 & 0.249 \end{array}\right)$ and $R=\left(\begin{array}{rrrrr} -3.464 & -4.907 & 0.866 & -1.443 & -3.753 \\ 0.0 & 1.384 & 0.181 & 2.107 & 1.866 \\ -0.0 & -0.0 & -4.496 & -4.197 & -2.872 \\ 0.0 & -0.0 & -0.0 & -0.931 & 2.578 \\ 0.0 & -0.0 & 0.0 & 0.0 & -1.594 \end{array}\right)$ Step 1 and Step 2 and Step 3 and Step 4 and and 

html('$Q\cdot R=%s$' %( latex((H1.transpose()*A).round(3)) ) ) print html( '$Q^T\cdot Q=%s$'%( latex((H1.transpose()*H1).round(3) ) ))
 $Q\cdot R=\left(\begin{array}{rrrrr} 1.0 & 2.0 & 3.0 & 4.0 & 4.0 \\ 1.0 & 1.0 & 2.0 & 2.0 & 3.0 \\ -1.0 & -2.0 & 2.0 & 1.0 & -3.0 \\ -0.0 & 1.0 & -0.0 & 2.0 & 0.0 \\ 3.0 & 4.0 & -2.0 & -0.0 & 1.0 \end{array}\right)$ $Q^T\cdot Q=\left(\begin{array}{rrrrr} 1.0 & 0.0 & 0.0 & 0.0 & 0.0 \\ 0.0 & 1.0 & -0.0 & -0.0 & 0.0 \\ 0.0 & -0.0 & 1.0 & -0.0 & -0.0 \\ 0.0 & -0.0 & -0.0 & 1.0 & 0.0 \\ 0.0 & 0.0 & -0.0 & 0.0 & 1.0 \end{array}\right)$