function [Q,R]=qr_generate(A)
% qr_generate computes a QR factorization
%
%  [Q,R]=qr_generate(A) computes for a square matrix A the orthogonal
%   matrix Q and upper triangular R such that Q*R=A.

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

%QR factorization: in place
[A,t]=qr_inplace(A);

%generate Q from lower triangle of A
% Q= H1*H2*H3*...*H{n-1}*I
%multiply from back to front
Q=eye(n,n);
for i=n-1:-1:1,
    v=[1;A(i+1:n,i)];
    %one would expect the following line, but the other is faster
    %Q(i:n,:) =apply_house(Q(i:n,:)  ,v,t(i));
    Q(i:n,i:n)=apply_house(Q(i:n,i:n),v,t(i));
end

%extract upper triangle
R=triu(A);
% this is the same as: for i=1:n,for j=i:n,R(i,j)=A(i,j);end,end