还有,我也用这个:
% Written by: Yunkai Zhou (周云开),
yzhou@smu.edu
% Assistant Professor, Department of Mathematics, Southern Methodist University
% 2005,06,25
%
% this script is adapted from Gerard Sleijpen's jdqr.m
%
function [Q, S, indx]=sortschur_old(Q, S, sigma, kk)
n = size(S,1);
if n<2,
indx=1; return,
elseif nargin<4, kk=n-1; end
if nargin<=4,
kk=min(kk,n-1);
else
sigma=sigma(1,:);
end
% transform real schur form to complex schur form if necessary
if norm(tril(S,-1),1)>0, [Q,S]=rsf2csf(Q, S); end
%------ find order eigenvalues ---------------
indx = SortEig(diag(S),sigma);
%------ reorder schur form -------------------
[Q, S] = SwapSchur(Q, S, indx(1:kk));
return
%----------------------------------------------------------------------
function I=SortEig(t,sigma);
%
% I=SortEig(T,SIGMA) sorts T according to SIGMA.
%
% T is a vector of scalars,
% SIGMA is a string or a vector of scalars.
% I is a permutation of (1:LENGTH(T))' such that:
% if SIGMA is a vector of scalars then
% for K=1,2,...,LENGTH(T) with KK = MIN(K,SIZE(SIGMA,1))
% ABS( T(I(K))-SIGMA(KK) ) <= ABS( T(I(J))-SIGMA(KK) )
% SIGMA(kk)=INF: ABS( T(I(K)) ) >= ABS( T(I(J)) )
% for all J >= K
if ischar(sigma)
switch sigma
case 'LM'
[s,I]=sort(-abs(t));
case 'SM'
[s,I]=sort(abs(t));
case 'LR'
[s,I]=sort(-real(t));
case 'SR'
[s,I]=sort(real(t));
case 'BE'
[s,I]=sort(real(t)); I=twistdim(I,1);
case 'LI'
[s,I]=sort(-imag(t));
case 'SI'
[s,I]=sort(imag(t));
end
else
[s,I]=sort(abs(t-sigma(1,1)));
ll=min(size(sigma,1),size(t,1)-1);
for j=2:ll
if sigma(j,1)==inf
[s,J]=sort(abs(t(I(j:end)))); J=flipdim(J,1);
else
[s,J]=sort(abs(t(I(j:end))-sigma(j,1)));
end
I=[I(1:j-1);I(J+j-1)];
end
end
return
%----------------------------------------------------------------------
function t=twistdim(t,k)
d=size(t,k); J=1:d; J0=zeros(1,2*d);
J0(1,2*J)=J; J0(1,2*J-1)=flipdim(J,2); I=J0(1,J);
if k==1, t=t(I,:); else, t=t(:,I); end
return
%----------------------------------------------------------------------
function [Q,S]=SwapSchur(Q,S,I)
% [Q,S]=SwapSchur(QQ,SS,P)
% QQ and SS are square matrices of size K by K
% P is the first part of a permutation of (1:K)'.
%
% If M = QQ*SS*QQ' and QQ'*QQ = EYE(K), SS upper triangular
% then M*Q = Q*S with Q'*Q = EYE(K), S upper triangular
% and D(1:LENGTH(P))=DD(P) where D=diag(S), DD=diag(SS)
%
% Computation uses Givens rotations. (similar to G.V. 1996, p. 366)
%
% note that using SwapSchur for symmetric problems is not necessary,
% symmetric problems only require direct swap by assignment
%
kk=min(length(I),size(S,1)-1);
j=1; while (j<=kk & j==I(j)), j=j+1; end;
while j<=kk
i=I(j);
for k=i-1:-1:j
q = [S(k,k)-S(k+1,k+1),S(k,k+1)];
if q(1) ~= 0
q = q/norm(q);
G = [[q(2);-q(1)],q'];
J = [k,k+1];
Q(:,J) = Q(:,J)*G;
S(:,J) = S(:,J)*G;
S(J,:) = G'*S(J,:);
end
S(k+1,k) = 0;
end
I=I+(I<i);
j=j+1; while (j<=kk & j==I(j)), j=j+1; end
end
return