%HessEigQR: Compute all eigenvalues of a matrix using the QR algorithm
% and deflation. The matrix is first reduced to Hessenberg form and the
% QR step is implemented using Givens rotations.
%
% Usage:
% >> D=HessEigQR(A);
%
% Note that this code only works if all the eigenvalues of the matrix are
% real. For a genrally safe code the QZ algorithm should be used but the
% changes needed are minor.
%
function [D]=HessEigQR(A);
%
% Initial transformation to Hessenberg form.
%
[A]=Hessenberg(A);
%
% Create a diagonal matrix D that is used for storing the
% eigenvalues.
%
D=zeros(size(A));
%
% Compute one eigenvalue at a time. Reduce the size of the matrix until
% we have all the eigenvalues.
%
while prod(size(A))>1
[n,m]=size(A);
done=0;nSteps=0; % nSteps is used as a progress counter.
while ~done
%
% Perform one QR step. Note that shifts preserve the Hessenberg
% form so we could for instance implement shifted QR the same way.
%
A=OneQRStep(A);
nSteps=nSteps+1;
%
% We terminate the iterations when the elements on the last row
% are close to zero. That means A(n,n) is a good approximation of an
% eigenvalue.
%
tol=10^-15;
done=(norm(A(n,1:n-1),1)