home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 4
/
DATAFILE_PDCD4.iso
/
languages
/
rlab1_23a
/
CTB
/
dsort
< prev
next >
Wrap
Text File
|
1995-11-14
|
1KB
|
51 lines
//-------------------------------------------------------------------------------
//
// dsort
//
// Syntax: A=dsort(p)
//
//DSORT Sort complex discrete eigenvalues in descending order.
//
// S=DSORT(P) sorts the complex eigenvalues in the vector P in
// descending order by magnitude. The unstable eigenvalues will
// appear first.
//
// [S,NDX] = DSORT(P) also returns the vector NDX containing the
// indexes used in the sort.
//
//-------------------------------------------------------------------------------
dsort = function(p)
{
local(narg,a,ndx,i,k,s,swap)
if (!exist(p)) {error("p doesn't exist");}
if (p.nr == 1) {
p=p.';
}
a=sort(-abs(p));
s=a.val;
ndx=a.ind';
for (i in 1:p.nc) {
k=1;
while( k < length(s)) {
if (imag(s[k;i]) != 0) {
if (imag(s[k;i]) < 0) {
s[k:k+1;i]=conj(s[k:k+1;i]);
swap=ndx[k;i];
ndx[k;i]=ndx[k+1;i];
ndx[k+1;i]=swap;
}
k=k+2;
else
k=k+1;
}
}
}
return << s=s; ndx=ndx >>
};