I am trying to map two versions of Householder algorithm to one another. The first version came from Terefethen and Bau book. $A$ is a $m \times n$ matrix and $m$ is greater than $n$.
$\underline {House \quad holder \quad QR \quad Factorization \quad Algorithm(A)}$
$For \quad k=1 \quad to \quad n$
$\quad x = A_{k:m,k}$
$\quad v_{k} = sign(x_{1})||x||_{2}e_{1} + x$
$\quad v_{k} =\frac {v_{k}} {||v_{k}||_2}$
$\quad A_{k:m,k:n}= A_{k:m,k:n} - 2v_{k}( v_{k}^{*} A_{k:m,k:n})$
$end$
The second version came from Matrix Computation book. It uses house function inside householder algorithm as follow:
$A$ is an $m \times n $ matrix.
$\underline {Householder(A)}$
$[m,n] = size(A)$
$R = A$
$for \quad j=1:n-1$
$\quad [v,b] = house(R(j:m,j))$
$\quad R(j:m,j:n)= R(j:m,j:n)-(b*v)*(v^{T}*R(j:m,j:n))$
$end$
$x \in R^{m}$, the following function computes $v \in R^m$ with $v(1) = 1$ and $b \in R$ such that $P = I_m – bvv^T$ is orthogonal and $Px= ||x||_{2}e_1$.
$\underline {House(x)}$
$[m] = size(x)$
$\sigma = x(2:m)^{T}x(2:m)$
$v = \begin {bmatrix} 1\\ x(2:m)\end {bmatrix}$
$if \quad ((\sigma == 0) \quad and \quad (x(1)>=0))$
$\quad b=0$
$else \quad if ((\sigma==0) \quad and \quad (x(1)<0))$
$\quad b = -2$
$else$
$\quad \mu=\sqrt{(x(1)^2+\sigma)}$
$\quad if (x(1)<=0)$
$\quad \quad v(1) = x(1) –\mu$
$\quad else$
$\quad \quad v(1) = \frac{-\sigma}{(x(1)+\mu)}$
$\quad end$
$\quad b=\frac{2*v(1)^2}{(\sigma+v(1)^2)}$
$\quad v = v/v(1)$
$\quad end$
$end$
At first I was trying to trace both algorithms by hand to see the similarities and differences, but it was too confusing to follow. I wrote the code for both versions in MATLAB and found the final results are the same in magnitude although the $v_{i}$ vectors were not the same. Can anyone help pointing out the differences in these two versions of the algorithm?
Is the second version more complete since it has special case for the condition $\sigma = 0$?
I tested the code for $A= \begin{bmatrix} 1 & 1 \\ 1 & 2\\ 1&3 \end{bmatrix}$. Why the second version just needs to loop from $0$ to $j-1$ to produce the $R$ while the first version loops from $1$ to $n$?