% DETHL   Calculates the determinant of a Hessenberg-like matrix
%       
%    D=DETHL(G,d,R)
%    Given a sequence of Givens transformations and a vector
%    the function calculates the determinant of the corresponding
%    Hessenberg-like 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)]
%
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 04/05/2004


function determ=DETHL(G,d,R)
 
% initializing the size of the matrix
  n=length(d);
  
  if n==0
    determ=[];
  elseif n==1
    determ=d(1);
  else
    
    % 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=R(i,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=R(n-1,n-1);
    newd=-G(2,n-1)*sd+G(1,n-1)*d(n);
    determ=determ*newd;
  end