% QRSSSD    Performing a QR step with shift on the semiseparable plus
%           diagonal matrix
%        
%        [G,d]=QRSSS(G,d,diag,kappa) 
%        In this function one QR step will be performed on the
%        semiseparable matrix plus diagonal which will be given as input.
%        the input is given in the special matrix representation
%        used for semiseparable matrices namely Givens G and vector d
%        the diagonal will be stored in diag
%
%        kappa is the shift we will use
%        1 is the Rayleigh shift
%        2 is the wilkinson shift
%
%
%        Software of the MaSe - Group
%        mase-at-cs.kuleuven.ac.be
%        Revision Date: 15/06/2004



function [G,d]=QRSSSD(G,d,dg,x)


  % be careful when calculating the shift, as also the diagonal is
  % involved in this computation
  if (isempty(d))
    d=[];
    dg=[];
    G=[];
  elseif length(d)==1
    d=d;
    dg=dg;
    G=[];
  else
      
    % the size of the matrix
    n=length(d);
    
    % initializing the shift kappa,
    % depending on the choice given
    switch x
     case 1
      kappa=d(n)+dg(n);
     case 2
      B=[G(1,n-1)*d(n-1)+dg(n-1),G(2,n-1)*d(n-1);G(2,n-1)*d(n-1),d(n)+dg(n)]; 
      le=eig(B);
      if abs(le(1)-B(2,2))<  abs(le(2)-B(2,2)),                       
        kappa =le(1);
      else
        kappa=le(2);
      end
    end

    % We will first calculate the special Givens, this is more
    % easy than after Crep
    c=G(1,1);
    s=G(2,1);
    givspec=givens(c*(dg(1)-kappa)+d(1),-s*(dg(1)-kappa));


    % Perform now the first sequence of Givens transformations, 
    % we know that the resulting ss + diag matrix has the same diagonal
    % but shifted down one position
    [G,d]=CRep(G,d,dg);
    
    
    % Now the second sequence of Givens transformations will
    % be performed on the semiseparable matrix, thereby giving
    % as output immediately the new Givens Vector representation.
    [G,d]=PerSeqGiv(G,d,dg,givspec);


    % the following function will recreate the 
    % givens and the diagonal to remake
    % the semiseparable matrix
    
    %disp('new build representation')
    [G,d]=SR(G,d);
  end


%------------------------------------------------------------
% This function will perform the first sequence of transformations on the
% semiseparable plus diagonal matrix. 
%
% [G,d]=CRep(G,d,dg)


function [Gnew,dnew]=CRep(G,d,dg)

  % the new representation 
  % will be stored in Gnew and
  % dnew
  % this can clearly be seen as an
  % easy O(n) algorithm 
  
  n=length(d);
  
  % initialization
  dnew(1)=d(n);  
  workdg=dg(n);
  
  for i=n:-1:2
    % initial parameter settings
    c=G(1,i-1);
    s=G(2,i-1);
    dwork=dnew(n-i+1);

    % updating the diagonal of the semiseparable
    dnew(n-i+1)=-s^2*d(i-1)+c*dwork;
    dnew(n-i+2)=s*(c*s*d(i-1)+s*dwork)+c*d(i-1); 

        
    % updating the givens
    Gnew(1:2,n-i+1)=[c ; s]; 
    
    % updating now the diagonal representation with the part
    % coming from the Givens applied on the diagonal.
    dnew(n-i+1)=dnew(n-i+1)+c*(-dg(i-1)+workdg);

    % workdg stores the upper left element of the 2 by 2 block
    % on which we perform the operations
    workdg=c^2*dg(i-1)+s^2*workdg;
    
  end
  
  % updating the final element.
  dnew(n)=dnew(n)+workdg; 

%------------------------------------------------------------
% this function will perform
% the disturbing givens transformations
% onto the semiseparable matrix
% which is constructed by G and d
% the matrix will not at all be completely
% semiseparable and with a special chase
% constructed of givens transformations
% through the semiseparable matrix
% this will be corrected.

function [Gn,dn]=PerSeqGiv(G,d,dg,giv)

n=length(d);

% because the application of the
% sequential givens transformations,
% divide the matrix into two semiseparable
% parts we have the following functions
% which will reduce the lower block
% and give enough output to calculate the
% special givens transformation
% the change in representation of the lower
% block is denoted by the variable factor

factor=1;
Gn=[];
dn=[];
f1=1;
f2=1;
d(n)=d(n)-dg(1);

for i=2:n-1
  % This routine will perform each step a Givens transformation in order
  % to chase the disturbed structure down, this procedure is iterated and
  % the new representation appears in Gn,dn.
  [G,d,Gn,dn,f1,f2,giv]=RedLowSS(G,d,Gn,dn,f1,f2,giv,dg,i);
  %if i>2
  %  [Gt,dt]=SR(Gn,dn);
  %  BSS(Gt,dt)+diag(dg)
  %end
  %pause
end



%------------------------------------------------------------\
% we will reduce the representation of the
% lower triangular semiseparable matrix
% because one givens transformation is performed
% which actually reduces this representation

% some ouput is given 
% factor for the correct calculation of the new
% lower triangular semiseparable block
% element to calculate the following special givens
% transformation

% Gn and dn are the new Givens vector representations


function [G,d,Gn,dn,f1,f2,giv]=RedLowSS(G,d,Gn,dn,f1,f2,giv,dg,i);
  
  % initialization of the dimension
  n=length(d);

  % because there are large connections between the
  % elements in semiseparable matrices
  % a lot of elements have to be initialized
    
  % Short initialization of the Givens
  c1=G(1,n-1);
  s1=G(2,n-1);

  c2=G(1,n-2);
  s2=G(2,n-2);
  

  % Initialization of the elements in B, using the factors.
  x=d(n); %-dg(i-1);
  y=f2*s1*d(n-1);
  z=c1*d(n-1);

  % This factor needs to be stored to calculate the dependencies in
  % the new Givens vector representation.
  f1giv=f1*s1;

  
  % Plugging everyting in the matrix Bn
  % note that the elements in the final row, only denote the dependencies
  % as the element d(n-2) might be zero, this corresponds to the special 
  % Givens transformation
  Bn=[x y ; y z ; f2*s1  c1];
  
  % Performing the essential Givens transformations in order to calculate
  % the new representation of the upper part. Remark once more that f1
  % and f2 do not represent the real elements but just a factor to
  % calculate the dependencies.
  Bn(1:2,:)=giv*Bn(1:2,:);
  Bn=Bn*giv';
  f1=Bn(3,1);
  f2=Bn(3,2);

  % Based on the factors f1 f2 and the element d(n-2) we can determine
  % the specific type of transformation needed to perform the correct
  % step in the chasing procedure
  Bn(2,2)=Bn(2,2)+dg(i-1)-dg(i);
  giv=csg(Bn,d(n-2),s2,c2);%,dg,i);
  

  % everything is done except the change in the representation
  % of the lower triangular semiseparable matrix
  % we will cut off elements and change some
  d(n-1)=Bn(2,2); 
  d=d(1:n-1);
  G=G(:,1:n-2);
  
 
  % Let us now store the new Givens and vector representation
  if length(dn)==0
    dn(n)=Bn(1,1);
  else
    [c,s,r]=Givens1(f1,f1giv);
    Gn(:,n)=[c;s];
    if c~=0
      f1=f1/c;
      dn(n)=Bn(1,1)/c;
    end
  end
  
  
  % if this iss the final loop, we calculate all the elements
  if length(d)==2
    
    % We work now with the real last block, so multiply with
    % the real diagonal element
    Bn(3,:)=Bn(3,:)*d(n-2)*s2;
    
    %d(n-2)
    %n
    %pause
    % Make the matrix square for the final transformation
    Bn(1,3)=Bn(3,1);  
    Bn(2,3)=Bn(3,2);
    Bn(3,3)=c2*d(n-2);
    
    % Perform the last transformation
    Bn(2:3,:)=giv*Bn(2:3,:);
    Bn(:,2:3)=Bn(:,2:3)*giv';
    
    % Adding the final elements to the representation of the new part
    [c,s,r]=Givens1(Bn(2,2),Bn(2,1)/Gn(1,3));
    Gn(:,2)=[c;s];
    dn(2)=Bn(2,2)/c;
    Bn(3,2)=Bn(3,2)/c;
    
    % updating the element Bn(3,3) before calculating the representation
    Bn(3,3)=Bn(3,3)+dg(i)-dg(i+1);
    [c,s,r]=Givens1(Bn(3,3),Bn(3,2));
    Gn(:,1)=[c;s];
    dn(1)=Bn(3,3)/c;
  end
  
% --------------------------------------------------------------------
% new method for calculating the special Givens transformation

 function [G]=csg(Bn,d3,s2,c2)
  % B denotes a 2 by 3 block, 
  % which is exactly the information
  % needed to calculate the givens
  
  % dit blokje bevat alle gegevens
  f1=Bn(3,1);f2=Bn(3,2);
  %Bn(2,2)=Bn(2,2)+dg(i-1)-dg(i);
  
 %XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
 % This is an important point in the transformations section
 % Here one can see when a diagonal element is considered zero.
 % The level is currently set to 10^(-14)
 %XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx
 
 
  %if ( abs(d3) < 10^(-14))
  %  disp('Special Givens desired')
  %else
    t=(f1*f2*d3*s2^2-Bn(2,1)*c2)/(Bn(2,1)*f2*s2-f1*s2*Bn(2,2));
  %end
  
  G=1/sqrt(1+t^2)*[t 1 ; -1 t]; 
  
  



%-----------------------------------------------
% needed functions for the main routine
%--------------------------------------

function [c,s,r] = Givens1(x,y);
%
%  computes c,s,r such that
%
%  [c  s] [x] = [r]
%  [-s c] [y]   [0]
%
%  with c*c + s*s = 1;
%
   if (y == 0),
      c = 1; s = 0; r = x;
   else
      if (abs(x) >= abs(y))
         t = y/x; r = sqrt(1 + t*t);
         c = 1/r;
         s = t*c;
         r = x*r;
      else
         t = x/y; r = sqrt(1 + t*t);
         s = 1/r;
         c = t*s;
         r = y*r;
      end
   end
% SR Swap the representation of the semiseparable matrix
%        
%    [G,d]=SR(G,d)
%    Given a sequence of Givens transformations and a diagonal
%    the function will give the representation of the matrix in 
%    the other direction.
%    building direction will change from
%
%      |
%      |      to  a representation   <-----
%      V
%
%    or in the other directions.
%     
%
%    cosine and sine of the givens have to be stored in the following 
%    way
%     
%     G= | c(1)  c(2)  c(3) .. .. .. c(n-1) |
%        | s(1)  s(2)  s(3) .. .. .. s(n-1) |
%      
%     d= [d(1) d(2) .. ..  d(n)]
%
%
%
% 
%        Software of the MaSe - Group
%        mase-at-cs.kuleuven.ac.be
%        Revision Date: 16/12/2003


function [G,d]=SR(G,d)

  
  n=length(d);  
  
  if (n~=1 && n~=0)
    dnew(n)=G(1,1)*d(1);
    r=d(1);
    
    for i=1:n-2
      [c,s,r]=Givens1(G(2,i)*r,d(i+1));
      Gnew(:,n-i)=[s;c];
      dnew(n-i)=G(1,i+1)*r;
    end
    
    i=n-1;
    [c,s,r]=Givens1(G(2,n-1)*r,d(n));
    Gnew(:,1)=[s;c];
    dnew(1)=r;
    
    G=Gnew;
    d=dnew;
  end
