[Householder] Answer to 락가바 보양터그터흐 question - Naguib

1914 days ago by bigdata2016

following the formulation in the link:

Householder matrix and QR factorization.PDF

 

 

the code is as follows:

x=matrix(RDF,5,1,[1,2,3,4,5]) x1_=-sgn(x[0,0])*sqrt((x.transpose()*x)[0,0]) u=matrix(RDF,x.nrows(),1) u[0,0]=sqrt((x1_-x[0,0])/(2*x1_)) for i in range(1,x.nrows()): u[i]=x[i,0]/(-2*u[0,0]*x1_) H=matrix(RDF,x.nrows(),x.nrows(),1)-2*u*u.transpose() ndigits = 5 html('$%s$'%LatexExpr(r"x = ")) show((x).round(ndigits)) html('$%s$'%LatexExpr(r"\hat{x_1} = ")) show((x1_)) html('$%s$'%LatexExpr(r"u^{T}u = ")) show((u.transpose()*u).round(ndigits)) html('$%s$'%LatexExpr(r"u = ")) show((u).round(ndigits)) html('$%s$'%LatexExpr(r"H^{T}H = ")) show((H.transpose()*H).round(ndigits)) html('$%s$'%LatexExpr(r"H = ")) show((H).round(ndigits)) html('$%s$'%LatexExpr(r"Hx = ")) show((H*x).round(ndigits))