%  LEVINSONSOLVER  Solves a system of equations for a 
%                  positive definite semiseparable plus diagonal
%    
%    X=LEVINSONSOLVER(u,v,d,b) is the solution of the system (S+D)X=b,
%    the matrix S+D is a semiseparable plus diagonal matrix represented
%    with two vectors u,v, and the vector d contains the diagonal
%    elements of the matrix D.
%    S+D=tril(u*v')+triu(v*u',1)+diag(d);
%
%    The matrix S+D should be positive definite and symmetric.
%    The complexity of the solver is 27n-4
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 24/02/2005
%


function x=LevinsonSolver(u,v,d,b)
    
% initialization of the length
n=length(u);
  
% initialization for the different vectors
denominator=(d(1)+u(1)*v(1));
a(1)=-v(1)/denominator;
mu(1)=b(1)/denominator;
f(1)=1;
g(1)=1;

% initialization for the different inproducts
ip=a(1)*v(1);
ipl=mu(1)*v(1);


% The main loop
for k=2:n
  % initialize
  denominator=(u(k)*v(k)+d(k)+u(k)^2*ip);
  
  % Calculation of alpha
  a(k)=(-v(k)-u(k)*ip)/denominator;  
  
  % Calculation of the factor f
  f(k)=(1+u(k)*a(k));
  
  % The calculation of mu
  mu(k)=(b(k)-u(k)*ipl)/denominator;

  % The factor g
  g(k)=mu(k)*u(k);
  
  % the innerproduct r^T x
  ipl=ipl+mu(k)*(u(k)*ip+v(k));
  
  % the innerproduct r^T y
  ip=(1+u(k)*a(k))*ip+v(k)*a(k);
  
end



% we construct now the solution with the 4 vectors.
x(n)=mu(n);
factor=g(n);

for i=n-1:-1:1  
  x(i)=mu(i)+factor*a(i);
  factor=g(i)+f(i)*factor;
end


