function [ahed,bhed,ched,dhed,aug,hsv] = reschmr(A,B,C,D,Type,no,info)
%
% [AHED,BHED,CHED,DHED,AUG,HSV]=RESCHMR(A,B,C,D,TYPE,NO,INFO) performs 
%    relative error Schur model reduction on a SQRARE, STABLE G(s):=
%    (A,B,C,D). The infinity-norm of the relative error is bounded as
%              -1                          __
%            |G (s)(Gm(s)-G(s))|    <= (   ||  [(1+si)/(1-si)] ) - 1,
%                               inf     k+1 to n
%    where si denotes the i-th Hankel singular value of the all-pass 
%    "phase matrix" of G(s).
%    The algorithm is based on the Balanced Stochastic Truncation (BST) 
%    theory with Relative Error Method (REM).
%    Based on the "TYPE" selected, you have the following options:
%     1). TYPE = 1  --- no: size "k" of the reduced order model.
%     2). TYPE = 2  --- find k-th order reduced model that 
%                       tolerance (db) <= "no".
%     3). TYPE = 3  --- display all the Hankel SV of phase matrix and 
%                   prompt for "k" (in this case, no need to specify "no").
%    Input variable: "info" = 'left '; or 'right' (default value)
%    Output variable "aug": aug(1,1) = no. of state removed
%                           aug(1,2) = relative error bound
%    Note that if D is not full rank, the problem is ill-posed.

% R. Y. Chiang & M. G. Safonov 2/30/88
% Copyright (c) 1988 by the MathWorks, Inc.
% All Rights Reserved.
% ---------------------------------------------------------------------------

[rd,cd] = size(D);
if nargin == 6
   info = 'right';
end
%
if (info == 'left ') | (rd < cd)
   A = A'; temp = B; B = C'; C = temp'; D = D';
end
%
if rank(D*D') < min([rd, cd])
  disp('   WARNING: D MATRIX IS NOT FULL RANK - - -');
  disp('            THE PROBLEM IS ILL-CONDITIONED !');
  return
end
%
disp(' ')
disp('        - - Working on Schur BST-REM model reduction - -');
[ma,na] = size(A);
Flag = exist('xxxxx0');
P = gram(A,B);
G = P*C'+B*D';
phi = D*D';
%
qrnQ = [zeros(ma) -C';-C -phi];
[K,Q,Qerr] = lqrc(A,G,qrnQ);
%
lambda = eig(P*Q);
hsv = sqrt(lambda);
[hsv,index] = sort(real(hsv));
hsv = hsv(ma:-1:1,:);
%
% ------ Model reduction based on your choice of TYPE:
%
if Type == 1
   kk = no+1;
end
%
if Type == 2
   toldb = no;    %in db
   tolabs = 10^(toldb/20);
   resig = 1;
   for i = ma:-1:1
      resig = resig*((1+hsv(i))/(1-hsv(i)));
      rem = resig -1; 
      if rem > tolabs
         kk = i+1;
         break
      end
   end
end
%
if Type == 3
   format short e
   format compact
   [mhsv,nhsv] = size(hsv);
   if mhsv < 60
      disp('    Hankel Singular Values:')
      hsv'
      kk = input('Please assign the k-th index for k-th order model reduction:');
   else
      disp('    Hankel Singular Values:')
      hsv(1:60,:)'
      disp('                              (strike a key for more ...)')
      pause
      hsv(61:mhsv,:)'
      kk = input('Please assign the k-th index for k-th order model reduction:');
   end
   format loose
   kk = kk + 1;
end
%
% ------ Save all the states:
%
if kk > na 
   ahed = a; bhed = b; ched = c; dhed = d;
   aug = [0 0];
   return 
end
%
% ------ Disgard all the states:
%
if kk < 1 
   ahed = zeros(ma,na);   bhed = zeros(ma,nd);
   ched = zeros(md,na);   dhed = zeros(md,nd);
   resig = 1;
   for i = 1:na
      resig = resig*(1+hsv(i))/(1-hsv(i));
   end
   rem = resig-1;
   aug = [na rem];
   return
end 
%
% ------ Computing the relative error bound:
%
resig = 1;
for i = kk:na
    resig = resig*(1+hsv(i))/(1-hsv(i));
end
rem = resig-1;
strm = na-kk+1;
aug = [strm rem];
% 
% ---------------------- Start Model Reduction:
%
% ------ Find the left-eigenspace basis:
%
ro = (hsv(kk-1)^2+hsv(kk)^2)/2.;
gammaa = P*Q-ro*eye(na);
if Flag == 2
   [va,ta] = rschur(gammaa,1);            % FORTRAN code
else
   [va,ta,msa,swpa] = hqr10(gammaa);
end
vlbig = va(:,(na-kk+2):na);
%
% ------ Find the right-eigenspace basis:
%
gammad = -gammaa;
if Flag == 2
   [vd,td] = rschur(gammad,1);           % FORTRAN code
else
   [vd,td,msd,swpd] = hqr10(gammad);
end
vrbig = vd(:,1:kk-1);
%
% ------ Find the similarity transformation:
%
ee = vlbig'*vrbig;
[ue,se,ve] = svd(ee);
%
seih = diag(ones(kk-1,1)./sqrt(diag(se)));
slbig = vlbig*ue*seih;
srbig = vrbig*ve*seih;
%
ahed = slbig'*A*srbig;
bhed = slbig'*B;
ched = C*srbig;
dhed = D;
%
if (info == 'left ') | (rd < cd)
  ahed = ahed'; temp = bhed; 
  bhed = ched'; ched = temp'; dhed = dhed';
end
%
disp(' ')
disp(['               ' int2str(aug(1,1)), '    states removed !!'])
%
% ------ End of RESCHMR.M --- RYC/MGS %
