% REDUS   Reduce an upper triangular semiseparable matrix to unreduced form
%
%       [G,d]=REDUS(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.
%       And the zero singular values can be found in the lower right corner.
%       
%
%       Software of the MaSe - Group
%       mase-at-cs.kuleuven.ac.be
%       Revision Date: 05/05/2004


function [G,d]=REDUS(G,d,c)

  
  % 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 elmements and store their positions in the vector
  zeroelm=[];
    
  % now the remaining diagonalelements
  for i=1:n
    if abs(d(i))<c
      zeroelm=[zeroelm,i];
    end
  end
  
  
  % removing the zero diagonal elements, by swapping the representation
  % and updating the other diagonal elements
  [G,d]=SR(G,d);
  [G,d]=remzero(G,d,zeroelm);
 
   
  %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 [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
