% REDSS   Reduce a semiseparable matrix to unreduced form
%
%       [G,d]=REDSS(G,d,c) transforms a semiseparable matrix to unreduced
%       form, input is the Givens-vector representation of the upper
%       triangular semiseparable 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]=REDSS(G,d,c)

  S=BSS(G,d);
  
  % 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);
  
  % locate the zero diagonal elements and store their positions in the vector
  zeroelm=[];
   
  % calculating the necessary information to check which elements are
  % includable in the semiseparable structure
  [dg,supd]=uppertr(G,d);

  dg,
  supd
  % checking the dependent rows
  incluelm=checkinclelm(G,d,dg,supd,c);
  incluelm

  disp('problem rows')
  pause

  % we will now remove the problem rows, by applying the correct
  % transformation and permuting the remaining entries.
  nincl=length(incluelm);
  for i=nincl:-1:1
    [G,d]=remzero(G,d,incluelm(i),c);
    eig(S)
    eig(BSS(G,d))
    disp('gelijk')
    pause
  end
  
   
  %swapping the representation such that we can redo the previous loop
  % now the remaining diagonalelements
  %zeroelm=[];
  %nc=length(d);
  %for i=1:nc
  %  if abs(d(i))<c
  %    zeroelm=[zeroelm,i];
  %  end
  %end
  %[G,d]=SR(G,d);
  %[G,d]=remzero(G,d,zeroelm);
  
  
  % initialization for the diagonalblock control
  nint=length(d);
  
  % check of er diagonaalblokken zijn  
  hlp=0;
  for i=1:nint-1
    hlp=G(2,i)^2*(hlp+d(i)^2);
    if abs(hlp)<c
      G(:,i)=[1;0];
    end
  end
  

  % terug aanvullen van de representatie, met de correcte nullen
  d=[d,zeros(1,n-nint)];
  G=[G,[ones(1,n-nint);zeros(1,n-nint)]];
   
%-----------------------------------------------------------
  
  function incluelm=checkinclelm(G,d,dg,supd,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(dg(i+1))*sqrt(hlp);
    check=c*(hlp+supd(i)^2);
    if factor<check
      incluelm=[incluelm,i];
    end
    hlp=G(2,i)^2*hlp+d(i+1)^2;
  end


 
  
  %---------------------------------------------
  % this function will construct lets say half of the
  % upper triangular matrix R, enough to be able to check for
  % inclusions in the diagonal
  % R is a vector containing the superdiagonal elements
  
    function [dg,supd]=uppertr(G,d);
   
    n=length(d); 
    
    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
      if i~=n
        sd=d(i-1)*G(2,i-1)*G(1,i);
      else
        sd=d(i-1)*G(2,i-1);
      end
      
      res=Giv*[sd;d(i)];

      dg(i)=res(2);
      supd(i-1)=res(1);
    end;
    dg(1)=d(1);
  
  %----------------------------------------------
  function [G,d]=remzero(G,d,zeroelm,c)

    n=length(d);
    % we know that the representation is arranged from top to bottom, so
    % we first transform the columns
      if abs(G(2,zeroelm-1))<c 
        % this means the row is already zero, therefore, we can just
        % remove it
        %zeroelm
        d=[d(1:zeroelm-1),d(zeroelm+1:n)];
        G=[G(:,1:zeroelm-1),G(:,zeroelm+1:n-1)];
      else 
        pos=zeroelm;
        if (pos+1)~=n
          [c,s,r]=Givens1(d(pos)*G(1,pos),d(pos)*G(2,pos)*G(1,pos+1));
        else
          [c,s,r]=Givens1(d(pos)*G(1,pos),d(pos)*G(2,pos));
        end
        
        % adapting the diagonal elements corresponding to the changes in
        % the representation
        d(pos)=r/G(1,pos);
        d(pos+1)=0;
        
        % we swap now the representation, in order to perform the second
        % transformation
        [G,d]=SR(G,d);
        
        %if pos~=2
           npos=n-pos;
           [c,s,r]=Givens1(d(npos+1)*G(1,npos+1),d(npos)*G(1,npos+1));
        %else
        %   [c,s,r]=Givens1(d(pos+1)*G(1,pos+1),d(pos)*G(1,pos+1));
        %end
        d(npos+1)=r/G(1,npos+1);
           
        d=[d(1:npos-1),d(npos+1:n)];
        G=[G(:,1:npos-1),G(:,npos+1:n-1)];   
        %d=[d(1:n-zeroelm),d(n-zeroelm+2:n)];
        %G=[G(:,1:n-zeroelm),G(:,n-zeroelm+2:n)];
      
        [G,d]=SR(G,d);
        
      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