givens[a_,b_,i_,j_]:=Block[{c,s,sgn},
c=a/Sqrt[a^2+b^2];
s=-b/Sqrt[a^2+b^2];
sgn=If[Abs[b]>Abs[a],Sign[b],Sign[a]];
{c,s}=sgn{c,s};
Normal[SparseArray[{{i,i}->c,{i,j}->s,{j,i}->-s,{j,j}->c,{k_,k_}->1},{n,n}]]]
givens[-1,2,2,3]//MatrixForm
givens[1,-2,2,3]//MatrixForm
givens[-2,1,2,3]//MatrixForm
givens[2,-1,2,3]//MatrixForm

b=Normal[SparseArray[{{i_,i_}->i,{i_,j_}/;j==i+1->1},{4,4}]//N];
b//MatrixForm

trail=b[[3;;,3;;]];
trail//MatrixForm
singvals=Diagonal[SingularValueDecomposition[trail][[2]]]
mu = singvals[[2]];

lead=b[[1;;2,1;;2]];
lead//MatrixForm
t=lead\[Transpose].lead;
t//MatrixForm
s=t-mu^2 IdentityMatrix[2];
s//MatrixForm
g1=givens[s[[1,1]],s[[1,2]],1,2];
g1//MatrixForm
b1=b.g1;
b1//MatrixForm

g2=givens[b1[[1,1]],b1[[2,1]],1,2];
b2=g2\[Transpose].b1;
b2//MatrixForm

g3=givens[b2[[1,2]],b2[[1,3]],2,3];
b3=b2.g3;
b3//MatrixForm

g4=givens[b3[[2,2]],b3[[3,2]],2,3];
b4=g4\[Transpose].b3;
b4//MatrixForm

g5=givens[b4[[2,3]],b4[[2,4]],3,4];
b5=b4.g5;
b5//MatrixForm

g6=givens[b5[[3,3]],b5[[4,3]],3,4];
b6=g6\[Transpose].b5;
b6//MatrixForm

Diagonal[b6]//InputForm

Diagonal[b6,1]//InputForm

n=3
b=Normal[SparseArray[{{i_,i_}->i,{i_,j_}/;j==i+1->2i},{n,n}]]//N;
b//MatrixForm

trail=b[[2;;,2;;]];
trail//MatrixForm
singvals=Diagonal[SingularValueDecomposition[trail][[2]]]
mu = singvals[[2]];

lead=b[[1;;2,1;;2]];
lead//MatrixForm
t=lead\[Transpose].lead;
t//MatrixForm
s=t-mu^2 IdentityMatrix[2];
s//MatrixForm
g1=givens[s[[1,1]],s[[1,2]],1,2];
g1//MatrixForm
b1=b.g1;
b1//MatrixForm

g2=givens[b1[[1,1]],b1[[2,1]],1,2];
b2=g2\[Transpose].b1;
b2//MatrixForm

g3=givens[b2[[1,2]],b2[[1,3]],2,3];
b3=b2.g3;
b3//MatrixForm

g4=givens[b3[[2,2]],b3[[3,2]],2,3];
b4=g4\[Transpose].b3;
b4//MatrixForm

Diagonal[b4]//InputForm

Diagonal[b4,1]//InputForm

b={{1,1,0},{0,0,1},{0,0,3}};
b//MatrixForm

trail=b[[2;;,2;;]];
singvals=Diagonal[SingularValueDecomposition[trail][[2]]]
mu = singvals[[2]];

lead=b[[1;;2,1;;2]];
t=lead\[Transpose].lead;
t//MatrixForm
s=t-mu^2IdentityMatrix[2];
s//MatrixForm
g1=givens[s[[1,1]],s[[1,2]],1,2];
g1//MatrixForm
b1=b.g1;
b1//MatrixForm

n=3;
b={{1, 2, 0},{0,2,1},{0,0,3}}//N;
b//MatrixForm

trail=b[[;;2,;;2]];
trail//MatrixForm
singvals=Diagonal[SingularValueDecomposition[trail][[2]]]
mu = singvals[[2]];

lead={{b[[3,3]],b[[2,3]]},{b[[3,2]],b[[2,2]]}};
lead//MatrixForm
t=lead\[Transpose].lead;
t//MatrixForm
s=t-mu^2IdentityMatrix[2];
s//MatrixForm
g1=givens[s[[1,1]],s[[1,2]],3,2];
g1//MatrixForm
b1=g1\[Transpose].b;
b1//MatrixForm

g2=givens[b1[[3,3]],b1[[3,2]],3,2];
g2//MatrixForm
b2=b1.g2;
b2//MatrixForm

g3=givens[b2[[2,3]],b2[[1,3]],2,1];
g3//MatrixForm
b3=g3\[Transpose].b2;
b3//MatrixForm

g4=givens[b3[[2,2]],b3[[2,1]],2,1];
g4//MatrixForm
b4=b3.g4;
b4//MatrixForm

Diagonal[b4]//InputForm

Diagonal[b4,1]//InputForm

n=4;
b=Normal[SparseArray[{{i_,i_}->i,{i_,j_}/;j==i+1->-1},{4,4}]]//N;
b//MatrixForm

lead=b[[;;2,;;2]];
t=b\[Transpose].b;
g1=givens[t[[1,1]],t[[1,2]],1,2];
b1=b.g1;
b1//MatrixForm

g2=givens[b1[[1,1]],b1[[2,1]],1,2];
b2=g2\[Transpose].b1;
b2//MatrixForm

g3=givens[b2[[1,2]],b2[[1,3]],2,3];
b3=b2.g3;
b3//MatrixForm

g4=givens[b3[[2,2]],b3[[3,2]],2,3];
b4=g4\[Transpose].b3;
b4//MatrixForm

g5=givens[b4[[2,3]],b4[[2,4]],3,4];
b5=b4.g5;
b5//MatrixForm

g6=givens[b5[[3,3]],b5[[4,3]],3,4];
b6=g6\[Transpose].b5;
b6//MatrixForm

Diagonal[b6]//InputForm

Diagonal[b6,1]//InputForm

n=9
b=Normal[SparseArray[{{i_,i_}->i,{i_,j_}/;j==i+1->2i},{n,n}]]//N;
b//MatrixForm

g1=givens[b[[9,9]],b[[8,9]],9,8];
g1//MatrixForm
b1=g1\[Transpose].b;
b1//MatrixForm

g1[[8;;9,8;;9]]\[Transpose].{{8,16},{0,9}}

A={{-2,0,0,0,0},{-1,5,0,0,0},{0,4,8,0,0},{0,0,5,-3,0},{0,0,0,2,7}}//N;
A//MatrixForm
n=Dimensions[A][[1]];

G1=givens[A[[1,1]],A[[2,1]],1,2];
G1//MatrixForm
A1=G1\[Transpose].A
A1//MatrixForm

G2=givens[A1[[2,2]],A1[[3,2]],2,3];
G2//MatrixForm
A2=G2\[Transpose].A1;
A2//MatrixForm

G3=givens[A2[[3,3]],A2[[4,3]],3,4];
G3//MatrixForm
A3=G3\[Transpose].A2;
A3//MatrixForm

G4=givens[A3[[4,4]],A3[[5,4]],4,5];
G4//MatrixForm
A4=G4\[Transpose].A3;
A4//MatrixForm

B=A4;
U=G1.G2.G3.G4;
U//MatrixForm
Chop[A-U.B]//MatrixForm