home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / languages / rlab1_23a / CTB / dsort < prev    next >
Text File  |  1995-11-14  |  1KB  |  51 lines

  1. //-------------------------------------------------------------------------------
  2. //
  3. // dsort
  4. //
  5. // Syntax: A=dsort(p)
  6. //
  7. //DSORT    Sort complex discrete eigenvalues in descending order.
  8. //
  9. //    S=DSORT(P) sorts the complex eigenvalues in the vector P in 
  10. //    descending order by magnitude.  The unstable eigenvalues will 
  11. //    appear first.
  12. //
  13. //    [S,NDX] = DSORT(P) also returns the vector NDX containing the 
  14. //    indexes used in the sort.
  15. //
  16. //-------------------------------------------------------------------------------
  17.  
  18. dsort = function(p)
  19. {
  20.    local(narg,a,ndx,i,k,s,swap)
  21.  
  22.    if (!exist(p)) {error("p doesn't exist");}
  23.  
  24.    if (p.nr == 1) {
  25.        p=p.';
  26.    }
  27.  
  28.    a=sort(-abs(p));
  29.    s=a.val;
  30.    ndx=a.ind';
  31.  
  32.    for (i in 1:p.nc) {
  33.         k=1;
  34.         while( k < length(s)) {
  35.               if (imag(s[k;i]) != 0) {
  36.                   if (imag(s[k;i]) < 0) {
  37.                       s[k:k+1;i]=conj(s[k:k+1;i]);
  38.                       swap=ndx[k;i];
  39.                       ndx[k;i]=ndx[k+1;i];
  40.                       ndx[k+1;i]=swap;
  41.                   }
  42.                   k=k+2;
  43.               else
  44.                   k=k+1;
  45.               }
  46.         }
  47.    }
  48.  
  49.    return << s=s; ndx=ndx >>
  50. };
  51.