% EIGSS    Eigenvalues and eigenvectors of semiseparable matrices
%
%    E = EIGSS(G,d,cutoff) is a vector containing the eigenvalues of 
%    the symmetric semiseparable matrix S constructed with G,d
%    ( check BSS CSS for info on the representation G,d)
%    remark that the matrix has to be in unreduced form
%    (check REDSS for infor about unreducedness)
%
%
%    [V,D] = EIGSS(G,d,cutoff) produces a diagonal matrix D of eigenvalues and a
%    full matrix V whose columns are the corresponding eigenvectors so
%    that SS*V = V*D. With SS the semiseparable matrix constructed
%    with G,d
%  
%    cutoff stands for the cutting off criterion if the subdiagonal
%    element is smaller than cutoff the eigenvalues is cutoff
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 30/05/2003

function [V,D]=EIGSS(G,d,cutoff);

n=length(d);
if not(exist('cutoff'))
     cutoff=10^(-8);
end

if nargout==2;
  Gs=G;
  ds=d;
end


eigvec=[];
[G,d,eigvec]=CutEigStart(G,d,eigvec,cutoff);

% starting the solving
counter=1;
while length(eigvec)<n
counter=counter+1;
 [G,d,eigvec]=QRQ(G,d,2,eigvec,cutoff);
end

% when the number of output arguments is 2
% also the eigenvectors need to be calculated
if nargout==2
  
 % nmit stands for the number of iterations to be performed
 % while performing the iterative refinement
 % 3 should normally be enough
  nmit=3;
  
  % initializing the starting vectors for this procedure
  E=rand(n,n);
  
  for i=1:n
    for k=1:nmit;
      E(:,i)=QRsolv(Gs,ds,-eigvec(i)*ones(1,n),E(:,i));
      E(:,i)=E(:,i)/norm(E(:,i));
      F=mul(Gs,ds,E(:,i));
      eigvec(i)= E(:,i)'*F;
    end
  end  
  
  D=diag(eigvec);
  V=E;
  
else
  V=eigvec;
end



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

% This function constructs
% the semiseparable matrix from the input
% matrix S which is symmetric
% [G,d]=CSS(S)
% G represents a sequence of givens
% d represents a diagonal
% the output is a stable representation

function [G,d]=CSS(S)

n=size(S,1);

A=S ;
G=zeros(2,n-1);
d=zeros(1,n);

for j=n:-1:2
  [A,G,d]=red(A,G,d,j);
end

% final completion of the representation
% these values can easily be extracted
% from the remaining matrix A

%--------------------------------------------------------
% This is a reduction step of the matrix
% one row more will be made dependent.


function [A,G,d]=red(A,G,d,j)

r=size(A,1);

% disp('Making zeros')
% dit moet nog door householders
% effectief vervangen worden.

A=SymHouse(A,j,j-1);

[A,G,d]=upd(A,G,d,j);


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

% this function will perform
% a Symmetric householder operation
% on the matrix A the column which will be annihilated
% will be column k from element 1:l
% A=SymHouse(A,k,l)



function A=SymHouse(A,k,l)

  % implemented like in Golub and vanloan
  
 
  [v, beta] = gallery('house',flipud(A(1:l,k)));
  v=flipud(v);
  
  % multiplication left
  w=beta*A(1:l,:)'*v;
  A(1:l,:)=A(1:l,:)-v*w'; 

  % multiplication right
  w=beta*A(:,1:l)*v;
  A(:,1:l)=A(:,1:l)-w*v'; 

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

% block the elements in these final rows and columns
% are zeros.
% Also a serie of givens and diagonal elements
% to calculate the remaining semiseparable part of this.
% matrix is given
% this function has the following form, and will
% update the representation.
% will reduce 1 in dimension and G and d will grow one.
% [A,G,d]=updrep(A,G,d);


function [A,G,d]=upd(A,G,d,j);

  % complete size of the starting matrix equals n
  n1=size(A,1);
  n=length(d);

  
  % for the first loop to be completed
  % in a perfect way: Somethings have to
  % be initialized

  d(j-1)=A(n1-1,n1-1);
  VAL=A(n1,n1-1);

  if j==n
   d(j)=A(n1,n1);
  end
  

  for i=j:n
    a=d(i-1);
    
    if i~=n    
      b=G(1,i)*VAL;
      c=G(1,i)*d(i);
     else
       b=VAL;
       c=d(i);
    end; 

    % The matrix G calculated below
    % is the matrix for which
    % [b,c]*G=[0,*];

    giv=givens(c,b);
    cs=giv(1,1);  %cs comes from cos
    sn=-giv(1,2); %sn comes from sin
  

    d(i)=(sn^2*a-2*sn*cs*b+cs^2*c);
    G(:,i-1)=[cs;-sn];

    % these calculations changed d(i-1)
    d(i-1)=(cs*a+sn*b);
     
    % the following value is very important
    % for the next loop

    if i~=n
      VAL=-sn*b+cs*c;
      VAL=G(2,i)*VAL/G(1,i); 
    end

  end
 
  A(n1-1,n1-1)=d(j-1);
  A=A(1:n1-1,1:n1-1);
 


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

% this function will cut off eigenvalues
% which are already converged in the first steps
% of the algorithm

function [G,d,eigvec]=CutEigStart(G,d,eigvec,cutoff)

n=length(d);
l=n-1;

while (l>1) & (abs(G(2,n-1)*d(n-1))<cutoff*abs(d(n)))
 %disp('ConstSS already converged')
 eigvec=[eigvec,d(n)];
 n=n-1;
 l=l-1;
 d=d(1:n);
 d(n)=G(1,n)*d(n);
 G=G(:,1:n-1);
end

%-----------------------------------------------------
% this function has the diagonal
% and the subdiagonal of the
% semiseparable matrix
% this will be used
% to cut off the eigenvalues and to change
% the representation
% for changing the cutting createrium
% just have to change this file


function [d2,sd2,eigvec]=CutEig(d2,sd2,eigvec,cutoff)

n=length(d2);
l=n-1;

while (l>1) & (abs(sd2(l))<cutoff*abs(d2(n)))
 eigvec=[eigvec,d2(n)];
 n=n-1;
 l=l-1;
 d2=d2(1:n);
 sd2=sd2(1:n-1);
end

% the last two eigenvalues
% are calculated in a regular way

if (l<2)
  if (abs(sd2(1))<cutoff)
   eigvec=[eigvec,d2];
  end
end

%-------------------------------------------------------------
% In this function one QR step will be performed on the
% semiseparable matrix which will be given as input.
% the input is given in the special matrix representation
% used for semiseparable matrices
% following function:
%
% [G,d]=QRQ(G,d,kappa)
% G is the sequence of givens
% d is the diagonal
% kappa is the shift we will use
% for kappa there are several options
% 1 is the last element
% 2 is the largest eigenvalue of the last 2 by 2 block
% 2 is the wilkinson shift 


function [G,d,eigv]=QRQ(G,d,x,eigv,cutoff)

% the size of the matrix
n=length(d);

% initializing the shift kappa,
% depending on the choice given
switch x
  case 1
    kappa=d(n);
  case 2
    B=[G(1,n-1)*d(n-1),G(2,n-1)*d(n-1);G(2,n-1)*d(n-1),d(n)]; 
    le=eig(B);
    if abs(le(1)-B(2,2))<  abs(le(2)-B(2,2)),                       
       kappa =le(1);
    else
       kappa=le(2);
    end
end



% first we will rearrange the representation.
% now the representing of SS starts from
% top to bottem, the first step of the
% algorithm requires to walk through
% the matrix and change the representation
% from bottem to top.
[G,d]=CRep(G,d);




% now we will calculate the special givens
% which will determine the rest of the 
% process 
% dex is the element coming from the 
% semiseparable matrix
c=G(1,n-1);
s=G(2,n-1);
dex=-s^2*d(n-1)+c*d(n);
givspec=givens(-c*kappa+dex ,s*kappa);


% now we have the starting givens and we have
% to run through the complete matrix
% to update the semiseparable matrix
% by using these special givens transformations
% this is done in the following
% function 

[d2,sd2]=PerSeqGiv(G,d,givspec);




% The representation is now in the form diagonal
% subdiagonal and will be checked for converged eigenvalues.
% they will be added to eigvec
[d2,sd2,eigv]=CutEig(d2,sd2,eigv,cutoff);



% the following function will recreate the 
% givens and the diagonal to remake
% the semiseparable matrix

[G,d]=ConsRep(d2,sd2);

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

% this function will construct the representation
% of the semiseparable matrix given the input
% of the diagonal and the subdiagonal
% this is necessary for the reconstruction
% of the representation of the semiseparable

function [G,d]=ConsRep(d2,sd2)

n=length(d2);
d(n)=d2(n);

for i=n-1:-1:2
  giv=givens(d2(i),sd2(i));
  %d2(i),sd2(i)
  G(:,i)=[giv(1,1); -giv(2,1)];
  d(i)=giv(1,1)*d2(i)+giv(1,2)*sd2(i);
  giv*[d(i);0];
  sd2(i-1)=sd2(i-1)/giv(1,1);
  %M=BSS(G,d);
  %M
  %pause
end
 
i=1;
giv=givens(d2(i),sd2(i));
G(:,i)=[giv(1,1); -giv(2,1)];
d(i)=giv(1,1)*d2(i)+giv(1,2)*sd2(i);


%-------------------------------------------------
% this function will perform
% the disturbing givens transformations
% onto the semiseparable matrix
% which is constructed by G and d
% the matrix will not at all be completely
% semiseparable and with a special chase
% constructed of givens transformations
% through the semiseparable matrix
% this will be corrected.

function [d2,sd2]=PerSeqGiv(G,d,giv)

n=length(d);

% because the application of the
% sequential givens transformations,
% divide the matrix into two semiseparable
% parts we have the following functions
% which will reduce the lower block
% and give enough output to calculate the
% special givens transformation
% the change in representation of the lower
% block is denoted by the variable factor

factor=1;

for i=2:n-1
  [G,d,factor,B]=RedLowSS(G,d,factor,giv);

  % B is a 3 by 3 blockneeded to calculate the following
  % special givens transformation, and needed to rebuild
  % the new semiseparable matrix
  giv=CalcSpecGiv(B(2:3,:));

  % To rebuild the complete semiseparable matrix
  % we need the diagonal and subdiagonal,
  % here we will store them in two vectors
  d2(i-1)=B(1,1);
  sd2(i-1)=giv(1,1)*B(2,1)+giv(1,2)*B(3,1);
end

   % this is not necessary anymore: captured completely below
   % T=flipud(fliplr(BSS(G,d)));
   % T(1,2)=factor*T(1,2);
   % T(2,1)=T(1,2)
   % a=G(1,1);
   % b=G(2,1);
   % [d(2), factor*b*d(1) ;factor*b*d(1), a*d(1)] 
 
   % T=giv*T*giv';
   % d2(n-1)=T(1,1);
   % d2(n)=T(2,2)
   % sd2(n-1)=T(2,1)

% updating van de laatste gegevens: namelijk de diagonaal  
c=giv(1,1);
s=giv(2,1);
a=G(1,1);
b=G(2,1);  
  
d2(n-1)=c^2*d(2)-2*s*c*factor*b*d(1)+s^2*a*d(1);
d2(n)=s^2*d(2)+2*c*s*factor*b*d(1)+c^2*a*d(1);
sd2(n-1)=c*s*(d(2)-a*d(1))+(c^2-s^2)*factor*b*d(1);

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

% we will reduce the representation of the
% lower triangular semiseparable matrix
% because one givens transformation is performed
% which actually reduces this representation

% some ouput is given 
% factor for the correct calculation of the new
% lower triangular semiseparable block

% element to calculate the following special givens
% transformation


function [G,d,factor,B]=RedLowSS(G,d,factor,giv);
  
  % initialization of the dimension
  n=length(d);

  % because there are large connections between the
  % elements in semiseparable matrices
  % a lot of elements have to be initialized
   
  c1=G(1,n-1);
  s1=G(2,n-1);

  c2=G(1,n-2);
  s2=G(2,n-2);

  x=d(n);
  y=s1*d(n-1)*factor;
  z=c1*d(n-1);
 
  u=s2*d(n-2);
  F1=u;        % this is very important for the reconstruction afterwards
 
  v=s1*u*factor;
  u=c1*u;
  dex=c2*d(n-2);
  B=[x y ; y z ; v u ];
 
  % end of the initialization, now the givens will be applied
  % to the matrix.
  B(1:2,:)=giv*B(1:2,:);
  B=B*giv';

 
  F2=B(3,2);    % F2 this gives us the remaining information for the
  factor=F2/F1; % reconstruction afterwards
  
  % we have now all the information
  % to calculate the special givens
  % this will be stored in B
  % and piped to the output
  
  B(1,3)=B(3,1);  
  B(2,3)=B(3,2);
  B(3,3)=dex;


  % everything is done except the change in the representation
  % of the lower triangular semiseparable matrix
  % we will cut off elements and change some
  d(n-1)=B(2,2);
  d=d(1:n-1);
  G=G(:,1:n-2);

%---------------------------------------------------------------
% This function will change the representation
% of the semiseparable matrix from top to bottem
% to the representation which will start
% to build from the bottem to the top
%
% [G,d]=CRep(G,d)

function [Gnew,dnew]=CRep(G,d)

  % the new representation 
  % will be stored in Gnew and
  % dnew
  % this can clearly be seen as an
  % easy O(n) algorithm 


  n=length(d);
  
  % initialization
  dnew(1)=d(n);  

  for i=n:-1:2
    % initial parameter settings
    c=G(1,i-1);
    s=G(2,i-1);
    dwork=dnew(n-i+1);

    % updating the diagonal
    dnew(n-i+1)=-s^2*d(i-1)+c*dwork;
    dnew(n-i+2)=s*(c*s*d(i-1)+s*dwork)+c*d(i-1); 

    % updating the givens
    Gnew(1:2,n-i+1)=[c ; s]; 
  end
%---------------------------------------------------

% this function will get input
% and calculate a specific givens
% from it.
% this is exactly the givens needed
% for the QR algorithm

function [G]=CalcSpecGiv(B);

% B denotes a 2 by 3 block, 
% which is exactly the information
% needed to calculate the givens

% dit blokje bevat alle gegevens

a=B(1,1);
b=B(1,2);
e=B(1,3);

d=B(2,1);
f=B(2,3);
t=(-1)*(a*f-d*e)/(a*e-d*b);

G=1/sqrt(1+t^2)*[t 1 ; -1 t];




% QRSOLVER    solves a system vi QR of semiseparable matrices
%
%    X = QRSOLVER(G,D,diag,B) 
%    solves the system (A+diag*I)X=B
%    with G,D the givens diagonal representation of the matrix
%    A (THIS IS THE SYMMETRIC SOLVER), diag is a vector representing
%    the shift which will be substracted from A.
%
%    To construct the G,d, representation of semiseparable matrices
%    take a look at CSS


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

n=length(d);

% 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

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

[G1,d1,G2,d2,dg,b]=upptrif(G1,d1,G2,d2,sd,b);

% In this final part we will solve the system with
% backward substitution in order n

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


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

% 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(G,d,dg,br)

n=length(d);

% REMARK
% this function is made for symmetric semiseparable,
% for non-symmetric the adaptation is quite easy, 
% because here the lower and upper part are split up.

% ADAPTING FOR EASINESS OF IMPLEMENTATION
% INTO  A LOWER AND UPPER TRIANGULAR PART
%----------------------------------------------------
G1=G;
d1=d;
for i=1:n-2
    d2(i)=d(i)*G(2,i);
end
d2(n-1)=d(n-1)*G(2,n-1);
G2=G(:,2:n-1);

clear G,d;
%------------------------------------------------------



% 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;

        
for i=n:-1:3
    % | 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)*d2(i-2);
        a=G2(1,i-2)*d2(i-2);
    else
        d2(i)=d2(i)/G2(1,i-1);
        b=G2(2,i-2)*G2(1,i-1)*d2(i-2);
        a=G2(1,i-2)*d2(i-2);
    end       
    
    % calculating the element which has to 
    % be added under a, this is x for the next loop
    x=a*c/b;
    
    % 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;   
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(2)=d2(2)/G2(1,1);
d2(1)=(G1(1,1)*c+G1(2,1)*x)/G2(1,1);

% 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]=CRep1(G1,d1);



%-----------------------------------------------------------------
%-----------------------------------------------------------------
%
% OTHER  FUNCTIONS USED IN THE PROGRAMM
%
%------------------------------------------------------------------
%------------------------------------------------------------------

% This function will change the representation
% of the semiseparable matrix from top to bottem
% to the representation which will start
% to build from the bottem to the top
%
% [G,d]=CRep(G,d)

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

n=length(d);


% constructing the subdiagonal of the representation
for i=1:n-2
    sd(i)=d(i)*G(2,i)*G(1,i+1);
end
sd(n-1)=d(n-1)*G(2,n-1);


% constructing the diagonal of the representation
for i=1:n-1
    d(i)=d(i)*G(1,i);    
end


% we now have a diagonal and subdiagonal
% we will swap this into the other representation
d=fliplr(d);
sd=fliplr(sd);

[G,d]=ConsRep1(d,sd);




%----------------------------------------------
function [G,d]=ConsRep1(d,sd)

n=length(d);
d(n)=d(n);
for i=n-1:-1:2
  [c,s,r]=Givens1(d(i),sd(i));
  G(:,i)=[c;s];
  d(i)=r;
  sd(i-1)=sd(i-1)/c;
end
 
[c,s,r]=Givens1(d(1),sd(1));
G(:,1)=[c;s];
d(1)=r;

%------------------------------------------------------------------
%
%  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=G1(1,i)*d1(i);
x2=G2(1,i)*d2(i);

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

z1=G1(1,i+1)*d1(i+1);
z2=G2(1,i+1)*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+x2,sd(i));
    d(i)=r;
    
    % The diagonal elements of the new representation
    % can be calculated easily now
    d1(i)=(c*y1+s*z1)/G1(1,i+1);
    d2(i)=(c*y2+s*z2)/G2(1,i+1);
    
    % 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(1,i+1)*G1(2,i+1)*G1(1,i+2);
        y2=x2/G2(1,i+1)*G2(2,i+1)*G2(1,i+2);
        z1=G1(1,i+2)*d1(i+2);
        z2=G2(1,i+2)*d2(i+2);
    else
        y1=x1/G1(1,i+1)*G1(2,i+1);
        y2=x2/G2(1,i+1)*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+x2,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);
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';

%------------------------------------
%
% function to perform a quick O(n)
% multiplication of a SS and a vector.

function b=mul(G,d,x)

n=length(d);

b(1)=x(1)*d(1);
for i=1:n-1
    wv=b(i);
    b(i)=G(1,i)*wv;
    b(i+1)=G(2,i)*wv+x(i+1)*d(i+1);    
end

b2(n-1)=G(2,n-1)*x(n);
for i=n-2:-1:1
    b2(i)=(b2(i+1)+G(1,i+1)*x(i+1))*G(2,i);
    b2(i+1)=b2(i+1)*d(i+1);
end
b2(1)=b2(1)*d(1);

b=(b+[b2,0])';
