%
%    Annihilating of components of vector x using
%    classical or hyperbolic Givens transformation matrices
%
%    [y,GG,GGG,Q,Psi,b]=vechg(n,p,x,Phi)
%
% input data:
%     n ... dimension of the vector x
%     p ... components p+1,...,n of the vector x will be 
%           annihilated,
%           their "energy" will be concentrated
%           into the p-th component of x
%     x ... the reduced vector
%   Phi ... the given diagonal matrix
%           (diagonal entries of Phi are +1 or -1)
% output data:
%     y ... vector x after the transformation
%    GG ... product of all matrices for transformation of x into y,
%            GG=G(n)*G(n-1)*...*G(p+1); GG*x=y
%   GGG ... GGG=G(p+1)*G(p+2)*...*G(n-1)*G(n),
%            GGG'*Phi*GGG=Psi, 
%     Q ... inverse matrix to GG; x=Q*y
%   Psi ... Psi=GGG'*Phi*GGG, matrix Phi after the transformation
%     b ... parameter,
%           it gives the number of "zero hyperbolic energy" cases
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [y,GG,GGG,Q,Psi,b]=vechg(n,p,x,Phi)
   b=0; a=0; GG=eye(n); GGG=eye(n); Q=eye(n);
   for q=(p+1):n
       if a==4         %the case of zero hyperbolic energy
       b=b+1;
       [y,G,Psi,a]=zerogiv(n,p,q,x,Phi);
       else
       [y,G,Psi,a]=hypgiv(n,p,q,x,Phi);
       end
       x=y;
       GG=G*GG;
       GGG=GGG*G;
       Q=Q*inv(Psi)*G'*Phi;
       Phi=Psi;
   end  
     if a==4
% the last transformed vector had zero hyperbolic energy, i.e. in the
% resulting matrix Psi would be Psi(p,p)=+2 or -2
        b=b+1;
        [y,G,Psi,a]=zerogiv(n,p,n,x,Phi);
        GG=G*GG;
        GGG=GGG*G;
        Q=Q*Psi*G'*Phi;
      end
	


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


