%   YWSOLVERBAND  Solves a Yule Walker system of equations for a 
%                strongly nonsingular Band matrix of band size l
%    
%    X=YWSOLVERBAND(B) is the solution of the system S X=-[0;eye(l)],
% 
%    B 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
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 08/04/2005
%

function y=YWSOLVERBAND(B)
  
  % let us start the initialization procedure
  % with n we denote the size of the matrix
  % with l we denote the band width
  % of the semiseparable matrix generated by u and v
    
  [n,l]=size(B);
  l=l-1;
  
  if l==0
    % this means the band matrix is a diagonal matrix
    disp ('error, the matrix B is a diagonal matrix')
  else
  
  % 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(1,l)=-1/B(1,l+1);
  
  % initialization of the innerproduct  this is a l x l matrix
  ty(l,l)=alpha(1,l);
  
  % the main loop in which we use always the previous solution
  for k=2:n
    
    % Because the innerproduct B(k,1:l)* ty appears two times
    bty=B(k,1:l)*ty;
    
    % Let us define the denominator
    denominator=(bty*B(k,1:l)'+B(k,l+1));
    
    % the computation of the vector alpha
    % The multiplication with the Projection operator is removed
    % and written as a change in the indices
    alpha(k,:)=[-bty(:,2:l),-1]/denominator;
    
     
    % Calculation of the new inproduct 
    % Due to the projection matrix P, we use lots of index shuffling
    tyb=ty(2:l,:)*B(k,1:l)';
    ty(1:l-1,1:l)=[ty(2:l,2:l)+tyb'*alpha(k,1:l-1),tyb'*alpha(k,l)];
    ty(l,:)=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);
  y(n,:)=alpha(n,:);
  

  % F1 is the p x p matrix used as the factor 
  F1=eye(l);
  
  % starting the construction of y
  for i=n-1:-1:1

     F1=[B(i+1,1)*y(i+1,:);F1(1:l-1,:)+B(i+1,2:l)'* y(i+1,:)];
     y(i,:)=alpha(i,:)*F1;   
  end
  
  end
  
end
  
  
  