% LTL   Computes the quasiseparable matrix L^-1 T L, 
%          In case T and S=LL^T are tridiagonal.
%
%       [D,G,v,vL]=LTL(dT,sdT,dS,sdS)
%       Computes the Givens-vector representation of a quasiseparable
%       matrix constructed as shown above. Also outputs the Givens-vector
%       representation of the inverse Cholesky factor, such that we can compute
%       the eigenvectors if desired.
%
%       
%       T,S are both symmetric tridiagonal matrices, having 
%       respectively dS,dT and sdS,sdT as diagonal/subdiagonals
%       D contains the diagonal of the quasiseparable matrix
%       G contain the Givens transformation of the representation of the
%       quasiseparable matrix and the upper triangular inverse cholesky
%       factor. The complete matrix G is necessary for the cholesky
%       factor. For the quasiseparable only from 2:n-1 is necessary.
%
%       
%       v contains the vector for constructing the quasiseparable matrix
%       G(:,2:n) Givens of the representation of the quasiseparable
%       vL contains the vector for constructing the inverse Cholesky factor
%       G contains the Givens of the representaiton of the inverse
%       Cholesky factor
%
%
%
%       Software of the MaSe - Group
%       mase-at-cs.kuleuven.ac.be
%       Revision Date: 12/10/2006


function [d,G,sd,v]=LTL(dT,sdT,dS,sdS)

  
  % Initializations
  n=length(dT);
  
  if (n==1)
    v=sqrt(dS);
    d=dT/dS;
    G=[];
    sd=[];
    
  elseif (n==2)
    [dL,sdL]=cholt(dS,sdS);
    hdL=dL;
    
    Giv=givens(hdL(2),sdL(1));
    G(1,1)=Giv(1,1);
    G(2,1)=Giv(2,1);
    %hdL(i-1)=Giv(1,1)*hdL(i-1); % to update the diagonal elements after
                                % a Givens has been performed.
    
    v=1./dL;
    v(1:n-1)=v(1:n-1)'./G(1,:);
    
    L=[G(1,1)*v(1), 0 ; G(2,1)*v(1), v(2)];
    T=[dT(1),sdT(1);sdT(1),dT(2)];
    
    LTL=L*T*L';
    
    sd(1)=LTL(2,1);
    d=diag(LTL);
    
    
  else
    
      
  % We start by simply computing the Cholesky decomposition of the matrix
  % S
  [dL,sdL]=cholt(dS,sdS);
  
  
  % Let us compute a sequence of Givens transformations to compute an LQ
  % factorization of the matrix L
  
  
  % Help variable which needs to be updated slightly.
  % In order to compute the correct Givens transformation
  hdL=dL;
  for i=n:-1:2
    Giv=givens(hdL(i),sdL(i-1));
    G(1,i-1)=Giv(1,1);
    G(2,i-1)=Giv(2,1);
    hdL(i-1)=Giv(1,1)*hdL(i-1); % to update the diagonal elements after
                                % a Givens has been performed.
  end
  
  
  % We will first construct the upper Hessenberg matrix T*inv(L')
  % We need the complete representation of this matrix to be able
  % to derive a fast method for determining the final representation
  
  
  % Let us first determine the representation of the matrix inv(Lchol)
  % The Givens are the ones stored in Gvector
  % The vector is the inverse of the diagonal in the cholesky factor
  v=1./dL;
  v(1:n-1)=v(1:n-1)'./G(1,:);
  
    
  % Let us compute the diagonal elements of UH
  duh(1)=dT(1)*v(1)*G(1,1);
  % 2 Operations
  for i=2:n-1
    duh(i)=sdT(i-1)*v(i-1)*G(2,i-1)*G(1,i)+dT(i)* v(i)*G(1,i);
    % 6 Operations * n-2
  end
  i=n;
  duh(i)=sdT(i-1)*v(i-1)*G(2,i-1)+dT(i)* v(i);
  % 4 Operations
  
   
  % Let us compute the generators of the upper triangular quasiseparable
  % part of the matrix UH, first we only compute the superdiagonal elements
  
  sduh(1)=dT(1)*v(1)*G(2,1)*G(1,2)+sdT(1)*v(2)*G(1,2);
  % 6 Operations
  for i=2:n-2
    sduh(i)=sdT(i-1)*v(i-1)*G(2,i-1)*G(2,i)*G(1,i+1);
    sduh(i)=sduh(i)+dT(i)*v(i)*G(2,i)*G(1,i+1);
    sduh(i)=sduh(i)+sdT(i)*v(i+1)*G(1,i+1);
    % 11 Operations * n-3
  end
  i=n-1;
  sduh(i)=sdT(i-1)*v(i-1)*G(2,i-1)*G(2,i);
  sduh(i)=sduh(i)+dT(i)*v(i)*G(2,i);
  sduh(i)=sduh(i)+sdT(i)*v(i+1);
  % 8 Operations

  
  % Update now the representation of the upper triangular part, which has
  % become now a strictly upper triangular part
  Guh=G(:,2:n-1);
  sduh(1:n-2)=sduh(1:n-2)./Guh(1,1:n-2);
  % n-2 Operations
  
  
  % The subdiagonal is necessary for computing the subdiagonal in the
  % full matrix, and hence necessary for generating the quasiseparable
  % matrix
  sbduh=sdT'.*v(1:n-1)'.*G(1,:);
  % 2*(n-1)
  
  
  %%%%%%% %%%%%%%%%%%% %%%%%%%%% %%%%%%%%%%% %%%%%%%%%% %%%%%%%
  % We have now computed the UH matrix in generator form correctly
  % Let us compute now the complet quasiseparable generators
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  % Let us start by trying to compute the diagonal elements of the
  % quasiseparable matrix
  
  d(1)=v(1)*G(1,1)*duh(1);
  temp=v(1)*G(2,1)*sduh(1);
  % 4 Operations
  
  for i=2:n-1
    d(i)=v(i)*G(1,i)*duh(i);  % Som van de twee diagonalen
    d(i)=d(i)+temp*G(1,i)*Guh(1,i-1);
    temp=temp*G(2,i)*Guh(2,i-1)+v(i)*G(2,i)*sduh(i);
    % 10 Operations * n-2
  end
  
    i=n;
    d(i)=v(i)*duh(i);  % Som van de twee diagonalen
    d(i)=d(i)+temp;
    % 2 Operations
     
  
  % The final computational effort is to compute the subdiagonal of this
  % newly constructed quasiseparable matrix
  
  sd(1)=v(1)*G(2,1)*G(1,2)*duh(1);
  sd(1)=sd(1)+sbduh(1)*v(2)*G(1,2);
  temp=v(1)*G(2,1)*G(2,2)*sduh(1);
  % 9 Operations
  
  for i=2:n-2
     sd(i)=v(i)*G(2,i)*G(1,i+1)*duh(i);
     sd(i)=sd(i)+sbduh(i)*v(i+1)*G(1,i+1);
     sd(i)=sd(i)+temp*G(1,i+1)*Guh(1,i-1);
     temp=temp*G(2,i+1)*Guh(2,i-1)+v(i)*G(2,i+1)*G(2,i)*sduh(i);
     % 15 Operations * n-3
  end
  
     i=n-1;
     sd(i)=v(i)*G(2,i)*duh(i);
     sd(i)=sd(i)+sbduh(i)*v(i+1);
     sd(i)=sd(i)+temp*Guh(1,i-1);
     % 6 Operations
  
     
   % Now we only construct the representation of this lower part
   %G=G(:,2:n-1);
   sd(1:n-2)=sd(1:n-2)./G(1,2:n-1);
   % n-2 Operations
  
  end
  
   
  % CHOLT   Computes the cholesky factorization of a tridiagonal matrix
%       
%
%       [d,sd]=cholt(dT,sdT)
%       Computes the cholesky factorization of the positive definite
%       tridiagonal matrix with dT as diagonal and sdT as subdiagonal
%
%       
%       The output are de diagonal and subdiagonal of the matrix L
%       such that LL* is the Cholesky decomposition of the tridiagonal
%       matrix.
%
%       Software of the MaSe - Group
%       mase-at-cs.kuleuven.ac.be
%       Revision Date: 29/09/2006


function [dT,sdT]=cholt(dT,sdT)

  % Initialize the length of the variables
  n=length(dT);
  
  % Compute the diagonal elements
 
  if (n==1)
    dT=sqrt(dT);
    sdT=[];
  else
    for j=2:n
      dT(j)=dT(j)-sdT(j-1)*(sdT(j-1)/dT(j-1));
      % 3 Operations * n-1
    end
  
    % Assign and scale the output
    sqdT=sqrt(dT);
    dT=dT./sqdT;
    sdT=sdT./sqdT(1:n-1);
  end
  
  