%
%
%	   Reduction of a vector x by use  of
%	the hyperbolic Householder transformation
%
%   [y,H,HH,Psi,a]=vechh(n,x,Phi)
%
% input data:
%     n ... dimension of the vector x
%     x ... vector to be reduced
%   Phi ... the given diagonal matrix with \pm 1 on the diagonal
% output data:
%     y ... vector x after the reduction
%     H ... hyperbolic Householder matrix, H*x=y, 
%    HH ...  HH'*Phi*HH=Psi
%   Psi ... the diagonal matrix with \pm 1 on the diagonal,
%	    generally different from Phi, H'*Phi*H=Psi
%     a ... a parameter:
%	    a = 1 ... the hyperbolic energy of x is nonzero,
%		      no permutation is needed
%	    a = 2 ... the hyperbolic energy of x is nonzero but
%		      the sign of the hyperbolic norm is not the same as
%		      the signum of Phi(1,1), i.e.
%		      a permutation is needed
%	    a = 3 ... the hyperbolic energy of x is zero
%
%
function [y,H,HH,Psi,a] = vechh(n,x,Phi)
he=x'*Phi*x;
if abs(he)<abs(eps)
   a=3;
   [y,H,HH,Psi]=zerohh(n,x,Phi);
else
   xphi=sign(he)*sqrt(abs(he));
   if sign(Phi(1,1))==sign(he)
      a=1; Psi=Phi;
      [y,H]=hyphsd(n,x,xphi,Phi);
      HH=H;
   else
      a=2; P=eye(n);
      j=2;
      while abs(sign(Phi(j,j))-sign(he))>abs(eps)
	  j=j+1;
      end
      P(1,1)=0; P(j,j)=0; P(1,j)=1; P(j,1)=1;
      Psi=P*Phi*P;
      x=P*x;
      [y,H]=hyphsd(n,x,xphi,Psi);
      H=H*P;
      HH=P*H;
      Psi=Phi;
   end
end


