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

## 2142 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))
 $x =$ \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 1.0 \\ 2.0 \\ 3.0 \\ 4.0 \\ 5.0 \end{array}\right) $\hat{x_1} =$ \newcommand{\Bold}[1]{\mathbf{#1}}-7.4161984871 $u^{T}u =$ \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 1.0 \end{array}\right) $u =$ \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 0.75327 \\ 0.17901 \\ 0.26851 \\ 0.35801 \\ 0.44751 \end{array}\right) $H^{T}H =$ \newcommand{\Bold}[1]{\mathbf{#1}}\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) $H =$ \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrr} -0.13484 & -0.26968 & -0.40452 & -0.53936 & -0.6742 \\ -0.26968 & 0.93591 & -0.09613 & -0.12817 & -0.16021 \\ -0.40452 & -0.09613 & 0.85581 & -0.19226 & -0.24032 \\ -0.53936 & -0.12817 & -0.19226 & 0.74366 & -0.32043 \\ -0.6742 & -0.16021 & -0.24032 & -0.32043 & 0.59946 \end{array}\right) $Hx =$ \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} -7.4162 \\ -0.0 \\ -0.0 \\ -0.0 \\ -0.0 \end{array}\right)