% SOLVSSD   solves the system Sx=b where S is a semiseparable plus
%           diagonal matrix 
%
%           X = SOLVSSD(G,D,diag,B) 
%           solves the system (A+diag*I)X=B
%           with G,D the givens diagonal representation of the symmetric
%           semiseparable matrix S and diag a vector containing the diagonal
%           elements. B is a column vector containing the right handside.
%
%
%           X = SOLVSSD(G,D,G1,D1,diag,B) 
%           solves the system (A+diag*I)X=B
%           where A is now an unsymmetric semiseparable matrix.
%           G,D represents the givens diagonal representation of the lower
%           triangular semiseparable part of A, and G1,D1 represents the givens
%           diagonal representation of the strictly upper triangular
%           semiseparable part of the matrix A.
%
%
%
%           Software of the MaSe - Group
%           mase-at-cs.kuleuven.ac.be
%           Revision Date: 05/05/2004


function [x]=SOLVSSD(G,d,G1,d1,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 nargin==4
  b=d1;
  dg=G1;
  d1=[];
  G1=[];
end


if n==0
  x=[];
elseif n==1
  x=b/(d+dg);
elseif n==2
  if nargin==4
    x=[G(1,1)*d(1)+dg(1) , G(2,1)*d(1) ; G(2,1)* d(1), d(2)+dg(2)]\b;
    else
      x=[G(1,1)*d(1)+dg(1) , d1(1) ; G(2,1)* d(1), d(2)+dg(2)]\b;
  end
else
  if nargin==4
  % first we construct the representation of the upper triangular part
   for i=1:n-1
        d1(i)=G(2,i)*d(i);
      end
      G1=G(:,2:n-1);
  end
    
  % In the first step the semiseparable matrix
  % will be reduced to upper Hessenberg form
  % the upper triangular part is of
  % rank2, therefor the ouput consists of
  % two Givens diagonal sequences and one subdiagonal row
  % on b the sequence of Givens is applied
  
  [G,d,G1,d1,sd,b]=upphessb(G,d,G1,d1,dg,b);
   
  % In the second part we will reduce the upperHessenberg
  % matrix to upper triangular form output
  % will again be two sequences of transfromations
  % and a diagonal
  
  [G,d,G1,d1,dg,b]=upptrif(G,d,G1,d1,sd,b);
   
  % In this final part we will solve the system with
  % backward substitution in order n

  [x]=bws(G,d,G1,d1,dg,b);
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 [G1,d1,G2,d2,sd,br]=upphessb(G1,d1,G2,d2,dg,br)

n=length(d1);

% be careful, because there is a lot of flipping
% in the representation in volved. In fact all the Givens
% from the representation of the lower part, can be kept
% only be careful, in reality they are swapped.



%-------------------------------------------------------
% The first loop is for the lower triangular part
% and for the diagonal 
%-------------------------------------------------------


for i=n:-1:2
    % calculating the subdiagonal elements
    % of the upper Hessenberg form
    sd(i-1)=dg(i-1)*(-G1(2,i-1));
    
    % calculating the diagonal elements coming
    % from the added diagonal dg and adding them to
    % the new representation of G1 d1
    d1(i-1)=d1(i-1)+dg(i-1)*G1(1,i-1);
    
    % updating of the right hand side
    br(i-1:i)=[G1(1,i-1),G1(2,i-1);-G1(2,i-1),G1(1,i-1)]*br(i-1:i);
end;

% all the updatings of the first diagonal d1
% are done except for the nth component
d1(n)=d1(n)+dg(n);

% NOTICE THAT THIS REPRESENTATION IS NOT YET IN THE
% THE DIRECTION FROM LEFT TO RIGHT, THIS WILL BE
% DONE IN THE FINAL PART OF THIS FUNCTION




%-----------------------------------------------
% the second loop is for the strict
% upper triangular part
%-----------------------------------------------

% initialization process before we can start with the 
% loop
x=0;
c=d2(n-1);

% the new element x can already be extracted from
% the new diagonal representation of d1
%d1(n)=d1(n)-x;
a=0;
b=0;

        
for i=n:-1:3
    A=[a,b;0,c;0,x];
    % | a b |
    % |   c | 
    % |   x |
    %
    % is given 
    % the givens is performed on [c x ]^T
    % and then an element under a is added
    % to make everything again of rank 1
   
    % calculating the new diagonal elements
    % of the representation G2 d2
    % if i not equal to n still a division
    % is needed, check in the loop
    d2(i)=-G1(2,i-1)*c+G1(1,i-1)*x;
        
    % updating the elements for the next step
    c=G1(1,i-1)*c+G1(2,i-1)*x;
 
    if i==n
        b=G2(2,i-2);
        a=G2(1,i-2);
    else
        if G2(1,i-1)~=0
          d2(i)=d2(i)/G2(1,i-1);
        end
       
        b=G2(2,i-2)*G2(1,i-1);
        a=G2(1,i-2);
    end       
    
    % calculating the element which has to 
    % be added under a, this is x for the next loop
    if b==0
      x=0;
    else
      x=a*c/b;
    end
    
    % this element is added in the semiseparable structure
    % it therefor has to be substracted from the other representation
    d1(i-1)=d1(i-1)-x;
    
    % storing c for the next loop
    c=a*d2(i-2);   
end

% still one more step of updating has to be completed

% updating the new diagonal elements of d2
d2(2)=-G1(2,1)*c+G1(1,1)*x;
d2(1)=(G1(1,1)*c+G1(2,1)*x);
if G2(1,1)~=0
  d2(2)=d2(2)/G2(1,1);
  d2(1)=d2(1)/G2(1,1);
end



% constructing the new G2
G2=[[0;1],G2];


%------------------------------------------------------------
% 
% this is the final part we will now swap the representation
% of G1 and d1 to the representation from left to right
%
%----------------------------------------------------------

G1=fliplr(G1);
d1=fliplr(d1);
[G1,d1]=sr(G1,d1);


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


function [G,d]=sr(G,d)

n=length(d);  
dnew(n)=G(1,1)*d(1);
r=d(1);

for i=1:n-2
    [c,s,r]=Givens1(G(2,i)*r,d(i+1));
    Gnew(:,n-i)=[s;c];
    dnew(n-i)=G(1,i+1)*r;
end

i=n-1;
[c,s,r]=Givens1(G(2,n-1)*r,d(n));
Gnew(:,1)=[s;c];
dnew(1)=r;

G=Gnew;
d=dnew;

%------------------------------------------------------------------
%
%  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 [G1,d1,G2,d2,d,b]=upptrif(G1,d1,G2,d2,sd,b)

% for each of the semiseparable matrices
% the working part just above the diagonal is denoted as
% |x y|
% |  z|

% after each givens transformation this will be adapted
% such that we have the correct numbers for the next step
% in the loop

n=length(d1);

% creating the first 2 by 2 block for the transformations
% this is the initialization of the loop

i=1;
x1=d1(i);
x2=d2(i);

y1=G1(2,i)*d1(i);
y2=G2(2,i)*d2(i);

z1=d1(i+1);
z2=d2(i+1);

% starting the loop in which the new
% diagonal d will be calculated
% and the new d1 and d2 for the representations

for i=1:n-2
   
    % calculation of the givens which has to be performed
    % the new diagonal element can be stored immediately
    [c,s,r]=Givens1(x1*G1(1,i)+x2*G2(1,i),sd(i));
    d(i)=r;
    
    % The diagonal elements of the new representation
    % can be calculated easily now
    d1(i)=(c*y1+s*z1); 
    d2(i)=(c*y2+s*z2); 
    
    % now we will start calculating the elements
    % x1 x2 y1 y2 for the next loop
    
    % The elements x1, x2 for the next loop can be calculated
    % easily
    x1=-s*y1+c*z1;
    x2=-s*y2+c*z2;

    
    % a little bit attention has to be paid
    % when i+2 equals n, the calculation has
    % to change a little bit in the calculation 
    % of y1,y2 z1, z2
    
    if (i+2)~=n
        y1=x1*G1(2,i+1);
        y2=x2*G2(2,i+1);
        z1=d1(i+2);
        z2=d2(i+2);
    else
        y1=x1*G1(2,i+1);
        y2=x2*G2(2,i+1);
        z1=d1(i+2);
        z2=d2(i+2);
    end
    
    
    % performing the givens operation on the vector
    % b
    b(i:i+1)=[c,s;-s,c]*b(i:i+1);
    
end

% The final Givens transformation still has to be performed
% and the new diagonal element still has to be calculated
i=n-1;
[c,s,r]=Givens1(x1*G1(1,i)+x2*G2(1,i),sd(i));
d(i)=r;

% storing the new diagonals for the representation of the
% rank 2 part
d1(i)=(c*y1+s*z1);
d2(i)=(c*y2+s*z2);

% adapting the righthandside with the new givens
b(i:i+1)=[c,s;-s,c]*b(i:i+1);

% The final new diagonal element still has
% to be calculated
d(n)=(-s*(y2+y1)+c*(z2+z1));


% The new representation of the strict upper triangular
% part has the same givens of the previous ones, except
% the first one.
G1=G1(:,2:n-1);
G2=G2(:,2:n-1);
d1=d1(1:n-1);
d2=d2(1:n-1);



% Solve the system via backward substitution

function [x]=bws(G1,d1,G2,d2,d,b);

% the system via backward substitution can be solved
% by exploiting the structure of the semiseparable matrices
% this structure exploiting results in the updating
% of the factor elements, always easy to update.
% with 2 multiplications and one adding


n=length(d);
sing=false;
alsing=false;

for i=1:n
  if d(i)==0;
    sing=true;
  elseif abs(d(i))<10^(-14)
    alsing=true;
  end
end

if sing==true
  disp('SINGULAR MATRIX: UNSOLVABLE')
else
  if alsing==true
    disp('ALMOST SINGULAR MATRIX: CAN GIVE PROBLEMS')
  end
  
  x(n)=b(n)/d(n);
  fact1=x(n);
  fact2=x(n);
  
  for i=n-1:-1:2
    % the backward substitution step
    x(i)=b(i)-(fact1*d1(i))-(fact2*d2(i));
    x(i)=x(i)/d(i);
    
    % the updating of the two factors for
    % the next step
    fact1=fact1*G1(2,i-1)+G1(1,i-1)*x(i);
    fact2=fact2*G2(2,i-1)+G2(1,i-1)*x(i);
  end
  
  % the backward substitution step
  x(1)=b(1)-(fact1*d1(1))-(fact2*d2(1));
  x(1)=x(1)/d(1);
  
  % loop finished no updating needed anymore
  
  x=x';
end
