%  LEVSOLVERHSS  Solves a system of equations for a 
%                strongly nonsingular higher order semiseparable 
%                matrix
%    
%    X=LEVSOLVERHSS(u,v,b) is the solution of the system (S)X=b,
%    the matrix S is a higher order generator semiseparable matrix represented
%    with two vectors u,v, a
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 07/04/2005
%


function x=LEVSOLVERHSS(u,v,b)
    
  % let us start the initialization procedure
  % with n we denote the size of the matrix
  % with p we denote the semiseparability rank
  % of the semiseparable matrix generated by u and v
    
  [n,p]=size(u);
  
  % the denominator
  denominator=u(1,:)*v(1,:)';
  
  % initializing the loop for the Yule-Walker system
  alpha=zeros(n,p);
  alpha(1,:)=-v(1,:)./denominator;
    
  % initialization for the Levinson system
  mu=zeros(n,1);
  mu(1)=b(1)./denominator;
    
  % ry stands for the inproduct matrix, this matrix
  % is of dimension p x p
  % rx stands also for an inproduct, this is vector of length p
  ry=v(1,:)'*alpha(1,:);
  rx=v(1,:)'*mu(1);
  
  % r is the righthandside, this is also a growing matrix.
  % but because we do not want to reallocate memory everytime,
  % we do not define the matrix because it is in fact the matrix v

  % the main loop in which we use always the previous solution
  for k=2:n
    
    % because ury appears twice and also ryu
    ury=u(k,:)*ry;
    ryu=ry*u(k,:)';
    
    % first we calculate the vector alpha
    denominator=ury*u(k,:)'+u(k,:)*v(k,:)';
    
    % Let us calculate the factor mu (Levinson part)
    mu(k)=(b(k)-u(k,:)*rx)/denominator;
    
    % we need to store alpha, because we will need it
    % afterwards for constructing the solution (YW part)
    alpha(k,:)=(-1)*(v(k,:)+ury)/denominator;
    
    % let us calculate the new inner product matrix rx and ry
    rx=rx+mu(k)*(ryu+v(k,:)');
    ry=ry+(ryu+v(k,:)')*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
  x(n)=mu(n);
  
  % G is a helping vector of length p
  G=mu(n)*u(n,:)';
  
  % starting the construction of the solution x
  for i=n-1:-1:1
   
    % Calculating now from bottom to top the solution
    x(i)=mu(i)+alpha(i,:)*G;

    % updating the help vector G
    G=x(i)*u(i,:)'+G; 
  end  

  x=x';
end
  
  
