%
%	[p,r12,r21,fail] = hinf_st(p,nmeas,ncon)
%
%	scale the d12 and d21 matrices to satisfy the formulas
%	and check the rank conditions.
%

function [p,r12,r21,fail] = hinf_st(p,nmeas,ncon)

fail = 0;
[ap,bp,cp,dp,b1,b2,c1,c2,d11,d12,d21,d22,ndata] = hinf_sp(p,nmeas,ncon);
np1 = ndata(1);
np2 = ndata(2);
nm1 = ndata(3);
nm2 = ndata(4);
%
% determine if   |A-jwI  b2 | has full column rank at w=0
%                |  c1   d12|
%
tmp_col=[ap b2;c1 d12];
[nr,nc]=size(tmp_col);
irank = rank(tmp_col,eps);
   if irank ~= nc
   fprintf('\n')
   disp('  [a b2;c1 d12] does not have full column rank at s=0 ')
   fail = 1;
   return
 end
%
% determine if   |A-jwI  b1 | has full row rank at w=0
%                |  c2   d21|
%
tmp_row=[ap b1;c2 d21];
[nr,nc]=size(tmp_row);
irank = rank(tmp_row,eps);
   if irank ~= nr
   fprintf('\n')
   disp('  [a b1;c2 d21] does not have full row rank at s=0 ')
   fail = 1;
   return
 end
%
%  scale the matrices to    q12*d12*r12 = | 0 |
%                                         | I |
%
[q12,r12] = qr(d12);
%
% determine if d12 has full column rank
%
irank = rank(r12,eps);
 if irank ~= nm2
   disp('  d12 does not have full column rank  ')
   fail = 1;
   return
 end
q12 = [q12(:,(nm2+1):np1),q12(:,1:nm2)]';
r12 = inv(r12(1:nm2,:));
%
%   r21*d21*q21 = [0 I]
%
[q21,r21] = qr(d21');
%
% determine if d21 has full column rank
%
 irank = rank(r21,eps);
 if irank ~= np2
   disp('  d21 does not have full row rank  ')
   fail = 1;
   return
 end
q21 = [q21(:,(np2+1):nm1),q21(:,1:np2)];
r21 = inv(r21(1:np2,:))';
c1 = q12*c1;
c2 = r21*c2;
cp = [c1;c2];
b1 = b1*q21;
b2 = b2*r12;
bp = [b1,b2];
d11 = q12*d11*q21;
d12 = q12*d12*r12;
d21 = r21*d21*q21;
d22 = d22;
dp = [d11 d12;d21 d22];
p = pck(ap,bp,cp,dp);

%---------------------------------------------------------------
%
% Copyright MUSYN INC 1991,  All Rights Reserved
