function [v,t,m] = blkrsch(a,Type,cut)
%
% Block-Ordered Real Schur Form
%
% [V,T,M] = BLKRSCH(A,TYPE,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:
%       
%         Type = 1 --- Re(eig(B1)) < 0, Re(eig(B2)) > 0.
%         Type = 2 --- Re(eig(B1)) > 0, Re(eig(B2)) < 0.
%         Type = 3 --- Re(eig(B1)) > Re(eig(B2)).
%         Type = 4 --- Re(eig(B1)) < Re(eig(B2)).
%         Type = 5 --- abs(eig(B1)) > abs(eig(B2)).
%         Type = 6 --- abs(eig(B1)) < abs(eig(B2)).
%

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

%
% ----- Ordered Complex Schur Decomposition :
%
[ra,ca] = size(a);
[qn,tn,m,swap] = cschur(a,Type);
%
% ----- Find the Orthonomal basis :
%
if Type == 1
  cut = m;
end
%
if Type == 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 %