I'm using the subroutine sgeqr2 from Lapack. This subroutine solves the QR-factorization
$$A = QR$$
It's easy to find the $R$ matrix, because the in-out argument $A$ of subroutine sgeqr2 will return a matrix, where the upper values from the diagonal is the $R$-values. Eeasy.
But how can I find the $Q$ matrix? According to the "manual".
The matrix Q is represented as a product of elementary reflectors
Q = H(1) H(2) . . . H(k), where k = min(m,n).
Each H(i) has the form
H(i) = I - tau * v * v**T
where tau is a real scalar, and v is a real vector with
v(1:i-1) = 0 and v(i) = 1; v(i+1:m) is stored on exit in A(i+1:m,i),
and tau in TAU(i).
The conclusion here is to take the vector $tau$ and pick the first value of $tau$. Then multiply it with a matrix $vv^T$.
Have I interprent this text correct?
For a matrix $A$
0.674878, 0.151285, 0.875139, 0.150518,
0.828102, 0.150747, 0.934674, 0.474325,
0.476510, 0.914686, 0.740681, 0.060455,
0.792594, 0.471488, 0.529343, 0.743405,
0.084739, 0.475160, 0.419307, 0.628999,
0.674878, 0.151285, 0.875139, 0.150518
I get the $R$ matrix
-1.568159 -0.751743 -1.762103 -0.808132
0.000000 0.887756 0.233565 0.241302
0.000000 0.000000 0.500422 -0.142971
0.000000 0.000000 0.000000 -0.700355
0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
And $tau$ vector
1.430363 1.205732 1.007224 1.655577
How should I do, to find $Q$ matrix if I know the dimension of $A$ and the values from $tau$?
I wrote som MALAB/Octave code to find the $Q$-matrix from $H$, but it won't work.
tau = [1.430363 1.205732 1.007224 1.655577];
H = eye(4); % Initial
for i = 1:4
v(1:i-1) = 0;
v(i) = 1;
H = H*(eye(4) - tau(i)*v'*v);
H
end
Now I got the answer!
Assume that we have a matrix $A$
And we implement it into the subroutine.
Then we get the matrix $A$ back, but it has change into this
The $R$ matrix is the upper triangular above the diagonal.
And then the lower traingular under the diagonal, we want to use those in this loop.
Where tau is this vector
Then our $Q$ matrix will be.
I post code instead of matrix math, because it will be easier to read.