function [A,t]=qr_inplace(A)
%qr_inplace computes a QR factorization inplace
%  [A,t]=qr_inplace(A) overwrites square A with its QR factorization in 
%   factored form. R is stored in the upper triangle of A. Q can be
%   factored as
%     Q=H1 * H2 * H3 *...* H{n-1}
%   where every Hi is of the form
%     Hi=(I-taui*vi*vi').
%   where vi is a vector of the form
%     [zeros(i-1,1); 1; A(i+1:n,i)].
%   the scalar taui can be computed as taui=2/(vi'*vi), but is also
%   returned in the second output as t(i)=taui. 

[n,m]=size(A);
if m~=n,error('A ist nicht quadratisch!');end

t=zeros(n,1);

for i=1:n-1
    [v,t(i)]=house(A(i:n,i));
    A(i:n,i:n)=apply_house(A(i:n,i:n),v,t(i));
    A(i+1:n,i)=v(2:n+1-i);
end
    