function u=Gauss(A,f,maxiter,tol,Rnd)
% GAUSS original iterative method by Gauss
%   u=Gauss(A,f,iter,tol,Rnd) attempts to solve the linear
%   system A*u=f using the iterative method originally proposed by
%   Gauss. The parameter maxiter limits the maximum number of
%   iterations, if the norm of the right hand side obtained compared
%   to the original one does not go below the parameter tol before.
%   Rnd is a flag to turn the rounding Gauss used on (Rnd=1) or off
%   (Rnd=0).

m=length(f);    
D=diag(A);
fn=f;                              % keep original right hand side
u=zeros(m,1);                      % to sum corrections
i=1;
while norm(fn)/norm(f)>tol && i<=maxiter
  [du,id]=max(abs(fn./D));         % find biggest contribution
  d=zeros(m,1);
  d(id)=fn(id)/D(id);
  if Rnd, d(id)=round(d(id)); end  % round if desired
  u=u+d;                           % add correction
  fn=fn-A*d;                       % update the right-hand side
  i=i+1;
end