function [lambda,x,its] = DomArrow(d,v,tol,itMax)
% d is a column n-vector with distinct positive values
% v is a column (n-1)-vector with nonzero entries
% lambda and x are an approximate dominant eigenpair for A = Arrow(d,v)
% that satisfies norm(A*x - lambda*x,2) <= tol*norm(A,1). It is assumed that
% x has unit 2-norm.
% its is the number of required iterations until convergence. itMax is an
% upper bound on the number of iterations.

n = length(d);
% Initial guess will be I(:,k) where d(k) is largest diagonal...
[dmax,k] = max(d);
x = zeros(n,1); x(k) = 1;

% 1-norm of A...
Anorm = max(max(d(1:n-1)+abs(v)),sum(abs(v))+d(n))
% (If you forget this factor in the termination criteria, then 2070 or so
% iterations are required.)

% In the following, w = Ax and lambda = x'Ax where x is the current iterate
w = Prod(d,v,x);
lambda = x'*w;
its = 0;

while norm(w - lambda*x)>tol && its < itMax
x = w/norm(w);
w = Prod(d,v,x);
lambda = x'*w;
its = its+1;
end

function w = Prod(d,v,x)
% w = Ax where A = arrow(d,v)
n = length(x);
w = d.*x;
w(1:n-1) = w(1:n-1) + x(n)*v;
w(n) = w(n) + v'*x(1:n-1);