QR factorization Lkhagva

1914 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
 and 

            

Step 2
 and 

            

Step 3
 and 

            

Step 4
 and 

            

    and 
Step 1
 and 

            

Step 2
 and 

            

Step 3
 and 

            

Step 4
 and 

            

    and 

Check the answer

html('$Q\cdot R=%s$' %( latex((H1.transpose()*A).round(3)) ) ) print html( '$Q^T\cdot Q=%s$'%( latex((H1.transpose()*H1).round(3) ) ))