| 1 |
function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z) |
| 2 |
%function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z) |
| 3 |
% |
| 4 |
% Takes U.T. matrices A, B, orthonormal matrices Q,Z, interchanges |
| 5 |
% diagonal elements i and i+1 of both A and B, while maintaining |
| 6 |
% Q'AZ' and Q'BZ' unchanged. If diagonal elements of A and B |
| 7 |
% are zero at matching positions, the returned A will have zeros at both |
| 8 |
% positions on the diagonal. This is natural behavior if this routine is used |
| 9 |
% to drive all zeros on the diagonal of A to the lower right, but in this case |
| 10 |
% the qz transformation is not unique and it is not possible simply to switch |
| 11 |
% the positions of the diagonal elements of both A and B. |
| 12 |
% |
| 13 |
% Copyright (C) 1997-2012 Tao Zha |
| 14 |
% |
| 15 |
% This free software: you can redistribute it and/or modify |
| 16 |
% it under the terms of the GNU General Public License as published by |
| 17 |
% the Free Software Foundation, either version 3 of the License, or |
| 18 |
% (at your option) any later version. |
| 19 |
% |
| 20 |
% It is distributed in the hope that it will be useful, |
| 21 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
% GNU General Public License for more details. |
| 24 |
% |
| 25 |
% If you did not received a copy of the GNU General Public License |
| 26 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 27 |
% |
| 28 |
|
| 29 |
realsmall=sqrt(eps)*10; |
| 30 |
%realsmall=1e-3; |
| 31 |
a = A(i,i); d = B(i,i); b = A(i,i+1); e = B(i,i+1); |
| 32 |
c = A(i+1,i+1); f = B(i+1,i+1); |
| 33 |
% A(i:i+1,i:i+1)=[a b; 0 c]; |
| 34 |
% B(i:i+1,i:i+1)=[d e; 0 f]; |
| 35 |
if (abs(c)<realsmall & abs(f)<realsmall) |
| 36 |
if abs(a)<realsmall |
| 37 |
% l.r. coincident 0's with u.l. of A=0; do nothing |
| 38 |
return |
| 39 |
else |
| 40 |
% l.r. coincident zeros; put 0 in u.l. of a |
| 41 |
wz=[b; -a]; |
| 42 |
wz=wz/sqrt(wz'*wz); |
| 43 |
wz=[wz [wz(2)';-wz(1)'] ]; |
| 44 |
xy=eye(2); |
| 45 |
end |
| 46 |
elseif (abs(a)<realsmall & abs(d)<realsmall) |
| 47 |
if abs(c)<realsmall |
| 48 |
% u.l. coincident zeros with l.r. of A=0; do nothing |
| 49 |
return |
| 50 |
else |
| 51 |
% u.l. coincident zeros; put 0 in l.r. of A |
| 52 |
wz=eye(2); |
| 53 |
xy=[c -b]; |
| 54 |
xy=xy/sqrt(xy*xy'); |
| 55 |
xy=[[xy(2)' -xy(1)'];xy]; |
| 56 |
end |
| 57 |
else |
| 58 |
% usual case |
| 59 |
wz = [c*e-f*b, (c*d-f*a)']; |
| 60 |
xy = [(b*d-e*a)', (c*d-f*a)']; |
| 61 |
n = sqrt(wz*wz'); |
| 62 |
m = sqrt(xy*xy'); |
| 63 |
if m<eps*100 |
| 64 |
% all elements of A and B proportional |
| 65 |
return |
| 66 |
end |
| 67 |
wz = n\wz; |
| 68 |
xy = m\xy; |
| 69 |
wz = [wz; -wz(2)', wz(1)']; |
| 70 |
xy = [xy;-xy(2)', xy(1)']; |
| 71 |
end |
| 72 |
A(i:i+1,:) = xy*A(i:i+1,:); |
| 73 |
B(i:i+1,:) = xy*B(i:i+1,:); |
| 74 |
A(:,i:i+1) = A(:,i:i+1)*wz; |
| 75 |
B(:,i:i+1) = B(:,i:i+1)*wz; |
| 76 |
Z(:,i:i+1) = Z(:,i:i+1)*wz; |
| 77 |
Q(i:i+1,:) = xy*Q(i:i+1,:); |