% SOLVHLD   solves the system Zx=b where Z is a Hessenberg-like matrix
%
%           X = SOLVHLD(G,D,R,diag,B) 
%           solves the system (A+diag*I)X=B
%           with G,D the givens diagonal representation of the lower part
%           of the Hessenberg-like matrix, and R represents the strictly
%           upper triangular part, diag is a vector containing the diagonal
%           elements. B is a column vector containing the right handside.
%
%
%           Software of the MaSe - Group
%           mase-at-cs.kuleuven.ac.be
%           Revision Date: 05/05/2004


function [x]=SOLVHLD(G,d,R,dg,b)

n=length(d);


% first we need to check the size of the involved
% matrix if the size <=2 we will not use the
% solver here but just a direct solver
% incorporated in matlab


if n==0
  x=[];
elseif n==1
  x=b/(d+dg);
elseif n==2
    x=[G(1,1)*d(1)+dg(1) , R ; G(2,1)* d(1), d(2)+dg(2)]\b;
else
  % We will add the diagonal to the strictly upper triangular part
  R=[zeros(n-1,1),R];
  R=[R;zeros(1,n)];
  R=R+diag(d);
  
  
  % In the first step the Hessenberg-like  matrix
  % will be reduced to upper Hessenberg form
  [R,b]=upphessb(G,d,R,b);
 
   
  % In the second part we will reduce the upperHessenberg
  % matrix to upper triangular form output
  [R,b]=upptrif(R,b);
 
  % In this final part we will solve the system with
  % backward substitution in order n
  [x]=bws(R,b);
  x=x';
  
end

%-------------------------------------------------------------------------

% in this routine the semiseparable matrix will be reduced
% to upper hessenberg form, the output is a rank 2 representation
% and also an extra diagonal


function [R,br]=upphessb(G,d,R,br)

n=length(d);

% perform the transformations to make the lower triangular part of upper
% triangular form at the same time on the upper triangular part of the
% Hessenberg-like plus diagonal matrix
 

% add the appropriate diagonal element to the upper triangular matrix
R(n,n)=R(n,n)+d(n);

for i=n:-1:2
    % Calculating the Givens transformation
    Giv=[G(1,i-1),G(2,i-1);-G(2,i-1),G(1,i-1)];
    
    % Perform now the Givens transformation to the upper triangular
    % matrix
    R(i-1:i,i-1:n)=Giv*R(i-1:i,i-1:n);
    
    % add the appropriate diagonal element to the upper triangular matrix
    R(i-1,i-1)=R(i-1,i-1)+d(i-1);
     
    % updating of the right hand side
    br(i-1:i)=Giv*br(i-1:i);
end;


%------------------------------------------------------------------
%
%  THE FUNCTION GIVENS1
%
%-------------------------------------------------------------------

function [c,s,r] = Givens1(x,y);
%
%  computes c,s,r such that
%
%  [c  s] [x] = [r]
%  [-s c] [y]   [0]
%
%  with c*c + s*s = 1;
%
   if (y == 0)
      c = 1; s = 0; r = x;
   else
      if (abs(x) >= abs(y))
         t = y/x; r = sqrt(1 + t*t);
         c = 1/r;
         s = t*c;
         r = x*r;
      else
         t = x/y; r = sqrt(1 + t*t);
         s = 1/r;
         c = t*s;
         r = y*r;
      end
   end

   
   
%------------------------------------------------------------------

% the upper hessenberg form will be reduced
% towards the upper triangular form by means
% of Givens transformations from top to bottem
% this adapting involves a lot of index shuffling


function [R,b]=upptrif(R,b)

% calculate the appropriate Givens transformation and perform it to the
% Hessenberg matrix and to the righthandside b

n=size(R,1);

for i=1:n-1
  
    % calculation of the givens which has to be performed
    [c,s,r]=Givens1(R(i,i),R(i+1,i));
    Giv=[c,s;-s,c];
        
    % applying the transformation on R
    R(i:i+1,i:n)=Giv*R(i:i+1,i:n);
       
    % applying the transformation to the righthandside
    b(i:i+1)=Giv*b(i:i+1);    
end



% Solve the system via traditional backward substitution

function [x]=bws(R,b);
 n=length(b);
 x(n)=b(n)/R(n,n);
  for i=n-1:-1:1
    x(i)=b(i)-R(i,i+1:n)*x(i+1:n)';
    x(i)=x(i)/R(i,i);
  end
  

