% tests the function [gh1,gh2,gh3]=SwapGivens(g1,g2,g3)
J=[0 -1;1 0];small=10000*eps;
S=[1 0;0 small];
g=orth(S*randn(2,1));g1=g*sign(g(1));
G1=[g1 J*g1 [0;0]; 0 0 1];
g=orth(S*randn(2,1));g2=g*sign(g(1));
G2=[1 0 0; [0;0] g2 J*g2];
g=orth(S*randn(2,1));g3=g*sign(g(1));
G3=[g3 J*g3 [0;0]; 0 0 1];
[g1 g2 g3]
M1=G1*G2*G3;
[gh1,gh2,gh3]=SwapGivens(g1,g2,g3);
[gh1 gh2 gh3]
Gh1=[1 0 0; [0;0] gh1 J*gh1];
Gh2=[gh2 J*gh2 [0;0]; 0 0 1];
Gh3=[1 0 0; [0;0] gh3 J*gh3];
M2=Gh1*Gh2*Gh3;
er1=norm(M1-M2);
er2=g1(2)*g2(2)/(gh2(2)*gh3(2))-1;
er3=g3(2)*g2(2)/(gh2(2)*gh1(2))-1;
err=[er1 er2 er3]/epsfunction [gh1,gh2,gh3]=SwapGivens(g1,g2,g3)
%
% Calculates the 2x2 Givens rotations Gh1, Gh2 and Gh3 that correspond 
% to the 2x2 Givens rotations G1, G2 and G3. All of these rotations
% are embedded in 3x3 matrices, but G1, G3 and Gh2 act in the first two
% coordinates while Gh1, G2 and Gh3 act in the last two coordinates
%
c1=g1(1);s1=g1(2);c2=g2(1);s2=g2(2);c3=g3(1);s3=g3(2);
ch2=c1*c3-s1*c2*s3;
a=[s1*c3+c1*c2*s3;s2*s3];b=[c1*s3+s1*c2*c3;s1*s2];
if abs(s1)>abs(s3),
  [gh1,sh2]=qr(a,0);if gh1(1)<0, gh1=-gh1;sh2=-sh2;end
  [gh3,sh2bis]=qr(b,0);if sign(sh2)~=sign(sh2bis), gh3=-gh3;end
else
  [gh3,sh2]=qr(b,0);if gh3(1)<0, gh3=-gh3;sh2=-sh2;end
  [gh1,sh2bis]=qr(a,0);if sign(sh2)~=sign(sh2bis), gh1=-gh1;end
end
gh2=[ch2;sh2];
return