%
%
%   Hyperbolic Householder matrix for the reduction of
%      the vector x with zero hyperbolic energy
%
%    [y,H,HH,Psi]=zerohh(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 ... the matrix of reduction, H*x=y,
%    HH ... HH'*Phi*HH=Psi
%   Psi ... the diagonal matrix with \pm 1 on the diagonal,
%	    generally different from Phi
%
%
function [y,H,HH,Psi]=zerohh(n,x,Phi)
for j=1:n
   y(j)=0;
end
y=y';
if n==2
   [y,H,HH,Q,Psi,b]=vechg(n,1,x,Phi);
else
  Phi1=eye(n-1);
  for j=1:n-1
      x1(j)=0;
  end
  x1=x1';
  for j=2:n
    x1(j-1)=x(j);
    Phi1(j-1,j-1)=Phi(j,j);
  end
  he1=x1'*Phi1*x1;
  x1phi=sign(he1)*sqrt(abs(he1));
  if sign(Phi1(1,1))==sign(he1)
     [y1,H1]=hyphsd(n-1,x1,x1phi,Phi1);
  else P=eye(n-1);
     j=2;
     while abs(sign(Phi1(j,j))-sign(he1))>abs(eps)
       j=j+1;
     end
     P(1,1)=0; P(j,j)=0; P(1,j)=1; P(j,1)=1;
 %    Phi1=P*Phi1*P;
  %   x1=P*x1;
     [y1,H1]=hyphsd(n-1,P*x1,x1phi,P*Phi1*P);
    H1=H1*P;
    HH1=P*H1;
   end
   z(1)=x(1); z(2)=y1(1);
   z=z';
   for j=2:n
     Phi(j,j)=Phi1(j-1,j-1);
   end
   Phi2=eye(2);
   Phi2(1,1)=Phi(1,1); Phi2(2,2)=Phi(2,2);
   [z,G1,GGG,Q,Psi1,b]=vechg(2,1,z,Phi2);
   H=eye(n); G=eye(n); HH=eye(n);
   for j=2:n
     for k=2:n
       H(j,k)=H1(j-1,k-1);
     end
   end
   for j=1:2
     for k=1:2
       G(j,k)=G1(j,k);
       HH(j,k)=GGG(j,k);
     end
   end
   y(1)=z(1);
   H=G*H;
     Psi(1,1)=Psi1(1,1); Psi(2,2)= Psi1(2,2);
   for j=3:n
     Psi(j,j)=Phi(j,j);
   end
end


