% EIGSSD    Eigenvalues and eigenvectors of semiseparable plus diagonal matrices
%
%    E = EIGSSD(G,d,diag,cutoff) is a vector containing the eigenvalues of 
%    the symmetric semiseparable matrix S constructed with G,d and the
%    vector containing the diagonal elements diag.
%    ( check BSS CSS for info on the representation G,d)
%    remark that the semiseparable plus diagonal matrix has to be in unreduced form
%
%    [V,D] = EIGSSD(G,d,diag,cutoff) produces a diagonal matrix D of eigenvalues and a
%    full matrix V whose columns are the corresponding eigenvectors so
%    that (SS+H)*V = V*D. With SS the semiseparable matrix constructed
%    with G,d and H the diagonal matrix with the elements of diag on the diagonal.
%  
%    cutoff stands for the cutting off criterion if the subdiagonal
%    element is smaller than cutoff the eigenvalues is cutoff
%    Remark that cutoff is an optional argumant. If cutoff is not given,
%    standard criterion used is 10^(-14)
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 16/06/2004

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

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

if nargout==2;
  Gs=G;
  ds=d;
  dgs=dg;
end
eigorig=sort(eig(BSS(G,d)+diag(dg)));

eigvec=[];
[G,d,dg,eigvec]=CutEig(G,d,dg,eigvec,cutoff);

% the kappa equal to 1:Rayleigh
% the kappa equal to 2:Wilkinson
kappa=2;

% starting the solving
while length(eigvec) < n 
 [G,d]=QRSSSD(G,d,dg,kappa);
 [G,d,dg,eigvec]=CutEig(G,d,dg,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
    dgit=dgs-eigvec(i)*ones(1,n);
    for k=1:nmit;
      E(:,i)=QRsolv(Gs,ds,dgit,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 will cut off eigenvalues
% which are already converged in the first steps
% of the algorithm

function [G,d,dg,eigvec]=CutEig(G,d,dg,eigvec,cutoff)

n=length(d);
l=n-1;
if n==2
 B=[G(1,1)*d(1)+dg(1),G(2,1)*d(1);G(2,1)*d(1),d(2)+dg(2)];
 eigvec=[eigvec,eig(B)'];
else
  while (l>1) & ( abs(G(2,n-1)*d(n-1)) < cutoff*(abs(d(n))+abs(dg(n)) ))
    eigvec=[eigvec,d(n)+dg(n)];
    n=n-1;
    l=l-1;
    d=d(1:n);
    dg=dg(1:n);
    d(n)=G(1,n)*d(n);
    G=G(:,1:n-1);
  end
end

%-------------------------------------------------------------
% 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 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])';



%-------------------------------------------------------------
% The QR step will be performed in here
%
%-------------------------------------------------------------

% QRSSSD    Performing a QR step with shift on the semiseparable plus
%           diagonal matrix
%        
%        [G,d]=QRSSSD(G,d,diag,kappa) 
%        In this function one QR step will be performed on the
%        semiseparable matrix plus diagonal which will be given as input.
%        the input is given in the special matrix representation
%        used for semiseparable matrices namely Givens G and vector d
%        the diagonal will be stored in diag
%
%        kappa is the shift we will use
%        1 is the Rayleigh shift
%        2 is the wilkinson shift
%
%
%        Software of the MaSe - Group
%        mase-at-cs.kuleuven.ac.be
%        Revision Date: 15/06/2004



function [G,d]=QRSSSD(G,d,dg,x)


  % be careful when calculating the shift, as also the diagonal is
  % involved in this computation
  if (isempty(d))
    d=[];
    dg=[];
    G=[];
  elseif length(d)==1
    d=d;
    dg=dg;
    G=[];
  elseif length(d)>2
      
    % the size of the matrix
    n=length(d);
    
    % initializing the shift kappa,
    % depending on the choice given
    switch x
     case 1
      kappa=d(n)+dg(n);
     case 2
      B=[G(1,n-1)*d(n-1)+dg(n-1),G(2,n-1)*d(n-1);G(2,n-1)*d(n-1),d(n)+dg(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

    % We will first calculate the special Givens, this is more
    % easy than after Crep
    c=G(1,1);
    s=G(2,1);
    givspec=givens(c*(dg(1)-kappa)+d(1),-s*(dg(1)-kappa));


    % Perform now the first sequence of Givens transformations, 
    % we know that the resulting ss + diag matrix has the same diagonal
    % but shifted down one position
    [G,d]=CRep(G,d,dg);
    
    
    % Now the second sequence of Givens transformations will
    % be performed on the semiseparable matrix, thereby giving
    % as output immediately the new Givens Vector representation.
    [G,d]=PerSeqGiv(G,d,dg,givspec);


    % the following function will recreate the 
    % givens and the diagonal to remake
    % the semiseparable matrix
    
    %disp('new build representation')
    [G,d]=SR(G,d);
  end


%------------------------------------------------------------
% This function will perform the first sequence of transformations on the
% semiseparable plus diagonal matrix. 
%
% [G,d]=CRep(G,d,dg)


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

  % 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);  
  workdg=dg(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 of the semiseparable
    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]; 
    
    % updating now the diagonal representation with the part
    % coming from the Givens applied on the diagonal.
    dnew(n-i+1)=dnew(n-i+1)+c*(-dg(i-1)+workdg);

    % workdg stores the upper left element of the 2 by 2 block
    % on which we perform the operations
    workdg=c^2*dg(i-1)+s^2*workdg;
    
  end
  
  % updating the final element.
  dnew(n)=dnew(n)+workdg; 

%------------------------------------------------------------
% 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 [Gn,dn]=PerSeqGiv(G,d,dg,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;
Gn=[];
dn=[];
f1=1;
f2=1;
d(n)=d(n)-dg(1);

for i=2:n-1
  % This routine will perform each step a Givens transformation in order
  % to chase the disturbed structure down, this procedure is iterated and
  % the new representation appears in Gn,dn.
  [G,d,Gn,dn,f1,f2,giv]=RedLowSS(G,d,Gn,dn,f1,f2,giv,dg,i);
  %if i>2
  %  [Gt,dt]=SR(Gn,dn);
  %  BSS(Gt,dt)+diag(dg)
  %end
  %pause
end



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

% Gn and dn are the new Givens vector representations


function [G,d,Gn,dn,f1,f2,giv]=RedLowSS(G,d,Gn,dn,f1,f2,giv,dg,i);
  
  % 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
    
  % Short initialization of the Givens
  c1=G(1,n-1);
  s1=G(2,n-1);

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

  % Initialization of the elements in B, using the factors.
  x=d(n); %-dg(i-1);
  y=f2*s1*d(n-1);
  z=c1*d(n-1);

  % This factor needs to be stored to calculate the dependencies in
  % the new Givens vector representation.
  f1giv=f1*s1;

  
  % Plugging everyting in the matrix Bn
  % note that the elements in the final row, only denote the dependencies
  % as the element d(n-2) might be zero, this corresponds to the special 
  % Givens transformation
  Bn=[x y ; y z ; f2*s1  c1];
  
  % Performing the essential Givens transformations in order to calculate
  % the new representation of the upper part. Remark once more that f1
  % and f2 do not represent the real elements but just a factor to
  % calculate the dependencies.
  Bn(1:2,:)=giv*Bn(1:2,:);
  Bn=Bn*giv';
  f1=Bn(3,1);
  f2=Bn(3,2);

  % Based on the factors f1 f2 and the element d(n-2) we can determine
  % the specific type of transformation needed to perform the correct
  % step in the chasing procedure
  Bn(2,2)=Bn(2,2)+dg(i-1)-dg(i);
  giv=csg(Bn,d(n-2),s2,c2);%,dg,i);
  

  % 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)=Bn(2,2); 
  d=d(1:n-1);
  G=G(:,1:n-2);
  
 
  % Let us now store the new Givens and vector representation
  if length(dn)==0
    dn(n)=Bn(1,1);
  else
    [c,s,r]=Givens1(f1,f1giv);
    Gn(:,n)=[c;s];
    if c~=0
      f1=f1/c;
      dn(n)=Bn(1,1)/c;
    end
  end
  
  
  % if this iss the final loop, we calculate all the elements
  if length(d)==2
    
    % We work now with the real last block, so multiply with
    % the real diagonal element
    Bn(3,:)=Bn(3,:)*d(n-2)*s2;
    
   % Make the matrix square for the final transformation
    Bn(1,3)=Bn(3,1);  
    Bn(2,3)=Bn(3,2);
    Bn(3,3)=c2*d(n-2);
    
    % Perform the last transformation
    Bn(2:3,:)=giv*Bn(2:3,:);
    Bn(:,2:3)=Bn(:,2:3)*giv';
    
    % Adding the final elements to the representation of the new part 
    if isempty(Gn)
     [c,s,r]=Givens1(Bn(2,2),Bn(2,1));
    else
      [c,s,r]=Givens1(Bn(2,2),Bn(2,1)/Gn(1,3));
    end
    Gn(:,2)=[c;s];
    dn(2)=Bn(2,2)/c;
    Bn(3,2)=Bn(3,2)/c;
     
    % updating the element Bn(3,3) before calculating the representation
    Bn(3,3)=Bn(3,3)+dg(i)-dg(i+1);
    [c,s,r]=Givens1(Bn(3,3),Bn(3,2));
    Gn(:,1)=[c;s];
    if c~=0
     dn(1)=Bn(3,3)/c;
    else
      dn(1)=Bn(3,3);
    end
    
  end
  
% --------------------------------------------------------------------
% new method for calculating the special Givens transformation

 function [G]=csg(Bn,d3,s2,c2)
  % B denotes a 2 by 3 block, 
  % which is exactly the information
  % needed to calculate the givens
  
  % dit blokje bevat alle gegevens
  f1=Bn(3,1);f2=Bn(3,2);
  %Bn(2,2)=Bn(2,2)+dg(i-1)-dg(i);
  
 %XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
 % This is an important point in the transformations section
 % Here one can see when a diagonal element is considered zero.
 % The level is currently set to 10^(-14)
 %XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx
 
 
  %if ( abs(d3) < 10^(-14))
  %  disp('Special Givens desired')
  %else
    t=(f1*f2*d3*s2^2-Bn(2,1)*c2)/(Bn(2,1)*f2*s2-f1*s2*Bn(2,2));
  %end
  
  G=1/sqrt(1+t^2)*[t 1 ; -1 t]; 
  
  



%-----------------------------------------------
% needed functions for the main routine
%--------------------------------------

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
% SR Swap the representation of the semiseparable matrix
%        
%    [G,d]=SR(G,d)
%    Given a sequence of Givens transformations and a diagonal
%    the function will give the representation of the matrix in 
%    the other direction.
%    building direction will change from
%
%      |
%      |      to  a representation   <-----
%      V
%
%    or in the other directions.
%     
%
%    cosine and sine of the givens 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: 16/12/2003


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

  
  n=length(d);  
  
  if (n~=1 && n~=0)
    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;
  end