% GENTESTMAT Generate test matrices for structured rank algorithms
%         
%
%    [G,R,H]=GENTESTMAT(n, struc,par,decu,decv)
%    Returns the Givens-vector representation of an arbitrarily
%    constructed Hessenberg-like matrix of dimension n x n.
%    G --> a 2 times n-1 vector containing the cosine and sine of the
%      Givens-vector representation
%    R --> an upper triangular matrix
%    H --> This is the full matrix
%
%    struc --> structure of the matrix, determines, if not specified 
%              the number of shifts k:
%              'rs' means a real symmetric matrix, k=1
%              'r' real not necessarily symmetric, k=2
%              'c' a complex matrix, k=1
% 
%    par --> construction parameter,
%            'g' random Givens for the Givens-vector
%            'u' random uv representation transformed to Givens-vector
%            standard choice is 'u'
%
%    dec --> puts a small decay on the elements of the lower triangular
%    rank 1 part. The vector u is multiplied elements wise with
%    (decu,decu^2,....), (dec can also be chosen >0).
%    Similar remark holds for decv. Only applied in case the par='u'.
%
%    We remark that the upper triangular part is constructed as the R
%    factor coming from the QR factorization of a random
%    matrix. Otherwise this matrix is extremely bad conditioned.
%
%
%    Software of the MaSe - Group
%    mase-at-cs.kuleuven.ac.be
%    Revision Date: 23/03/2007

function [G,R,H]=GENTESTMAT(n,struc,par,decu,decv)
  
  
  % Check if the structure is properly defined
  if (exist('struc')~=1)
    disp('PLEASE SELECT THE STRUCTURE OF THE MATRIX');
    disp('"rs" means a real symmetric matrix');
    disp('"r" real not necessarily symmetric');
    disp('"c" a complex matrix');
    return
  end
  
  
  % Assign the individual parameters for the specific problem
  switch struc
   case 'rs'
    cs=0;
   case 'r'
    cs=1;
   case 'c'
    cs=2;
   otherwise
    disp('PLEASE SELECT THE STRUCTURE OF THE MATRIX');
    disp('"rs" means a real symmetric matrix');
    disp('"r" real not necessarily symmetric');
    disp('"c" a complex matrix');
    return
  end
  
  % BRIEFLY THIS MEANS
  % 2 for complex matrix
  % 1 for real matrix (complex conj eigv)
  % 0 for a real symmetric matrix


  if (exist('decu')==1)
    % Generate a decay factor for the elements
    elt=1;
    fac=decu;
    for ct=1:n
      elt=elt*fac;
      defact(ct)=elt;
    end         
    
    % The defact is turned off, as it tends to create almost 
    % singular matrices
  else
    decu=ones(n,1);
  end
  
  
  if (exist('decv')==1)
    % Generate a decay factor for the elements
    elt=1;
    fac=decv;
    for ct=1:n
      elt=elt*fac;
      defact(ct)=elt;
    end         
    
    % The defact is turned off, as it tends to create almost 
    % singular matrices
  else
    decv=ones(n,1);
  end



  if (par=='g')
    % Generate the random givens transformations
    
    for k=1:n-1;
      x=rand(1,1);
      y=rand(1,1);
      if (cs==2)
        x=x+i*rand(1,1);
        y=y+i*rand(1,1);
      end
      
      % Compute the random Givens transformation.
      [c,s]=Givensch(x,y);
      G(:,k)=[c;s];
    end
    
    
    
    % Construct the upper triangular part
    
    
    Rhlp=randn(n,n);
    if (cs==2)
      Rhlp=Rhlp+i*rand(n,n);
    end
    [Q,R]=qr(Rhlp);

    if (cs==0)
      disp('SORRY THIS PART IS NOT FUNCTIONAL YET')
      disp('NO GIVENS-VECTOR SYMMETRIC GENERATION')
      % This means we have a symmetric matrix, we have to construct
      % the upper triangular part, very specific.
       d=rand(n,1);
       H=diag(d);
       for cnt=1:n-1
         Giv=[G(1,cnt),-conj(G(2,cnt));G(2,cnt),conj(G(1,cnt))];
         H(:,cnt:cnt+1)=H(:,cnt:cnt+1)*Giv';
       end
       R=triu(H);
      
    end
    
    
    
    
    
  else
    % GENERATORS U AND V
    % Generate the vectors with randNNN !!!
    u=randn(n,1); 
    v=randn(n,1);
    
    % cs=2 means complex matrices
    if (cs==2)
      u=u+i*randn(n,1); %.*defact';
      v=v+i*randn(n,1);
    end
    
    % Apply the rescaling
    u=u.*decu;
    v=v.*decv;
    
    % Due to the fact that an upper triangular random matrix is extremely 
    % bad conditioned, we will try to change it via the QR
    % Real matrices
    if (cs==0)
      H=tril(u*v');
      H=H+triu(H',1);
    else 
      % Generate a generator representable Hessenberg-like matrices
      % The upper triangular part needs to be coming from the QR factorization,
      % otherwise it will be extremely bad conditioned
      Rhlp=randn(n-1,n-1)+i*rand(n-1,n-1);
      [Q,Rhlp]=qr(Rhlp);
      R=zeros(n,n);
      R(1:n-1,2:n)=Rhlp;
      H=R+tril(u*v');
    end
    
    % ---------------------END CONSTRUCTION OF THE MATRIX-----------------




    %----- Extract the Givens-upper triangular matrix representation------
    % apply the givens to H to check the accuracy
    R=H;
    nrm=u(n);
    for cnt=n-1:-1:1
      % The output s from the Givensn function equals -conj(s) of the first
      % column
      [c,s,nrm]=Givensnch(u(cnt),nrm);
      Giv=[c,s;-conj(s),conj(c)];
      % The Givens-vector representation stores the first column
      % of the Givens transformations In this case the first column of the
      % transpose 
      G(1,cnt)=conj(c);
      G(2,cnt)=conj(s);
      R(cnt:cnt+1,:)=Giv*R(cnt:cnt+1,:);
      %pause
    end

    % Delete small elements
    R=triu(R);

  end
  


  % --------- REBUILD THE TEST MATRIX -------------------------------
  % In order to make an as accurate as possible comparison, we will rebuild
  % the matrix based on the Givens-vector representation, and output this
  % one as the correct matrix
  H=R;
  for cnt=1:n-1
    Giv=[G(1,cnt),-conj(G(2,cnt));G(2,cnt),conj(G(1,cnt))];
    H(cnt:cnt+1,:)=Giv*H(cnt:cnt+1,:);
  end
  
  
  
  
  
  
  
  %-------------------------------------------------------------
  % GIVENSN Compute a Givens transformation matrix
%
%    [c,s,r] = GIVENS(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. 


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