%  LEVSOLVERBAND Solves a system of equations for a 
%                strongly nonsingular band matrix
%    
%    X=LEVSOLVERHSS(B,d) is the solution of the system BX=d,
% 
%    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: 10/04/2005


function x=LEVSOLVERBAND(B,d)
   
  % 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;
  
 
  % 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);
  alpha(1,l)=-1/B(1,l+1);
  
  % initialization of the innerproduct  
  ty(l,l)=alpha(1,l);
   
  % Initialization for the Levinson system
  mu=zeros(n,1);
  mu(1)=d(1)/B(1,l+1);
  
  % initialization of the important product tx
  tx(l)=mu(1);
  % In fact tx is a column vector, but, otherwise matlab complains
  % due to a bug, a dimension mismatch in the tridiagonal case,
  % therefore, this implementation is written, such that tx is row vector
 
  
  % 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;
    tyb=ty(2:l,:)*B(k,1:l)';
    
    % 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;
    
    % updating of the value of mu
    mu(k)=(d(k)-B(k,1:l)*tx')/denominator;
    
    % Calculation of the new inproduct tx (this is a vector of length k)
    tx=[tx(2:l)+mu(k)*tyb',mu(k)];
         
    % Calculation of the new inproduct ty
    % Due to the projection matrix P, we use lots of index shuffling
    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
  
  % M2 is the vector of length p used as the factor
  % initialization of the factors
  M2=zeros(l);
  % similarly as in the definition of tx we have to use a row vector
  
  % initialization of the solution vector x
  x(n)=mu(n);
  
  % starting the construction of x
  for i=n-1:-1:1
    % updating the two factors M2 
    %M2=[B(i+1,1)'*x(i+1);B(i+1,2:l)'*x(i+1)+M2(1:l-1)];
    % Above is the reallife vector a column vector, below, the change due
    % to a matlab bug
    M2=[B(i+1,1)'*x(i+1),B(i+1,2:l)*x(i+1)+M2(1:l-1)];
    
    % calculating a new element in the vector x
    x(i)=mu(i)+alpha(i,:)*M2';  
  end
  
   
  % making a column of x
  x=x';
  
end
  
  
  