% INV_SYM_TRID Inverts an symmetric tridiagonal matrix returning
% two vectors u,v
%         
%
%       [u,v]=INV_SYM_TRID(subd,d)
%       subd contains the subdiagonal of the tridiagonal T
%       d contains the diagonal of the tridiagonal T
%       u,v are the generators of the unsymmetric semiseparable matrix
%       The inverse S of T:
%       S= tril(u.'*v)+triu(v.'*u,1);
%
%       This algorithm is described in (and the references therein):
%       Vandebril R., Van Barel M. and Mastronardi N., 
%       ``Matrix Computations and Semiseparable Matrices I: 
%       Linear Systems'', xviii+575 pp., 
%       The Johns Hopkins University Press, Baltimore, 2007 
%       (ISBN-13: 978-0-8018-8714-7). 
%       http://www.press.jhu.edu/books/title pages/9085.html
%       ---> See Chapter 3, Section 3.1.3
%
%       Raf Vandebril
%       raf.vandebril - a - cs.kleuven.be
%       Revision Date: 1/9/2008


function  [u,v]=Inv_Sym_Trid(b,d) 
  
  n=length(d);
  
  
  % We remark that this routine is not tuned at all,
  % the determinant of the tridiagonal matrix T can be computed much more
  % efficiently than done here!!
  T=diag(d)+diag(b,1)+diag(b,-1);

  v(1)=(-1)/det(T)*prod(b(1:n-1));
  u(1)=(-1)*det(T(2:n,2:n))/prod(b(1:n-1));
  
  for i=2:n-1  
    v(i)=(-1)^i/det(T)*prod(b(i:n-1))*det(T(1:i-1,1:i-1));
    u(i)=(-1)^i*det(T(i+1:n,i+1:n))/prod(b(i:n-1));
  end
  
  v(n)=(-1)^n/det(T)*det(T(1:n-1,1:n-1));
  u(n)=(-1)^n;
  