function [Q,R] = gs(A)
%
% Un-modified Gram-Schmidt iteration to compute the
% QR factoring of a matrix A
%
% For teaching purposes, RMC 2002
%
% Shocking loss of orthogonality of Q already on the 8 by 8 Hilbert matrix
%
[m,n] = size(A);
R = eye(m,m);
Q = A;
for j=1:n,
%    for k=1:j-1,
%        R(k,j) = Q(:,k)'*Q(:,j); 
%        Q(:,j) = Q(:,j) - R(k,j)*Q(:,k);
%    end;
    R(1:j-1,j) = Q(:,1:j-1)'*Q(:,j);
    Q(:,j) = Q(:,j) - Q(:,1:j-1)*R(1:j-1,j);
    R(j,j) = norm(Q(:,j),2);;
    Q(:,j) = Q(:,j)/R(j,j);
end;
