%  YWSOLVERBANDHSS Solves a system of equations for a 
%                   strongly nonsingular band plus semiseparable matrix
%    
%    X=YWSOLVERBANDHSS(U,V,A) is the solution of the system (B+S)X=Q
% 
%    A has to be a matrix of dimension n times l+1 where l is the band
%    width of B. More information concerning this representation can be
%    found in the routine BBAND
%
%    The matrix S is a higher order generator semiseparable matrix represented
%    with two vectors U,V
%
%    The right hand side Q is a n x (k+l) matrix. More information
%    concerning this matrix Q can be found in the corresponding technical
%    report.
%
%   
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 14/04/2005

function Y=YWSOLVERBANDHSS(U,V,A)
  
  % let us start the initialization procedure
  % with n we denote the size of the matrix
  % with l we denote the band width
  % with p is meant the semiseparability rank
    
  [n,l]=size(A);
  l=l-1;
  [n,p]=size(U);
  

  % The first l times in the for loop are a little different, because we
  % reduce the complexity by not starting with a full right hand side 
  alpha=zeros(n,l+p);
  alpha(1,[1:p,p+l])=[-V(1,:),-1]./(U(1,:)*V(1,:)'+A(1,l+1));

  % initialization of the product Q'*Y  this is a (l+p) x (l+p) matrix
  qy=[V(1,:),zeros(1,l-1),1]'*alpha(1,:);
  
  % the main loop in which we use always the previous solution
  for k=2:n
    
    % initialize a help var c
    % Because the product c*qy appears two times
    c=[U(k,:),A(k,1:l)];
    cqy=c*qy;
       
    % Calculate the denominator of alpha
    denominator=cqy*c'+U(k,:)*V(k,:)'+A(k,l+1);
    
    % the computation of the vector alpha
    alpha(k,:)=[-V(k,:)-cqy(1:p),-cqy(p+2:l+p),-1]/denominator;
  
    % Calculation of the new Product Q'*Y
    qyc=qy([1:p,p+2:l+p],:)*c';
    qyc(1:p)=qyc(1:p)+V(k,:)';
    qy=[qy([1:p,p+2:l+p],[1:p,p+2:l+p])+qyc*alpha(k,1:l+p-1),qyc* ...
            alpha(k,l+p)];
    qy=[qy;alpha(k,:)];
  end
  
  
  % This part is excluded from the main loop, because in this way, we
  % can dramatically reduce the complexity of the overall algorithm
  
  % initialization
  Y=zeros(n,l+p);
  Y(n,:)=alpha(n,:);
  

  % F1 is the p x p matrix used as the factor 
  Fn=eye(l+p);
  
  % starting the construction of y
  for i=n-1:-1:1
     Fn(1:p,:)=Fn(1:p,:)+U(i+1,:)'*Y(i+1,:);
     Fn(p+2:l+p,:)=Fn(p+1:l+p-1,:)+A(i+1,2:l)'*Y(i+1,:);
     Fn(p+1,:)=A(i+1,1)*Y(i+1,:);
     Y(i,:)=alpha(i,:)*Fn;   
  end
