function U = gram_schmidt_tolerance ( A )

%*****************************************************************************80
%
%% GRAM_SCHMIDT_TOLERANCE implements Gram-Schmidt orthonormalization.
%
%  Discussion:
%
%    A tolerance is used to discard vectors whose orthogonal component
%    is very small.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    20 January 2019
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real A(M,NA), a matrix whose columns are to be orthonormalized.
%
%    Output, real U(M,NU), the matrix with orthonormalized columns.
%    Note that 0 <= NU <= NA, because some columns of A may be rejected.
%
  tolerance = 100.0 * eps;

  [ m, na ] = size ( A );

  nu = 0;
  U = [];

  for j = 1 : na

    v = A(:,j);

    for j2 = 1 : nu
      dot = v' * U(:,j2);
      v = v - dot * U(:,j2);
    end

    v_norm = norm ( v );

    if ( tolerance < v_norm )
      nu = nu + 1;
      U(:,nu) = v / v_norm;
    end

  end

  return
end
