% BUS   Build an upper triangular semiseparable matrix
%       
%        SSU=BUS(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)]
%
%
%
%        SSU=BUS(G,d)  OR  SSU=BUS(G,d,'lr')
%        will perform the givens in the direction
%
%                ------->
%      
%        from left to right
%
%
%        SSU=BUS(G,d,'bt')
%        will perform the givens from bottom to top
%              /\  
%             /  \
%              ||
%              ||
%              ||
%            
%
%        SSU=BUS(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: 16/12/2003


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

  if not(exist('opt'))
    opt='lr';
  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=='bt')
      if verb
        disp('from bottom to top')
      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=='lr') 
      if verb
        disp('from left to right')
      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
  
  


