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*T; Dv=Tv*Dv;
L=Tv*L*T; Lv=Tv*Lv*T;
I=Tv*I;

% step 6
T=eye(m,m);
Tv=eye(m,m);
for p = i+1:m;
  T1=eye(m,m); T1(p,i)=I(p,i);
  T1v=eye(m,m); T1v(p,i)=-I(p,i);
  T=T*T1;
  Tv=T1v*Tv;
endfor;
L=L*T; Lv=Tv*Lv;
I=Tv*I;

i=m;

% step 5
T=eye(m,m); T(i,i)=I(i,i);
Tv=eye(m,m); Tv(i,i)=1.0/I(i,i);
D=D*T; Dv=Tv*Dv;
L=Tv*L*T; Lv=Tv*Lv*T;
I=Tv*I;

i=m;
r=i;

% step 10
T=eye(m,m);
Tv=eye(m,m);
for p = 1:i-1;
  T1=eye(m,m); T1(p,i)=I(p,i);
  T1v=eye(m,m); T1v(p,i)=-I(p,i);
  T=T1*T;
  Tv=Tv*T1v;
endfor;
U=U*T; Uv=Tv*Uv;
I=Tv*I;

i=3;

% step 10
T=eye(m,m);
Tv=eye(m,m);
for p = 1:i-1;
  T1=eye(m,m); T1(p,i)=I(p,i);
  T1v=eye(m,m); T1v(p,i)=-I(p,i);
  T=T1*T;
  Tv=Tv*T1v;
endfor;
U=U*T; Uv=Tv*Uv;
I=Tv*I;

i=2;

% step 10
T=eye(m,m);
Tv=eye(m,m);
for p = 1:i-1;
  T1=eye(m,m); T1(p,i)=I(p,i);
  T1v=eye(m,m); T1v(p,i)=-I(p,i);
  T=T1*T;
  Tv=Tv*T1v;
endfor;
U=U*T; Uv=Tv*Uv;
I=Tv*I;

% step 12
T=eye(n,n);
Tv=eye(n,n);
for q=(r+1:n);
  for p=(1:r);
    T(p,q)=I(p,q);
    Tv(p,q)=-I(p,q);
  endfor;
endfor;
I=I*Tv;
K=T*K; Kv=Kv*Tv;

Ir = zeros(m,n);
for p=(1:m); Ir(p,p)=1.0; endfor;
Gl = P*D*L*U*eye(m,m);
Gr = Ir*K*S;

HrInr = zeros(n,n-m); HrInr(n)=1;

