%  ConsMatLeft  Reconstructs a matrix using rotational patterns
%
%    A=ConsMatLeft(Grot,Gind,R) Constructs the matrix A=QR, where the unitary
%    matrix Q is represented by the factorization Grot, Gind.
%    R is an upper triangular matrix, on which the rotations will be applied.
%
%    Software written by Raf Vandebril
%    raf.vandebril-at-cs.kuleuven.be
%    Revision Date: August 30, 2010
%


function R=ConsMatLeft(Grot,Gind,R)
  
  % Check if the input is not empty
    if (~isempty(Gind))
  
      % The rotations are re-ordered sot that they can be performed
      % immediately on the matrix R
      [SG,I]=sort(Gind(:,3));
      Gind=Gind(I,:); % Reorder the index storage
      Grot=Grot(:,I); % Reorder the rotations themselves
      
      % Perform all operations on the matrix.
      for i=size(Gind,1):-1:1
        % Assign the rotation to Giv and execute it.
        Giv=[Grot(1,i) Grot(2,i) ; -Grot(2,i)' Grot(1,i)'];
        R([Gind(i,1),Gind(i,2)],:)=Giv*R([Gind(i,1),Gind(i,2)],:);
      end    
  end
  
    
