function [S,T,Q,Z] = qzswitch(i,S,T,Q,Z) % % Written by Chris Sims % Takes U.T. matrices S, T, orthonormal matrices Q,Z, interchanges % diagonal elements i and i+1 of both S and T, while maintaining % Q'SZ' and Q'TZ' unchanged. Does nothing if ratios of diagonal elements % in S and T at i and i+1 are the same. Aborts if diagonal elements of % both S and T are zero at either position. % a = S(i,i); d = T(i,i); b = S(i,i+1); e = T(i,i+1); c = S(i+1,i+1); f = T(i+1,i+1); wz = [c*e-f*b, (c*d-f*a)']; xy = [(b*d-e*a)', (c*d-f*a)']; n = sqrt(wz*wz'); m = sqrt(xy*xy'); if n == 0 return else wz = n\wz; xy = m\xy; wz = [wz; -wz(2)', wz(1)']; xy = [xy;-xy(2)', xy(1)']; S(i:i+1,:) = xy*S(i:i+1,:); T(i:i+1,:) = xy*T(i:i+1,:); S(:,i:i+1) = S(:,i:i+1)*wz; T(:,i:i+1) = T(:,i:i+1)*wz; Z(:,i:i+1) = Z(:,i:i+1)*wz; Q(i:i+1,:) = xy*Q(i:i+1,:); end