% REDHL   Reduce a Hessenberg-Like matrix to unreduced form
%
%       [G,d,R]=REDHL(G,d,R,c) transforms a Hessenberg-Like matrix to unreduced
%       form, input is the Givens-vector representation of the 
%       Hessenberg-like matrix, R is the strictly upper triangular part 
%       of the Hessenberg-like matrix, c is an optional argument and
%       stands for the cutting off criterion, standard it is set to
%       10E-14. The output consists of the Givens vector-representation
%       where the blocks are separated by a column [1;0] in the vector G.
%
%       
%
%       Software of the MaSe - Group
%       mase-at-cs.kuleuven.ac.be
%       Revision Date: 05/05/2004


function [G,d,R]=REDHL(G,d,R,c)

  HL=BHL(G,d,R);
  
  % setting the appropriate cutting level
  if not(exist('c'))
    c=10^(-14);
  end
  
  % first we will check for zeros, on the diagonal, this can happen when
  % either a diagonal element is zero, or either the Givens
  % transformation equals [0,1]
  n=length(d);
  
  
  % perform all the necessary transformations on the Hessenberg-like
  % matrix to make it upper triangular.
  ctu=1;  

  while (ctu==1)
    % make the matrix upper triangular
    UR=uppertr(G,d,R);
    
    % we will now check which upper diagonal elements are includable in the
    % semiseparable structure using the criterion of the thesis on page 178
    % incluelm, will store the positions which have to be removed
    incluelm=checkinclelm(G,d,UR,c);
    
    if ~isempty(incluelm)
      nincl=length(incluelm);
      % remove the includable elements out of the structure
      [G,d,R]=reminclelm(G,d,UR,incluelm(nincl));
    else
      ctu=0;
    end
  end
    
  
  % initialization for the diagonalblock control
  nd=length(d);
  
  hlp=0;
  for i=1:nd-1
    hlp=G(2,i)^2*(hlp+d(i)^2);
    if abs(hlp)<c
      G(:,i)=[1;0];
    end
  end
  

  
  % Adding enough entries to the reduced ss form, to achieve the correct
  % size
  nrem=n-nd;
  d=[d,zeros(1,nrem)];
  G=[G,[ones(1,nrem);zeros(1,nrem)]];
  R=[R,zeros(nd-1,nrem)];
  R=[R;zeros(nrem,n-1)];
  
  
  %-----------------------------------------------------------
  
  function incluelm=checkinclelm(G,d,R,c);
  
  incluelm=[];
  n=length(d);
    
  hlp=d(1)^2; %hlp corresponds to the norm of the row Rk+dk, namely a on page 178
  for i=1:n-1
    factor=abs(R(i+1,i+1))*sqrt(hlp);
    check=c*(hlp+R(i,i+1)^2);
    if factor<check
      incluelm=[incluelm,i];
    end
    hlp=G(2,i)^2*hlp+d(i+1)^2;
  end




%---------------------------------------------------------------
  function R=uppertr(G,d,R);
   
    n=length(d); 
   
    %enlarging the matrix R a little bit
    R=[zeros(n-1,1),R];
    R=[R;zeros(1,n)];
  
   
    % add the appropriate diagonal element to the upper triangular matrix
    R(n,n)=d(n);
    
    for i=n:-1:2
      % Calculating the Givens transformation
      Giv=[G(1,i-1),G(2,i-1);-G(2,i-1),G(1,i-1)];
      
      % Perform now the Givens transformation to the upper triangular
      % matrix
      R(i-1:i,i-1:n)=Giv*R(i-1:i,i-1:n);
      
      % add the appropriate diagonal element to the upper triangular matrix
      R(i-1,i-1)=d(i-1);
    end;
  
  
  %----------------------------------------------------------------
  
  function [G,d,R]=reminclelm(G,d,R,elm)
  
    % This function is quite expensive, but has to remove each incluelm
    % separately
      
    n=length(d);
      
    for i=elm+1:n-1
      % calculating the necessary transformation
      [c,s,r]=Givens1(R(i,i+1),R(i+1,i+1));
      
      % Calculating the Givens transformation
      Giv=[c,s;-s,c];
      
      % storing temporily these values
      Ghelp(:,i-elm)=[c;s];
  
      % Perform now the Givens transformation to the upper triangular
      % matrix
      R(i:i+1,i+1:n)=Giv*R(i:i+1,i+1:n);
    end
    R(n,n)=0;
    
    % now we will rebuild the upper part of the Hessenberg-like matrix
    % by performing the first sequence of transformations to the right
    % and thereby storing the new temporary Givens-vectore representation
    
    % first we already perform the last transformation because we will
    % neglect the last column...

    
    for i=n:-1:2
      % initializing the givens transformation
      Giv=[G(1,i-1),-G(2,i-1);G(2,i-1),G(1,i-1)];
      c=G(1,i-1);
      s=G(2,i-1);
            
      % perform the transformation on the right
      R(1:i-1,i-1:i)=R(1:i-1,i-1:i)*Giv;  
      
      % storing the new diagonal element...
      % remark that we have 1 element less to store
      % as the new representation lost lets say a zero row
      dnew(n-i+1)=R(i,i);
      
      % updating the givens
      Gnew(1:2,n-i+1)=[c ; s]; 
    end
    dnew(n)=R(1,1);
    
   
    G=Gnew(:,2:n-1);
    d=dnew(2:n);
    [G,d]=SR(G,d);
   
    % now we still have to perform the special givens transformations
    % used to chase the includable elements away.
    % to make the adaptation of the Givens vector representation easy, we
    % first swap the representation, in this way, we only need to adapt
    % the diagonal elements.
    
    for i=1:n-2
      R(i,i)=G(1,i)*d(i);
    end
    R(n-1,n-1)=d(n-1);
    
    for i=elm+1:n-1
      % Calculating the Givens transformation
      Giv=[Ghelp(1,i-elm),-Ghelp(2,i-elm);Ghelp(2,i-elm),Ghelp(1,i-elm)];
      
      % Perform now the Givens transformation to the upper triangular
      % matrix
      R(1:i,i:i+1)=R(1:i,i:i+1)*Giv;
            
      % store now the new diagonal elements which have slightly changed.
      if i~=n-1
       d(i)=R(i,i)/G(1,i);
       d(i+1)=R(i,i+1)/G(1,i)*G(2,i);
       if i~=n-2
         R(i+1,i+1)=d(i+1)*G(1,i+1);
       else
         R(i+1,i+1)=d(i+1);
       end
      else
        d(i)=R(i,i);
      end
     
      % also the following diagonal element needs to be adapted, as this
      % is also involved in the transformation working on both of the columns
    end
  
     
      R=triu(R,1);
      R=R(1:n-2,2:n-1);
      

  

      
      
      
  %----------------------------------------------
  function [G,d]=remzero(G,d,zeroelm)

  n=length(d);
  nc=n;
  nzero=length(zeroelm);
  if not(isempty(zeroelm))
    begin=1;
    if zeroelm(1)==1
      G=G(:,1:n-2);
      d=d(:,1:n-1);
      begin=2;
      nc=nc-1;
    end
  for i=begin:nzero
    pos=n-zeroelm(i)+1;
    [c,s,r]=Givens1(d(pos)*G(2,pos),d(pos+1));
    d(pos+1)=r;
    d=[d(1:pos-1),d(pos+1:nc)];
    G=[G(:,1:pos-1),G(:,pos+1:nc-1)];
    nc=nc-1;
  end
  end
  
  %i=1;
  % check of the Givens transformatie [0;1] voorkomt
  %while (i<=n-1)
  %for i=1:n-1
  %abs(d(i)*G(1,i))
  %  if abs(hlp)<c
  %     [c,s,r]=Givens1(d(i)*G(2,i),d(i+1));
  %     d(i+1)=r;
  %     d=[d(1:i-1),d(i+1:n)];
  %     %d(n)=0;
  %     G=[G(:,1:i-1),G(:,i+1:n-1)];
       %G(:,n-1)=[1;0];
  %     n=n-1;
  %     %i=i;
  %     BUS(G,d)
  %     pause
  %  else
  %    i=i+1;
  %  end
  %end
  
  
  
 %------------------------------------------------------------------
%
%  THE FUNCTION GIVENS1
%
%-------------------------------------------------------------------

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