%
%        An application:
%    QR decomposition of a matrix A
%    using hyperbolic Givens transformations
%
%   [A,Q1,Q2,Q3,Psi,b]=qrhg(n,m,A,Phi)
%
%  Input data:
%        n,m ... dimensions of the matrix A
%          A ... nxm matrix to be reduced into an upper triangular form
%        Phi ... the given diagonal matrix, diagonal elements are \pm 1
%  Output data:
%          A ... upper triangular matrix
%                (input matrix A after the reduction)
%         Q1 ... product of all matrices used for reduction,
%                A(out)=Q1*A(in); Q1=Q1(p)*Q1(p-1)* ... *Q1(2)*Q1(1)
%         Q2 ... Q2'*Phi*Q2=Psi; product of the same matrices as in Q1
%                in oposite order; Q2=Q2(1)*Q2(2)*...*Q2(p-1)*Q2(p)
%         Q3 ... A(in)=Q3*A(out), i.e. Q3=inv(Q1)  
%        Psi ... matrix Phi after the transformation,
%                Psi=Q2'*Phi*Q2
%          b ... parameter, number of cases of zero hyperbolic energy
%                during each run of vechg

function [A,Q1,Q2,Q3,Psi,b]=qrhg(n,m,A,Phi)

Q1=eye(n); Q2=eye(n); Q3=eye(n);
k=min(n,m);
for i=1:k
    b(i)=0;
    end

for j=1:(k-1)
    p=j;
    x=A(:,j);
    [y,GG,GGG,Q,Psi,s]=vechg(n,p,x,Phi)
    Q1=GG*Q1;
    Q2=Q2*GGG;
    Q3=Q3*Q;
    A=GG*A;
    Phi=Psi;
    b(j)=b(j)+s;
end
if m<n
   [y,GG,GGG,Q,Psi,s]=vechg(n,k,A(:,k),Phi)
   Q1=GG*Q1;
   Q2=Q2*GGG;
   Q3=Q3*Q;
   A=GG*A;
   Phi=Psi;
   b(k)=b(k)+s;
end

