function b=lgs_solve_qr(A,b)
%lgs_solve_qr solves a linear system of equations using a QR factorization
%
%  x=lgs_solve_qr(A,b) solves the linear system of equations A*x=b for x.
%    A must be n-by-n. B must have n rows, but can have any number of
%    columns. x is of the same size as b.

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

[A,t]=qr_inplace(A);
% apply Householders to b
for i=1:n-1
    v=[1;A(i+1:n,i)];
    b(i:n,:)=apply_house(b(i:n,:),v,t(i));
end
%back substitution
for i=n:-1:1,
    for j=i+1:n,
        b(i,:)=b(i,:)-A(i,j)*b(j,:);
    end
    b(i,:)=b(i,:)/A(i,i);
end

    