% SSEIG    Eigenvalues and eigenvectors. Via semiseparable matrices.
%
%    E = SSEIG(X) is a vector containing the eigenvalues of a square 
%    matrix X. (X has to be symmetric)
% 
%    [V,D] = SSEIG(X) produces a diagonal matrix D of eigenvalues and a
%    full matrix V whose columns are the corresponding eigenvectors so
%    that X*V = V*D.
%
%
%    The routine is implemented in such a way that the intermediate
%    matrices are semiseparable matrices. As described in the paper about
%    the implicit QR.
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 30/05/2003


function [V,D]=SSEIG(S)
  
  if not(exist('cutoff'))
     cutoff=10^(-8);
  end
  
  if nargout==0 | nargout==1
    n=size(S,1);
    % generating the givens and diagonal
    % of the representation
    [G,d]=CSS(S);
    
    % before starting the QR iteration
    % we have to be sure that we cut off
    % eigenvalues which are already converged
    % other eigenvalues are cut off
    % in the routine CutEig.m
    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
    
    V=eigvec;
        
  else
    n=size(S,1);
    Q=eye(n);
    % generating the givens and diagonal
    % of the representation
    [G,d,Q]=CSSQ(S,Q);
    
    % before starting the QR iteration
    % we have to be sure that we cut off
    % eigenvalues which are already converged
    % other eigenvalues are cut off
    % in the routine CutEig.m
    eigvec=[];
    [G,d,eigvec]=CutEigStart(G,d,eigvec,cutoff);
    
    % starting the solving
    counter=1;
    while length(eigvec)<n
      counter=counter+1;
      [G,d,Q,eigvec]=QRQQ(G,d,Q,2,eigvec,cutoff);
    end
    eigvec=fliplr(eigvec);
    V=Q';
    D=diag(eigvec);
  end
  
  
  %------------------------------------------------------------------
  %  
  % HERE WILL APPEAR THE DIFFERENT ROUTINES FOR SOLVING THE TWO
  % DIFFERENT PROBLEMS, THE PROBLEMS ARE COMPLETELY SPLIT UP
  % OTHERWISE THERE WOULD BE TO MUCH OVERHEAD IN TESTING WITH NARGOUT
  %
  %------------------------------------------------------------------
  
  
  
  %-------------------------------------------------------------------
  %
  % FUNCTIONS USED FOR THE QR WITH THE CALCULATION OF THE EIGENVECTORS
  %
  %-------------------------------------------------------------------
  
% 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,Q]=CSSQ(S,Q)

n=size(S,1);

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

for j=n:-1:2
  [A,G,d,Q]=redQ(A,G,d,j,Q);
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,Q]=redQ(A,G,d,j,Q)

r=size(A,1);

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

[A,Q]=SymHouseQ(A,j,j-1,Q);

[A,G,d,Q]=updQ(A,G,d,j,Q);


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

% 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,Q]=SymHouseQ(A,k,l,Q)

   % Instead of applying a symmetric householder
  % a loop of Givens is performed, for
  % numerical unstable problems, this is the best
  % option

  % implemented like in Golub and vanloan
  n=size(A,1);
  
  for i=1:n-2
   [A,Q]=SymGiv2(A,i,i+1,n,Q);
   %Q*Ao*Q'
   %A
   %pause
  end


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

function [M,Q]=SymGiv2(M,Row,Row2,Column,Q)

% on M we perform the symmetric operation
% on Q the operations on the left
% this to calculate the eigenvalues 
% in the factorization

G=givens(M(Row2,Column),M(Row,Column));

Mtransf=[M(Row2,:) ; M(Row,:) ];

Mtransf=G*Mtransf;
%Mtransf(2,1)=0;

M(Row2,:)=Mtransf(1,:);
M(Row,:)=Mtransf(2,:);
M;

% Calculation of Q
Qtransf=[Q(Row2,:) ; Q(Row,:)];
Qtransf=G*Qtransf;
Q(Row2,:)=Qtransf(1,:);
Q(Row,:)=Qtransf(2,:);

Column=Row2;
Column2=Row;

clear Mtransf

Mtransf= [ M(:,Column) M(:,Column2)];

Mtransf=Mtransf*G';

M(:,Column)=Mtransf(:,1);
M(:,Column2)=Mtransf(:,2);

M;

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

% 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,Q]=updQ(A,G,d,j,Q);

  % 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-1
    a=d(i-1); 
    % The matrix G calculated below
    % is the matrix for which
    % [b,c]*G=[0,*];
    [ct,st,rt]=Givens1(d(i),VAL);
    Qt=[Q(i-1,:);Q(i,:)];
    Qt=[ct, -st; st, ct]*Qt;
    Q(i-1,:)=Qt(1,:);
    Q(i,:)=Qt(2,:);
    
    d(i)=(ct*G(1,i)*(rt+st*VAL)+st^2*a);
    G(:,i-1)=[ct;st];
    d(i-1)=(ct*a-st*VAL*G(1,i));
    VAL=rt*G(2,i);
  end

  a=d(n-1);
  % The matrix G calculated below
  % is the matrix for which
  % [b,c]*G=[0,*];
  [ct,st,rt]=Givens1(d(n),VAL);
    Qt=[Q(n-1,:);Q(n,:)];
    Qt=[ct, -st; st, ct]*Qt;
    %Qt,ct,st
    %pause
    Q(n-1,:)=Qt(1,:);
    Q(n,:)=Qt(2,:);
  d(n)=(ct*(rt+st*VAL)+st^2*a);
  G(:,n-1)=[ct;st];
  d(n-1)=(ct*a-st*VAL);
 
  A(n1-1,n1-1)=d(j-1);
  A=A(1:n1-1,1:n1-1);

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

%-------------------------------------------------------------
% 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,Q,eigv]=QRQQ(G,d,Q,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,Q]=CRepQ(G,d,Q);


% 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,Q]=PerSeqGivQ(G,d,Q,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 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,Q]=PerSeqGivQ(G,d,Q,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;

Qt=[Q(1,:);Q(2,:)];
Qt=giv*Qt;
Q(1,:)=Qt(1,:);
Q(2,:)=Qt(2,:);


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,:));
  Qt=[Q(i,:);Q(i+1,:)];
  Qt=giv*Qt;
  Q(i,:)=Qt(1,:);
  Q(i+1,:)=Qt(2,:);

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

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

%---------------------------------------------------------------
% 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,Q]=CRepQ(G,d,Q)

  % 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);
    Qt=[Q(i-1,:); Q(i,:)];
    Qt=[c s ; -s c]*Qt;
    Q(i-1,:)=Qt(1,:);
    Q(i,:)=Qt(2,:);
    
    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 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
  % Instead of applying a symmetric householder
  % a loop of Givens is performed, for
  % numerical unstable problems, this is the best
  % option

  % implemented like in Golub and vanloan
  n=size(A,1);
  
  for i=1:n-2
   [A]=SymGiv(A,i,i+1,n);
   %Q*Ao*Q'
   %A
   %pause
  end
 

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

% 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,fliplr(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];




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 



























% This function, performs a Givens rotation
% on element, by multipying on the lefthandside, 
% in fact applying row operations
% the performed operation is a symmetric givens



% The element that we want to annihilate, 
% is the element in the matrix M
% on position Row Column

% the row that will be used for the givens is 
% the row Row2

function [M,G,m1]=SymGiv(M,Row,Row2,Column)

G=givens(M(Row2,Column),M(Row,Column));

Mtransf=[M(Row2,:) ; M(Row,:) ];

Mtransf=G*Mtransf;
%Mtransf(2,1)=0;

M(Row2,:)=Mtransf(1,:);
M(Row,:)=Mtransf(2,:);
M;

m1=M(1,1);

Column=Row2;
Column2=Row;


clear Mtransf

Mtransf= [ M(:,Column) M(:,Column2)];

Mtransf=Mtransf*G';

M(:,Column)=Mtransf(:,1);
M(:,Column2)=Mtransf(:,2);

M;





