function [v,t,m] = blkrsch2(a,odtype,cut)
%BLKRSCH Block-ordered real Schur form.
%
% [V,T,M] = BLKRSCH(A,ODTYPE,CUT) produces a real block-ordered
%       decomposition of a real matrix A
%
%               V'AV = T = |B1  B12|
%                          | 0   B2|
%
%               B1 = T(1:CUT,1:CUT)
%               B2 = T(CUT+1:N,CUT+1:N)
%
%     Six types of ordering can be selected:
%       
%         odtype = 1 --- Re(eig(B1)) < 0, Re(eig(B2)) > 0.
%         odtype = 2 --- Re(eig(B1)) > 0, Re(eig(B2)) < 0.
%         odtype = 3 --- Re(eig(B1)) > Re(eig(B2)).
%         odtype = 4 --- Re(eig(B1)) < Re(eig(B2)).
%         odtype = 5 --- abs(eig(B1)) > abs(eig(B2)).
%         odtype = 6 --- abs(eig(B1)) < abs(eig(B2)).
%

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

%
% ----- Ordered Complex Schur Decomposition :
%
[ra,ca] = size(a);
[qn,tn,m,swap] = cschur2(a,odtype);
%
% ----- Find the Orthonormal basis :
%
if odtype == 1
  cut = m;
end
%
if odtype == 2
  cut = ra-m;
end
%
kk = -1;
if cut == ra,     v = eye(ra);     t = a;     kk = 1;  end
if cut == 0.,     v = eye(ra);     t = a;     kk = 1;  end
if kk < 0
     qord = [real(qn(:,1:cut)) imag(qn(:,1:cut))];
     [v,r] = qr(qord);
     t = v' * a * v;
end
%
% ----- End of BLKRSCH.M ---- RYC/MGS 4/4/86 %