% BLS   Build a lower triangular semiseparable matrix
%       
%        SSL=BLS(G,d)
%        Given a sequence of Givens transformations and a diagonal
%        the function constructs a semiseparable matrix from it
%        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)]
%
%
%
%        SSL=BLS(G,d)  OR  SSL=BLS(G,d,'tb')
%        will perform the givens in the direction from top to bottom.
%
%        SSL=BLS(G,d,'rl')
%        will perform the givens from right to left
%            
%
%        SSL=BLS(G,d,opt,verbose)
%        verbose=1 will print the direction of building the matrix
%        verbose=0 no info will be printed
%
%
%        Software of the MaSe - Group
%        mase-at-cs.kuleuven.ac.be
%        Revision Date: 20/05/2003


function SSU=BLS(G,D,opt,verb)

  if not(exist('opt'))
    opt='tb';
  end
  if not(exist('verb'))
    verb=0;
  end
  
  
  n=length(D);
  A=zeros(n,n);
  if n==0
    SSU=[];
  elseif n==1;
    SSU=D(1);
  else
    if (opt=='rl')
      if verb
        disp('from right to left')
      end
      A(n,n)=D(1);
      for i=n-1:-1:1;
        % construction of the givens transformation
        Giv=[G(1,n-i) , G(2,n-i); -G(2,n-i), G(1,n-i)]; 
        A(i:i+1,:)=Giv*A(i:i+1,:);
        A(i,i)=D(n-i+1);
      end;
      SSU=A';
    elseif (opt=='tb') 
      if verb
        disp('from top to bottom')
      end
      A(1,1)=D(1);
      for i=1:n-1;
        % construction of the givens transformation
        Giv=[G(1,i) , G(2,i); -G(2,i), G(1,i)]; 
        A(:,i:i+1)=A(:,i:i+1)*Giv;
        A(i+1,i+1)=D(i+1);
      end;
      SSU=A';  
    else
      disp('ERROR UNKNOWN OPTION')
    end
  end


