% UNIEQUINORM Transforms a normal matrix via unitary equivalences 
%             to tridiagonal form
%
%       T=UniEquiNorm(N)
%       Outputs a unitary equivalent tridiagonal matrix T=U'*N*V,
%       N is assumed to be normal.
%
%       [U,T,V]=UniEquiNorm(N)
%       T is tridiagonal and N=U*T*V', U and V are unitary. 
%
%       [U,T,V,D]=UniEquiNorm(N)
%       D is unitary diagonal, such that T is self-adjoint 
%       w.r.t. the scalar product <x,y>_D = x^T D y.
%       This means that inv(D)*transp(T)*D = T.
%       [See Mackey, Mackey & Tisseur, SIMAX, 2005]
%       
%
%       T=UniEquiNorm(N,redtype)
%       [U,T,V]=UniEquiNorm(N,redtype)
%       [U,T,V,D]=UniEquiNorm(N,redtype)
%       redtype specifies the structure of the resulting tridiagonal
%      
%       When not specified, T is arbitrary tridiagonal
%       redtype='free'     no specific reduction;
%                          this is the standard setting;
%               'sym'      symmetric reduction;
%               'pseusym'  pseudo-symmetric reduction;
%                          specify d, consisting of 1,-1, see below;
%               'skewsym'  skew-symmetric reduction;
%               'herm'     hermitian reduction;
%               'pseuherm' pseudo-hermitian reduction;
%                          Specify d, consisting of 1,-1, see below;
%               'skewherm' skew-conjugate reduction;
%               'arb'      arbitrary reduction: Specify d, see below
%
%
%       [U,T,V,D]=UniEquiNorm(N,'arb',d)
%       [U,T,V,D]=UniEquiNorm(N,'pseusym',d)
%       the vector d imposes the relation between sub- and superdiagonal 
%       elements: T(i+1,i)=d(i)*T(i,i+1)
%      
%
%
%       Raf Vandebril
%       Revision Date: 25 IX 08


% REMARK: To make the code better, which means more stable and less
% memory consuming, we should not make the explicit product of the
% Householder matrices, but store them in the place which became
% available after creating all the zeros in the tridiagonal matrix.
% ---> See comments in the manuscript

function [U,T,V,D]=UniEquiNorm(A,redtype,D)
  
    % Initializations for the matrix A 
    n=size(A,1);
    I=sqrt(-1);

    if (nargout>1)
      % Also the unitary transformations are desired
      U=eye(n);
      V=eye(n);
    end
    
      

    for i=1:n-1
      
      % Use the Householder transformation from the gallery
      [v,beta,s] = gallery('house',A(i+1:n,i));
      % H = eye(n) - beta*V*V'
      % H x = s e1
      A(i+1:n,i+1:n)=A(i+1:n,i+1:n)-beta*v*v'*A(i+1:n,i+1:n);
      A(i+1,i)=s;
      A(i+2:n,i)=0;
      
      % Construct the cummulated U
      if (nargout>1)
        U(:,i+1:n)=U(:,i+1:n)-beta*U(:,i+1:n)*v*v';
      end
      
      
      [v,beta,s] = gallery('house',A(i,i+1:n)');
      A(i+1:n,i+1:n)=A(i+1:n,i+1:n)-conj(beta)*A(i+1:n,i+1:n)*v*v';
      A(i,i+1)=conj(s);
      A(i,i+2:n)=0;
 
      if (nargout>1)
        V(:,i+1:n)=V(:,i+1:n)-conj(beta)*V(:,i+1:n)*v*v';
      end
    end
    
    % Assign outputs in case of 1 or 3 arguments
    if (nargout>1)
      T=A;
    else
      U=A;
    end
    
    
    % After the standard transformation is performed we will now adjust
    % the factorization such to obtain the chosen self-adjoint T for a
    % specific scalar product space.

    
    if (exist('redtype') && ~strcmp(redtype,'free'))
      % Build now the matrix D used for the transformations
      switch redtype
       case 'sym'      % symmetric reduction;
        D=diag(ones(n,1));
       case 'pseusym'  % pseudo-symmetric reduction;
        if ~exist('D')
          error('D not specified');
        end
       case 'skewsym'  % skew-symmetric reduction;
        d=ones(1,n);
        d(2:2:n)=-1;
        D=diag(d);
       case 'herm'     % hermitian reduction;
        [S,UD]=CompSymUniDec(T);
        [S,D]=CompConjUniDec(S);
        D=inv(D);
       case 'pseuherm'
        [S,UD]=CompSymUniDec(T);
        [S,Dt]=CompConjUniDec(S);
        Dt=inv(Dt);
        D=Dt*D;
        %d=diag(Dt);
        %d(2:2:n)=(-1)*d(2:2:n);
        %D=diag(d);
        
        
       case 'skewherm' % skew-conjugate reduction;
        [S,UD]=CompSymUniDec(T);
        [S,D]=CompConjUniDec(S);
        D=inv(D);
        d=diag(D);
        d(2:2:n)=(-1)*d(2:2:n);
        D=diag(d);
        
       case 'arb'      % arbitrary reduction: Specify D
        if ~exist('D')
          error('D not specified');
        end 
       otherwise
        warning('Unknown matrix type')
        help UniEquiNorm;
        error('Exit without assigning N')
      end

      %D=(-1)*diag(ones(n,1));
    
      
      [S,UD]=CompSymUniDec(T);
      T=T*conj(UD)*D;
      V=V*conj(UD)*D;
    end
    
    
    if ((nargout==4) && ~exist('D'))
      % The unitary weight factor is desired and not yet specified
      [S,D]=CompSymUniDec(T);
      
    end
    
    
    
    
%---------------------------------------------------------
    
% COMPSYMUNIDEC Computes the complex symmetric unitary diagonal
%       decomposition of a tridiagonal matrix.
%
%       [S,U]=CompSymUniDec(T)
%       T is a tridiagonal coming from the unitary equivalence 
%              transformation of normal matrices
%       S is symmetric and U a unitary Diagonal with U(1,1)=1.
%       T=S*U
%       
%
%
%       Raf Vandebril
%       raf.vandebril - a - cs.kleuven.be
%       Revision Date: 1/9/2008

function [S,U]=CompSymUniDec(T)

  n=size(T,1);
  
  D=zeros(n,1);
  D(1)=1;
  for k=2:n
    D(k)=D(k-1)*T(k,k-1)/T(k-1,k);
  end
  U=diag(D);
  
  S=T*U;
  U=conj(U);
  