% DETSS   Calculates the determinant of a semiseparable matrix
%       
%    D=DETSS(G,d)
%    Given a sequence of Givens transformations and a vector
%    the function calculates the determinant of the corresponding
%    symmetric semiseparable matrix
%
%    The givens and the vector have to be stored in the following way
%
%    G= | c(1)  c(2)  c(3) .. .. .. c(n-1) |
%       | s(1)  s(2)  s(3) .. .. .. s(n-1) |
%     
%    d= [d(1) d(2) .. ..  d(n)]
%
%
%    D=DETSS(G,D,G1,D1)
%    calculates the determinant of the unsymmetric semiseparable matrix S
%    which has the lower triangular part represented with G,D and the
%    strict upper triangular part represented with G1,D1.
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 16/12/2003


function determ=DETSS(G,d,G1,d1)
 
% initializing the size of the matrix
  n=length(d);
  
  if n==0
    determ=[];
  elseif n==1
    determ=d(1);
  else
        
    % if the matrix is symmetric we have to assign
    % G1 and d1
    if nargin==2
      for i=1:n-1
        d1(i)=G(2,i)*d(i);
      end
      G1=G(:,2:n-1);
    end
    
    % initializing the size of the matrix
    n=length(d);
    
    % because d(1) does not change by the operations
    % it is immediately included in the determinant
    determ=d(1);
    
    for i=1:n-2
      % Calculation of the super diagonal element
      % not stored in a vector but immediately multiplied
      sd=d1(i)*G1(1,i);
    
      % making the correct multiplication between
      % the diagonal element and the super diagonal element
      % in correspondance with the making of the upper triangulr
      % structure with the givens
      newd=-G(2,i)*sd+G(1,i)*d(i+1);
      
      % the element above is a diagonal element
      % from the R factor of the QR factorization, and
      % will immediately be multiplied to the determinant
      determ=determ*newd;
      
    end
    
    % n-1 is not included because G(1,n) does not exist
    sd=d1(n-1);
    newd=-G(2,n-1)*sd+G(1,n-1)*d(n);
    determ=determ*newd;
  end