  function [v,vals,err]=arnR(A,m,v,order,tol,maxits)
% function [v, vals, err]=arnR(A,m,v,order,tol,maxits)
% restarted  Arnoldi process 
% order = -1 --> leftmost eigenvalue
% order = +1 --> rightmost eigenvalue 
%%--------------------
 n = size(A,1);
 res = 1.0; its = 0; tol1 = 0.0;
%%-------------------- restart loop 
 while (its*m < maxits  & res > tol1) 
   its = its+1;
   ro = norm(v)  ; 
%%-------------------- if ro == 0 ...
   t = 1.0/ ro   ;
   V(:,1) = v*t; 
   v  = A*V(:,1) ;
   val  = v'*V(:,1) ;
   res = norm(v - val*V(:,1)); 
   if (its == 1), tol1 = tol*res;, end
   err(its)  = res;
%%-------------------- anoldi loop 
   for i=1:m  
     i1 = i + 1  ; 
     V(:,i1) = A*V(:,i)  ;
%%-------------------- modified GS  ;
     for j=1:i 
       t = V(:,j)'*V(:,i1)  ;
       H(j,i) = t  ;
       V(:,i1) = V(:,i1) - t*V(:,j)  ; 
     end  
%
     t = norm(V(:,i1),2)  ;
     H(i1,i) = t  ;
     if (t  ~= 0.0)  
       t = 1.0 / t  ;
       V(:,i1) = V(:,i1)*t  ;  
     end  
   end
%%-------------------- end of Arnoldi loop. Restart
   Vm = V(:,1:m);
   Hm = H(1:m,1:m);
   [X,vals] = eig(Hm);
   vals = diag(vals);
   [t, k] = max(order*real(vals));
   v = V(:,1:m)*X(:,k) ;
 end
