% ST   Performs the shift-through operation 
%
%    Transitions in both directions are performed. This means:
%
%      /\  -> \  /    OR \  / -> /\
%     /  \     \/         \/    /  \
%
%
%
%    [G1,G2,G3]=ST(H1,H2,H3)
% 
%    All involved rotations are stored in the following format [c,s]^T, 
%    where
%        
%        | c       s       |   
%    G = |                 |   
%        |-conj(s) conj(c) |   
%
%
%    Software written by Raf Vandebril
%    raf.vandebril-at-cs.kuleuven.be
%    Revision Date: August 31, 2010
%


function [G1,G2,G3]=ST(H1,H2,H3)
  % This function performs the shift throug-lemma
  % It changes slightly the order in which the rotations are computed,
  % to retain maximum accuracy.
       
    
  % The following vectors are used for determining G1 and G3
  % v1 corresponds to the elements in positions (1,3) and (2,3)
  % v2 corresponds to the elements in positions (3,1) and (3,2)
  % It might seem that not all signs correspond, this is indeed the case,
  % this is all taken into account when computing the rotations further
  % in the code.
  v1(2)=H2(2)*H3(2);
  v1(1)=H1(1)*conj(H2(1))*H3(2)+H1(2)*conj(H3(1)); 
  
  % These signs are reverted because we need to obtain an r which is
  % negative and real in the computation of G3.
  v3(2)=-conj(H1(2))*conj(H2(2));
  v3(1)=conj(H1(2))*conj(H2(1))*H3(1)+conj(H1(1))*conj(H3(2)); 

  
   % Compute already the top element of G2
   % To store it we need the conj of this element.
   G2(1)=(H3(1))*(H1(1))-conj(H3(2))*(H2(1))*(H1(2));
 
   % Compute G1
   [c,s,th1]=GivensR(v1(1),v1(2));
   G1(1)=c;
   G1(2)=conj(s);
   
   % Compute G3
   [c,s,th3]=GivensR(v3(1),v3(2));
   G3(1)=c;
   G3(2)=-s;
 
   
   
   
   % Assign G2(2), based on the most dominant vector, v3 or v1
   % Note that in the assignment no conj are used since both th1 and th3
   % are real elements
   if ( abs(H1(2)) < abs(H3(2)) ) 
 
      % Store now the sine of G2
      G2(2)=th1;   
        
   else 
     
     % Store now the sine of G2
      % Since the signs of v3(1) and v3(2) are already reverted, we do
      % not need to revert th3 here. 
      G2(2)=th3;

    end
