| 1 |
function [A,B,Q,Z] = qzdivct(stake,A,B,Q,Z) |
| 2 |
%function [A,B,Q,Z] = qzdivct(stake,A,B,Q,Z) |
| 3 |
% |
| 4 |
% Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them |
| 5 |
% so that all cases of real(B(i,i)/A(i,i))>stake are in lower right |
| 6 |
% corner, while preserving U.T. and orthonormal properties and Q'AZ' and |
| 7 |
% Q'BZ'. abs(A(i,i))<1e-11 is interpreted as a zero and as generating |
| 8 |
% an infinitely positive real part of the ratio. All i's for which this |
| 9 |
% criterion are satisfied are grouped together in the lower right corner |
| 10 |
% of the lower right corner, with the non-zero roots above them. This |
| 11 |
% version differs from |
| 12 |
% qzdiv in that it works on the real part's value, as is appropriate for |
| 13 |
% continuous time models, instead of on the absolute value, as is |
| 14 |
% appropriate for discrete time models. |
| 15 |
% |
| 16 |
% Copyright (C) 1997-2012 Tao Zha |
| 17 |
% |
| 18 |
% This free software: you can redistribute it and/or modify |
| 19 |
% it under the terms of the GNU General Public License as published by |
| 20 |
% the Free Software Foundation, either version 3 of the License, or |
| 21 |
% (at your option) any later version. |
| 22 |
% |
| 23 |
% It is distributed in the hope that it will be useful, |
| 24 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 |
% GNU General Public License for more details. |
| 27 |
% |
| 28 |
% If you did not received a copy of the GNU General Public License |
| 29 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 30 |
% |
| 31 |
|
| 32 |
realsmall=sqrt(eps)*10; |
| 33 |
%realsmall=1e-3; |
| 34 |
[n jnk] = size(A); |
| 35 |
root = [diag(A) diag(B)]; |
| 36 |
% first sort on the non-zero root criterion |
| 37 |
xdown0 = abs(root(:,1))<realsmall; |
| 38 |
xdown = (xdown0 | (real(root(:,2)./(xdown0+root(:,1))) > stake)); |
| 39 |
for i = n:-1:1 |
| 40 |
m=0; |
| 41 |
for j=i:-1:1 |
| 42 |
if xdown0(j) |
| 43 |
m=j; |
| 44 |
break |
| 45 |
end |
| 46 |
end |
| 47 |
if (m==0) |
| 48 |
break |
| 49 |
end |
| 50 |
for k=m:1:i-1 |
| 51 |
[A B Q Z] = qzswitch(k,A,B,Q,Z); |
| 52 |
root=[diag(A) diag(B)]; |
| 53 |
xdown0(k:k+1)=flipud(xdown0(k:k+1)); |
| 54 |
xdown(k:k+1)=flipud(xdown(k:k+1)); |
| 55 |
if any(xdown(k:k+1)~=(xdown0(k:k+1) | (real(root(k:k+1,2)./(xdown0(k:k+1)+root(k:k+1,1)))) > stake)) |
| 56 |
disp('xdown shift during 0 pack at i,k:') |
| 57 |
disp([i k]) |
| 58 |
end |
| 59 |
end |
| 60 |
end |
| 61 |
% now repeat, using the stake criterion |
| 62 |
for i = n:-1:1 |
| 63 |
m=0; |
| 64 |
for j=i:-1:1 |
| 65 |
if xdown(j) |
| 66 |
m=j; |
| 67 |
break |
| 68 |
end |
| 69 |
end |
| 70 |
if (m==0) |
| 71 |
return |
| 72 |
end |
| 73 |
for k=m:1:i-1 |
| 74 |
gevOld=root(k:k+1,:); |
| 75 |
[A B Q Z] = qzswitch(k,A,B,Q,Z); |
| 76 |
root=[diag(A) diag(B)]; |
| 77 |
xdown0(k:k+1)=flipud(xdown0(k:k+1)); |
| 78 |
xdown(k:k+1)=flipud(xdown(k:k+1)); |
| 79 |
if any(xdown(k:k+1)~=(xdown0(k:k+1) | (real(root(k:k+1,2)./(xdown0(k:k+1)+root(k:k+1,1)))) > stake)) |
| 80 |
disp('xdown shift during pos pack at i,k:') |
| 81 |
disp([i k]) |
| 82 |
gev=root(k:k+1,:); |
| 83 |
[gevOld gevOld(:,1).\gevOld(:,2);gev gev(:,1).\gev(:,2)] |
| 84 |
end |
| 85 |
end |
| 86 |
end |