% GENNORMAL Generate any type of normal matrix
%
%       N=GENNORMAL(n)
%       Generates an arbitrary normal matrix of size n.
%      
%       N=GENNORMAL(n,ntype)
%       Generates a specific normal matrix of size n.
%       ntype='real'     arbitrary real normal matrix;
%             'complex'  arbitrary complex normal matrix;
%             'sym'      symmetric matrix;
%             'skewsym'  skew-symmetric matrix;
%             'herm'     hermitian matrix;
%             'skewherm' skew-hermitian matrix;
%             'uni'      unitary matrix;
%             'ort'      orthogonal matrix;
%
%       N=GENNORMAL(n,ntype,outp)
%       standard outp='n'
%       in case outp='y' extra info is generated
%
%
%       Raf Vandebril
%       raf.vandebril - a - cs.kleuven.be
%       Revision Date: 1/9/2008


function N=GenNormal(n,ntype,outp)
  
  if not(exist('ntype'))
    ntype='complex';
  end
  
  if not(exist('outp'))
    outp='n';
  end
  
  % Define I to be the square root of -1
  I=sqrt(-1);
   
  switch ntype % switch for strings invokes strcmp
   case 'complex' % Arbitrary normal matrix
    D=diag(rand(n,1)+I*rand(n,1));
    A=rand(n,n)+I*rand(n,n);
    [Q,R]=qr(A);
    N=Q'*D*Q;
    if outp=='y' 
      disp('Arbitrary normal matrix'), 
      disp('Norm of N N^* - N^* N');
      norm(N*N'-N'*N)
    end;
   
   case 'real' % Arbitrary real normal matrix
    %D=diag(rand(n,1));
    A=rand(n,n);
    disp('THIS DOES NOT WORK AS IT SHOULD, only real eigenvalues')
    disp('No 2x2 complex blocks!!')
    N=A+A';
    %[Q,R]=qr(A);
    %N=Q'*D*Q;
    if outp=='y' 
      disp('Arbitrary normal matrix'), 
      disp('Norm of N N^* - N^* N');
      norm(N*N'-N'*N)
    end;
  
    
   case 'sym' % Symmetric matrix (Real elements)
    % Possible option based on the eigenvalues
    %D=diag(rand(n,1));
    %A=rand(n,n);
    %[Q,R]=qr(A);
    %N=Q'*D*Q;
    A=rand(n,n);
    N=A+A.';
    if outp=='y' 
      disp('Symmetric matrix')
      disp('Norm of N N^* - N^* N');
      norm(N*N'-N'*N)
      disp('Norm of N - transpose(N) ');
      norm(N-N.')
    end;
      
   case 'skewsym' % Skew symmetric matrix (Real elements)
    A=rand(n,n);
    N=A-A.';
    if outp=='y' 
      disp('Skew-symmetric matrix');
      disp('Norm of N N^* - N^* N');
      norm(N*N'-N'*N)
      disp('Norm of N+transpose(N)');
      norm(N+N.')
    end;
  
   case 'herm' % Hermitian matrix (Complex elements)
    % Possible option based on eigenvalues
    %D=diag(rand(n,1));
    %A=rand(n,n)+I*rand(n,n);
    %[Q,R]=qr(A);
    %N=Q'*D*Q;
    A=rand(n,n)+I*rand(n,n);
    N=A+A';
    if outp=='y' 
      disp('Hermitian matrix');
      disp('Norm of N N^* - N^* N');
      norm(N*N'-N'*N)
      disp('Norm of N-N^*');
      norm(N-N');
    end;
    
   
   case 'skewherm' % Skew-Hermitian matrix (Complex elements)
    % Possible option based on eigenvalues
    %D=diag(I*rand(n,1));
    %A=rand(n,n)+I*rand(n,n);
    %[Q,R]=qr(A);
    %N=Q'*D*Q;
    A=rand(n,n)+I*rand(n,n);
    N=A-A';
    if outp=='y' 
      disp('Skew-hermitian matrix')
      disp('Norm of N N^* - N^* N');
      norm(N*N'-N'*N)
      disp('Norm N+N^*')
      norm(N+N')
    end;

   case 'uni' % Unitary matrix (Complex elements)
    A=rand(n,n)+I*rand(n,n);
    [N,R]=qr(A);
    if outp=='y' 
      disp('Unitary matrix');
      disp('Norm of N N^* - N^* N');
      norm(N*N'-N'*N)
      disp('Norm of N N^* - I');
      norm(N*N'-eye(n))
    end;
    
   case 'ort' % Orthogonal matrix (Real elements)
    A=rand(n,n);
    [N,R]=qr(A);
    if outp=='y' 
      disp('Orthogonal matrix'), 
      disp('Norm of N N^* - N^* N');
      norm(N*N'-N'*N)
      disp('Norm of N transpose(N)- I');
      norm(N*N.'-eye(n))
      
    end;
    
   otherwise
    warning('Unknown matrix type')
    help GenNormal;
    error('Exit without assigning N')
      
  end