% ST   Performs the shift-through lemma onto three Givens transformations.
%
%    [G1n,G2n,G3n]=FUSION[G1,G2,G3]
% 
%    G1,G2,G3 are the three Givens transformations where G1 and G3 act on
%    row 2 and 3 (resp. 1 and 2), and the Givens G2 acts on row 1 and 2
%    (resp. 2 and 3).
%
%    The outcome are three Givens transformations G1n,G2n,G3n, where G1
%    and G3 act on row 1 and 2 (resp.2 and 3) and G2 acts on row 2 and 3
%    (resp. 1 and 2). 
%
%
%    All three Givens transformations are stored in the following format [c,s]^T, 
%    where
%        
%        | c       s       |   
%    G = |                 |   
%        |-conj(s) conj(c) |   
%
%
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 21/06/2007


function [G1,G2,G3]=ST(G1,G2,G3)

    global G1o G2o G3o G;
    global niter LOOP;
    tt=0;
    G1o=G1;
    G2o=G2;
    G3o=G3;
  


  % This function performs the shift throug-lemma
  % It changes slightly the order of the Given Givens trnasformations
    
  % This is a rigid implementation, based on the $QR$-decomposition
  % of the involved orthogonal matrix, constructed by the
  % Givens transformations
 
  % Due to the non uniqueness of the shift through lemma in case of
  % matrices almost identical to the identity we will now build in a
  % stopping criterion
  th=10^(-10); % threshold for removing the value
  
  if (abs(G1(2))<th)
      tt=1;
  end
  
  
  if (abs(G2(2))<th)
      tt=1;
  end
      
    
  if (abs(G3(2))<th)
    tt=1;
  end
  
  % TEMPORARY
  go1=G1;
  go2=G2;
  go3=G3;
  
    
  % Construct the orthogonal matrix, by creating the Full Givens
  % transformation matrices
    Giv1=[G1(1),-conj(G1(2)),0;G1(2),conj(G1(1)),0;0,0,1];
    Giv2=[1,0,0;0,G2(1),-conj(G2(2));0,G2(2),conj(G2(1))];
    Giv3=[G3(1),-conj(G3(2)),0;G3(2),conj(G3(1)),0;0,0,1];
    
  % Make the multiplications
    O=Giv1*Giv2*Giv3;
    Oorig=O;
   
    
  % Compute the new factorization
  
    transposed = false;
    if norm(O(2:3,1)) < norm(O(1,2:3))
    	transposed = true;
	O=O';
	Oorig=Oorig';
    end
  
    [c,s,r] = Givensnch(O(2,1),O(3,1));
    Giv1=[1,0,0;0,c,s;0,-conj(s),conj(c)];
    O=Giv1*O;
    G1(1)=conj(c);
    G1(2)=conj(s); 
    
    % compute the second Givens based on the vector having highest norm
    sw = false;
    kk=1;
    vv=O(1:2,1);
    maxnorm=norm(vv);
    for k=2:3
	if norm(O(1:2,k)) > maxnorm
		sw=true;
		vv=O(1:2,2);
	end
    end
		
      
    [c,s,r] = Givensnch(vv(1),vv(2));
    if sw
    	% Compute the Givens transformation first
        Giv2=[-conj(s),conj(c),0;-c,-s,0;0,0,1];
	Ohelp=Giv2*O;
        ss=conj(sign(Ohelp(1,1)));
        % Based on the value of Ohelp, we change the transformation 
        % sligthly
        s=s*conj(ss);
        c=c*conj(ss);
        Giv2=[-conj(s),conj(c),0;-c,-s,0;0,0,1];
        G2(1)=-s;
	G2(2)=c;
	O=Giv2*O;
    else
    	Giv2=[c,s,0;-conj(s),conj(c),0;0,0,1];
    	O=Giv2*O;
    	G2(1)=conj(c);
    	G2(2)=conj(s);
    end
    
    [c,s,r] = Givensnch(O(2,2),O(3,2));
    Giv3=[1,0,0;0,c,s;0,-conj(s),conj(c)];
    O=Giv3*O;
    G3(1)=conj(c);
    G3(2)=conj(s); 
    
    if transposed
    % star the Givens
      Gh=G1;
      G1(1)=conj(G3(1));
      G1(2)=-G3(2);
      G3(1)=conj(Gh(1));
      G3(2)=-Gh(2);
      Gh=G2;
      G2(1)=conj(Gh(1));
      G2(2)=-Gh(2);
    end


% GIVENSNCH Compute a Givens transformation matrix
%
%    [c,s,r] = GIVENSCH(x,y) returns the cosine and sine of the complex
%    Givens rotation matrix and the resulting norm r.
%    
%        | c       s       |                  | x |     | r |
%    G = |                 |   such that  G * |   |  =  |   |
%        |-conj(s) conj(c) |                  | y |     | 0 |
% 
%    where c, s are complex, and c^2 + |s|^2 = 1. 
%    The r is real
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 21/06/2007

function [c,s,r] = GIVENSNCH(x,y);

   if (y == 0),
      c = 1; s = 0; r = x;
   else
      if (abs(x) >= abs(y))
         %disp('Case 1')
         t = y/x; r = sqrt(1 + abs(t)^2);
         c = 1/r;
         s = t'*c;
         r = x*r;
      else
         %disp('Case 2')
         t = x/y; r = sqrt(1 + abs(t)^2 );
         s = 1/r;
         c = t'*s;
         r = y*r;
      end
   end

   
   %% Unfortunately this Algorithm can output negative values of r,
   %% This is not consistent with our computations, we will identify the
   %% Givens transformations uniquely by outputting only positive real r
   %% In order to turn slightly the Givens transformation we will
   %% multiply the result with abs(x)
   
   if (x~=0)
     % First we turn the matrix, such that c is real
     %theta=conj(c)/abs(c);
     
     % Or we assume the r to be real
     theta=conj(sign(r));
     c=c*theta;
     s=s*theta;
     r=theta*r;
     
     
   end
   
   