% This is the generic file for testing all routines and functions present in this
% directory.
clc;
clear all;
close all;

% Specify the dimension for some of the inital tests
n=20;

% $$$ % Generate Unitary matrix and test properties
% $$$ disp('---- Generating Unitary matrices ----')
% $$$ 
 fprintf('%s\n', 'Unitary matrix')
 U=GenUni(n);
% $$$ fprintf('%s', 'Value of norm(U*herm(U) - eye(n)):')
% $$$ fprintf('%d\n',(norm(U*U'-eye(n))))
% $$$ 
% $$$ fprintf('%s\n', 'Orthogonal matrix')
% $$$ U=GenUni(n,1);
% $$$ fprintf('%s', 'Value of norm(U*transpose(U) - eye(n)):')
% $$$ fprintf('%d\n',norm(U*U.'-eye(n)))
% $$$ 
% $$$ fprintf('%s\n', 'Determinant equal 1')
% $$$ U=GenUni(n,1,1);
% $$$ fprintf('%s','Value of norm(U*herm(U) - eye(n)):') 
% $$$ fprintf('%d %s %d\n',norm(U*U.'-eye(n)),' and det(U)',det(U))
% $$$ 
% $$$ pause


% $$$  % Test the new Givens rotation Givens and GivensR
% $$$  x=rand(1,1)+sqrt(-1)*rand(1,1);
% $$$  y=rand(1,1)+sqrt(-1)*rand(1,1);
% $$$  
% $$$  disp('--- Testing of Givens rotations ---')
% $$$  fprintf('%s\n','1) Arbitrary rotation')
% $$$  [c,s,r]=Givens(x,y);
% $$$  fprintf('%s','Value r: ')
% $$$  fprintf('%d + %d i \n', real(r), imag(r))
% $$$  fprintf('%s','Multiplication of G with [x;y]: ');
% $$$  G=[c s; -s' c' ];
% $$$  fprintf('[ %d ; %d ]\n\n',G*[x;y])
% $$$  
% $$$  fprintf('%s\n','2) Output of real r')
% $$$  [c,s,r]=GivensR(x,y);
% $$$  fprintf('%s','Value r: ')
% $$$  fprintf('%d + %d i \n', real(r), imag(r))
% $$$  fprintf('%s','Multiplication of G with [x;y]: ');
% $$$  G=[c s; -s' c' ];
% $$$  fprintf('[ %d ; %d ]\n\n',G*[x;y])
% $$$ 
% $$$  pause



 % Generate the pyramid QR factorization of a unitary matrix
 disp('--- Testing of Givens rotations, gplot function, matrix reconstruction ---')
 fprintf('%s','Generate pyramidal factorization of U')
 U=GenUni(n);
 [Grot,Gind,alpha]=PyramidUni(U);
 fprintf('%s\n','Plot the corresponding graphical representation')
 Gplot(Grot,Gind)
 fprintf('%s\n','Reconstruct the unitary matrix')
 I=eye(n,n); I(n,n)=alpha;
 Ur=ConsMatLeft(Grot,Gind,I);
 fprintf('%s','norm(original U - reconstructed U): ')
 fprintf('%d\n\n',norm(Ur-U))


% $$$ % The shift-through operation tested on some matrices.
% $$$ disp('--- Testing of the Shift-through operation ---')
% $$$ I=sqrt(-1);
% $$$ x=rand(1,1)+I*rand(1,1);y=rand(1,1)+I*rand(1,1);
% $$$ [c,s,r]=Givens(x,y); H1=[c;s];
% $$$ H1f=[1 0 0 ; 0 c s ; 0 -conj(s) conj(c)];
% $$$ 
% $$$ x=rand(1,1)+I*rand(1,1);y=rand(1,1)+I*rand(1,1);
% $$$ [c,s,r]=Givens(x,y); H2=[c;s];
% $$$ H2f=[c s 0 ; -conj(s) conj(c) 0; 0 0 1];
% $$$ 
% $$$ x=rand(1,1)+I*rand(1,1);y=rand(1,1)+I*rand(1,1);
% $$$ [c,s,r]=Givens(x,y); H3=[c;s];
% $$$ H3f=[1 0 0 ; 0 c s ; 0 -conj(s) conj(c)];
% $$$ UP=H1f*H2f*H3f;
% $$$ 
% $$$ % Perform now the Shift-Through operation.
% $$$ [G1,G2,G3]=ST(H1,H2,H3);
% $$$ 
% $$$ c=G1(1);s=G1(2);
% $$$ G1f=[c s 0 ; -conj(s) conj(c) 0; 0 0 1];
% $$$ c=G2(1);s=G2(2);
% $$$ G2f=[1 0 0 ; 0 c s ; 0 -conj(s) conj(c)];
% $$$ c=G3(1);s=G3(2);
% $$$ G3f=[c s 0 ; -conj(s) conj(c) 0; 0 0 1];
% $$$ UPST=G1f*G2f*G3f;
% $$$ 
% $$$ fprintf('%s','norm(original U - new U): ')
% $$$ fprintf('%d\n\n',norm(UP-UPST))

% $$$  % The shift-through operation tested on some matrices.
% $$$  % Now we test the shift-through in the other direction
% $$$ disp('--- Testing of the Shift-through operation  (other direction) ---')
% $$$  I=sqrt(-1);
% $$$  x=rand(1,1)+I*rand(1,1);y=rand(1,1)+I*rand(1,1);
% $$$  [c,s,r]=Givens(x,y); H1=[c;s];
% $$$  H1f=[c s 0 ; -conj(s) conj(c) 0; 0 0 1];
% $$$  
% $$$  x=rand(1,1)+I*rand(1,1);y=rand(1,1)+I*rand(1,1);
% $$$  [c,s,r]=Givens(x,y); H2=[c;s];
% $$$  H2f=[1 0 0 ; 0 c s ; 0 -conj(s) conj(c)];
% $$$  
% $$$  x=rand(1,1)+I*rand(1,1);y=rand(1,1)+I*rand(1,1);
% $$$  [c,s,r]=Givens(x,y); H3=[c;s];
% $$$  H3f=[c s 0 ; -conj(s) conj(c) 0; 0 0 1];
% $$$  UP=H1f*H2f*H3f;
% $$$  
% $$$  % Perform now the Shift-Through operation.
% $$$  [G1,G2,G3]=ST(H1,H2,H3);
% $$$  
% $$$  c=G1(1);s=G1(2);
% $$$  G1f=[1 0 0 ; 0 c s ; 0 -conj(s) conj(c)]; 
% $$$  c=G2(1);s=G2(2);
% $$$  G2f=[c s 0 ; -conj(s) conj(c) 0; 0 0 1];
% $$$  c=G3(1);s=G3(2);
% $$$  G3f=[1 0 0 ; 0 c s ; 0 -conj(s) conj(c)]; 
% $$$  UPST=G1f*G2f*G3f;
% $$$  
% $$$  fprintf('%s','norm(original U - new U): ')
% $$$  fprintf('%d\n\n',norm(UP-UPST))

% $$$ The Fusion operation tested on some rotations.
% $$$ disp('--- Testing of the Fusion operation ---')
% $$$ I=sqrt(-1);
% $$$ x=rand(1,1)+I*rand(1,1);y=rand(1,1)+I*rand(1,1);
% $$$ [c,s,r]=Givens(x,y); G1=[c;s];
% $$$ G1f=[c s ; -conj(s) conj(c)];
% $$$ 
% $$$ x=rand(1,1)+I*rand(1,1);y=rand(1,1)+I*rand(1,1);
% $$$ [c,s,r]=Givens(x,y); G2=[c;s];
% $$$ G2f=[c s ; -conj(s) conj(c)];
% $$$   
% $$$ G3=Fusion(G1,G2);
% $$$ c=G3(1);s=G3(2);
% $$$ G3f=[c s ; -conj(s) conj(c)];
% $$$ 
% $$$ fprintf('%s','norm(G1*G2 - G3 ): ')
% $$$ fprintf('%d\n\n',norm(G1f*G2f-G3f))
% $$$ pause


% $$$  % Generate the pyramid QR factorization
% $$$  disp('--- Testing of Givens rotations, gplot function, matrix reconstruction ---')
% $$$  fprintf('%s','Generate pyramidal factorization of U')
% $$$  A=rand(n,n);
% $$$  [Grot,Gind,R]=PyramidQR(A);
% $$$  fprintf('%s\n','Plot the corresponding graphical representation')
% $$$  Gplot(Grot,Gind)
% $$$  fprintf('%s\n','Reconstruct the matrix')
% $$$  Ar=ConsMatLeft(Grot,Gind,R);
% $$$  fprintf('%s','norm(original A - reconstructed A): ')
% $$$  fprintf('%d\n\n',norm(Ar-A))

% $$$ % Test some important routines
% $$$ % Transfering rotations through an upper triangular matrix R
% $$$ % in both directions left <-> right
% $$$ disp('--- Testing of transfering left and right and ConsMatRight ---')
% $$$ A=rand(n,n);
% $$$ [Grot,Gind,R]=PyramidQR(A);
% $$$ Ao=ConsMatLeft(Grot,Gind,R); % Original Matrix
% $$$ fprintf('%s','Transfer to the right ')
% $$$ [Grrot,Grind,Rr]=TransfRight(Grot,Gind,R);
% $$$ Ar=ConsMatRight(Grrot,Grind,Rr);
% $$$ fprintf('%s','norm(original A - reconstructed A): ')
% $$$ fprintf('%d\n',norm(Ar-Ao))
% $$$ 
% $$$ fprintf('%s','Transfer to the left ')
% $$$ [Glrot,Glind,Rl]=TransfLeft(Grrot,Grind,Rr);
% $$$ Al=ConsMatLeft(Glrot,Glind,Rl);
% $$$ fprintf('%s','norm(original A - reconstructed A): ')
% $$$ fprintf('%d\n\n',norm(Al-Ao))
% $$$ pause

% $$$ % Construct unitary similar matrix, via prescribed position vector.
% $$$ disp('--- Testing Unitary Similarity transform to compressed representation ---')
% $$$ n=10;
% $$$ 
% $$$ A=rand(n,n);
% $$$ [Grot,Gind,R]=PyramidQR(A);
% $$$ eigA=eig(A);
% $$$ %fprintf('%s','Reduction to Hessenberg format ')
% $$$ %or=ones(1,n-1);
% $$$ %[Grot,Gind,R]=UniSimCR(Grot,Gind,R,or);
% $$$ 
% $$$ %fprintf('%s','Reduction to Hessenberg-like format ')
% $$$ %or=zeros(1,n-1);
% $$$ %[Grot,Gind,R]=UniSimCR(Grot,Gind,R,or);
% $$$ 
% $$$ fprintf('%s','Reduction to CMV-format ')
% $$$ or=zeros(1,n-1);
% $$$ or(1:2:end)=1;
% $$$ [Grot,Gind,R]=UniSimCR(Grot,Gind,R,or);
% $$$ 
% $$$ % help plot function
% $$$ I=[];
% $$$ for i=1:size(Gind,1)
% $$$   if (Gind(i,4)==0)
% $$$     I=[I,i];
% $$$   end
% $$$ end
% $$$ Gtind=Gind(I,:);
% $$$ Gtrot=Grot(:,I);
% $$$ Gplot(Gtrot,Gtind(:,1:3))

% $$$ 
% $$$ % Split a sequence in ascending descending formats
% $$$ disp('--- Testing the splitting of a sequence of rotations ---')
% $$$ n=20;
% $$$ A=rand(n,n);
% $$$ % Start by computing the QR-factorization
% $$$ disp('pyramid qr factorization')
% $$$ [Grot,Gind,R]=PyramidQR(A);
% $$$ eigA=eig(A);
% $$$ % Perform the unitary similarity transformation to Hessenberg form.
% $$$ % Select between the following lines
% $$$ %or=zeros(1,n-1);
% $$$ %or=ones(1,n-1);
% $$$ %or=ones(1,n-1);or(10:end)=0;
% $$$ or=zeros(1,n-1);or(10:end)=1;
% $$$ %or=zeros(1,n-1);or(1:2:end)=1;
% $$$ [Grot,Gind,R]=UniSimCR(Grot,Gind,R,or);
% $$$ %[Glrot,Glind,Grrot,Grind]=SplitSeqRot(Grot, Gind,'da','l');
% $$$ [Glrot,Glind,Grrot,Grind]=SplitSeqRot(Grot, Gind,'da','r');
% $$$ % To visualize the result we use gplot
% $$$ fprintf('%s\n','DA-splitting ')
% $$$ fprintf('%s\n','Plot the left sequence')
% $$$ figure(1)
% $$$ Gplot(Glrot,Glind)
% $$$ pause
% $$$ fprintf('%s\n','Plot the right sequence')
% $$$ figure(2)
% $$$ Gplot(Grrot,Grind)
% $$$ pause
% $$$ %[Glrot,Glind,Grrot,Grind]=SplitSeqRot(Grot, Gind,'da','l');
% $$$ [Glrot,Glind,Grrot,Grind]=SplitSeqRot(Grot, Gind,'da','r');
% $$$ % To visualize the result we use gplot
% $$$ fprintf('%s\n','AD-splitting ')
% $$$ fprintf('%s\n','Plot the left sequence')
% $$$ figure(1)
% $$$ Gplot(Glrot,Glind)
% $$$ pause
% $$$ fprintf('%s\n','Plot the right sequence')
% $$$ figure(2)
% $$$ Gplot(Grrot,Grind)
% $$$ pause


% $$$ % Compute eigenvalues of a compressed represented matrix, via a
% $$$ % variation of the QR-algorithm.
% $$$ disp('--- Testing eigenvalue computation via an implicit approach')
% $$$ n=20;
% $$$ A=rand(n,n)+sqrt(-1)*rand(n,n);
% $$$ % Start by computing the QR-factorization
% $$$ disp('pyramid qr factorization')
% $$$ [Grot,Gind,R]=PyramidQR(A);
% $$$ eigA=sort(eig(A));
% $$$ % Perform the unitary similarity transformation to Hessenberg form.
% $$$ % Select between the following lines
% $$$ or=zeros(1,n-1);
% $$$ or=ones(1,n-1);
% $$$ %or=ones(1,n-1);or(5:end)=0;
% $$$ %or=zeros(1,n-1);or(5:end)=1;
% $$$ %or=zeros(1,n-1);or(1:2:end)=1;
% $$$ [Grot,Gind,R]=UniSimCR(Grot,Gind,R,or);
% $$$ % Start with the eigenvalue computeations
% $$$ disp('eigenvalue computations')
% $$$ % Select between the following lines
% $$$ %[ev,niter]=EigCR(Grot,Gind,R,0);
% $$$ %[ev,niter]=EigCR(Grot,Gind,R,1);
% $$$ [ev,niter]=EigCR(Grot,Gind,R,0);
% $$$ disp('Comparison of eigenvalues')
% $$$ [eigA,sort(ev)]
% $$$ niter
% $$$ sum(niter)


% $$$ % Compute eigenvalues of a compressed represented matrix, via a
% $$$ % variation of the QR-algorithm.
% $$$ disp('--- Testing eigenvalue computation via an implicit approach')
% $$$ n=40;
% $$$ A=rand(n,n)+sqrt(-1)*rand(n,n);
% $$$ % Start by computing the QR-factorization
% $$$ disp('pyramid qr factorization')
% $$$ [Grot,Gind,R]=PyramidQR(A);
% $$$ eigA=sort(eig(A));
% $$$ % Perform the unitary similarity transformation to Hessenberg form.
% $$$ % Select between the following lines
% $$$ %or=zeros(1,n-1); %--> Hessenberg
% $$$ %or=ones(1,n-1);  %--> Hessenberg-like
% $$$ %or=ones(1,n-1);or(5:end)=0; 
% $$$ %or=zeros(1,n-1);or(5:end)=1;
% $$$ or=zeros(1,n-1);or(1:2:end)=1; % --> CMV
% $$$ 
% $$$ [Grot,Gind,R]=UniSimCR(Grot,Gind,R,or);
% $$$ % Show structure of rotations
% $$$ Gplot(Grot,Gind)
% $$$ disp('Rotational pattern taken -> See figure')
% $$$ % Start with the eigenvalue computeations
% $$$ disp('eigenvalue computations')
% $$$ % Select between the following lines
% $$$ %[ev,niter]=EigCR(Grot,Gind,R,0);
% $$$ %[ev,niter]=EigCR(Grot,Gind,R,1);
% $$$ 
% $$$ disp('------------------------------------')
% $$$ disp('Case 1: random changes')
% $$$ [ev,niter,nleft,nright,nchanges]=EigCRran(Grot,Gind,R);
% $$$ disp('Comparison of eigenvalues')
% $$$ [eigA,sort(ev)]
% $$$ %niter
% $$$ fprintf('%s %d\n', 'Total number of iterations: ',sum(niter))
% $$$ fprintf('%s %d\n', 'Average number of iterations: ',sum(niter)/n)
% $$$ fprintf('%s %d\n', 'Number of rotations taken left: ',nleft)
% $$$ fprintf('%s %d\n', 'Number of rotations taken right: ',nright)
% $$$ fprintf('%s %d\n', 'Number of direction changes:',nchanges)
% $$$ 
% $$$ disp('------------------------------------')
% $$$ disp('Case 2: only left final rotation (Hessenberg)')
% $$$ [ev,niter]=EigCRfdc(Grot,Gind,R,1,0,1);
% $$$ disp('Comparison of eigenvalues')
% $$$ [eigA,sort(ev)]
% $$$ %niter
% $$$ fprintf('%s %d\n', 'Total number of iterations: ',sum(niter))
% $$$ fprintf('%s %d\n', 'Average number of iterations: ',sum(niter)/n)
% $$$ 
% $$$ disp('------------------------------------')
% $$$ disp('Case 3: only right final rotation (Hessenberg-like)')
% $$$ [ev,niter]=EigCRfdc(Grot,Gind,R,0,1,1);
% $$$ disp('Comparison of eigenvalues')
% $$$ [eigA,sort(ev)]
% $$$ %niter
% $$$ fprintf('%s %d\n', 'Total number of iterations: ',sum(niter))
% $$$ fprintf('%s %d\n', 'Average number of iterations: ',sum(niter)/n)
% $$$ 
% $$$ disp('------------------------------------')
% $$$ disp('Case 4: Changing every step (CMV-shape)')
% $$$ [ev,niter]=EigCR(Grot,Gind,R,1);
% $$$ disp('Comparison of eigenvalues')
% $$$ [eigA,sort(ev)]
% $$$ %niter
% $$$ fprintf('%s %d\n', 'Total number of iterations: ',sum(niter))
% $$$ fprintf('%s %d\n', 'Average number of iterations: ',sum(niter)/n)
