function [x, its, norm_r] = jacobi (A, b, tol, max_its, x0)
%
% [x, its, norm_r] = jacobi (A, b, tol, max_its, x0)
%
% Compute the solution of A*x=b through the Jacobi method. tol is a
% vector of two components with the relative and the absolute
% tolerances (defaults [1e-6, 1e-6]). max_its is the maximum number of
% iterations (default 100) and x0 the initial solution (default zero).
% x is the computed solution, its the number of iterations and norm_r
% the norm of the final residual.
n = length (A);
if ((nargin <= 2) || isempty (tol))
  tol = [1e-6, 1e-6];
end
if ((nargin <= 3) || isempty (max_its))
  max_its = 100;
end
if ((nargin <= 4) || isempty (x0))
  x0 = zeros (n, 1);
end
if (isscalar (tol))
  tol = [tol, tol];
end
norm_b = norm (b);
x = x0;
r = b - A * x;
norm_r = norm (r);
its = 0;
while ((norm_r > tol(1) * norm_b + tol(2)) && (its < max_its))
  its = its + 1;
  for i = 1:n
    x(i) = (b(i) - ...
         A(i, 1:i - 1) * x0(1:i - 1) - ...
         A(i, i + 1:n) * x0(i + 1:n)) / A(i, i);
  end
  r = b - A * x;
  norm_r = norm (r);
  x0 = x;
end
if (norm_r > tol(1) * norm_b + tol(2))
  warning ('Impossibile raggiungere la tolleranza richiesta')
end
%!test
%! A = [2, 1, 0; 1, 2, 1; 0, 1, 2];
%! b = A * ones (3, 1);
%! x = jacobi (A, b, 1e-8);
%! assert (x, ones (3, 1), 1e-7)
%!test
%! A = [1, 1, 1; 1, 2, 1; 0, 1, 1];
%! b = A * ones (3, 1);
%! [x, it] = jacobi (A, b, 1e-8);
%! assert (it, 100)
