function [ Q, R ] = gs_factor ( A )
%
% function [ Q, R ] = gs_factor ( A )
%
%  Discussion:
%
%    GS_FACTOR computes the QR factorization of a matrix A, using the Gram-Schmidt
%    approach.
%
%  Modified:
%
%    21 March 2000
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real A(M,N), a matrix regarded as containing N column vectors
%    of length M.
%
%    Output, real Q(M,N), R(N,N), the QR factorization of A.
%    Some columns of Q may be zero, but all other columns of Q have unit 
%    L2 norm.  The columns of Q are pairwise orthogonal.  R is an upper
%    triangular matrix.  A = Q * R.
%
[ m, n ] = size ( A );

Q = zeros ( m, n );
R = zeros ( n, n );

for j = 1: n

  for i = 1 : j-1
    R(i,j) = Q(:,i)' * A(:,j);
    A(:,j) = A(:,j) - Q(:,i) * R(i,j);
  end

  R(j,j) = norm ( A(:,j) );

  if ( R(j,j) ~= 0.0 )
    Q(:,j) = A(:,j) / R(j,j);
  end 

end

