home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / r / rlab / CTB / eignm < prev    next >
Encoding:
Text File  |  1995-11-15  |  4.7 KB  |  179 lines

  1. //---------------------------------------------------------------------
  2. //
  3. // eignm
  4. //
  5. // Syntax: B=eignm(A,B)
  6. //
  7. // This routine solves the generalized eigenvalues problem given by,
  8. //
  9. //    A x = lambda B x
  10. //
  11. // It then orders and normalizes the eigenvectors according to the
  12. // following normalizations:
  13. //
  14. //    phi(i)'*B*phi(i) = 1
  15. //    psi(i)'*A*phi(j) = indrealnecker delta(i,j)
  16. //
  17. // where phi is the right eigenvector matrix, psi is the left eigenvector
  18. // matrix and where phi(i) is the ith right eigenvector and psi(i) is the
  19. // ith left eigenvector, indrealnecker delta is the Kronecker Delta function.
  20. //
  21. // The routine sorts them in the following order:
  22. //
  23. //   real eigenvalues (ascending order)
  24. //   comples eigenvalues (ascending order according to real part)
  25. //
  26. // where "real" is defined as imag(lambda) < 1.0e-12
  27. //
  28. // The results are returned in a list:
  29. //
  30. //     D.lambda = eigenvalues
  31. //     D.phi    = right eigenvectors
  32. //     D.psi    = left eigenvectors
  33. //
  34. // Ref:  Junkins and Kim, Dyn. & Ctrl of Structures, Introduction to Dynamics
  35. //       and Control of Flexible Structures, AIAA, 1993.
  36. //
  37. // Copyright (C), by Jeffrey B. Layton, 1994
  38. // Version JBL 940918
  39. //---------------------------------------------------------------------
  40.  
  41. eignm = function(A,B)
  42. {
  43.    local(n,lambda,phi,psi,LAMR,PHI,LAML,PSI,indreal,indcompx,lamreal,nargs,...
  44.          lamcompx,numr,numc,i,ic,ip,term1,Adum,indcompxs,indreals,phii,psii)
  45.  
  46. // Count number of inpute arguments
  47.    nargs=0;
  48.    if (exist(A)) {nargs=nargs+1;}
  49.    if (exist(B)) {nargs=nargs+1;}
  50.  
  51.    if (nargs < 2) {
  52.        error("EIGNM: Wrong number of input arguments.");
  53.    }
  54.  
  55. // Dimension Check
  56.    if (A.nr != B.nr) {
  57.        error("eign: A and B don't have the same number of rows.");
  58.    }
  59.  
  60. // Initialize needed matrices
  61. // Note: This RLaB from having to resize as elements are added.
  62.    n=max(size(A));
  63.    lambda=zeros(n,1);
  64.    phi=zeros(n,n);
  65.    psi=zeros(n,n);
  66.  
  67. // Solve Right Eigenvalues Problem.
  68.    Adum=eig(A,B);
  69.    LAMR=Adum.vec;
  70.    PHI=Adum.val.';
  71.  
  72. // Solve Left Eigenvalue Problem.
  73.    Adum=eig(A',B');
  74.    LAML=Adum.vec;
  75.    PSI=Adum.val.';
  76.    clear(Adum);
  77.  
  78. // Initialize sorted storage
  79. // Note: This keeps RLaB from having to resize as elements are added.
  80.    indreal=zeros(n,1);
  81.    indcompx=zeros(n,1);
  82.    lamreal=zeros(n,1);
  83.    lamcompx=zeros(n,1);
  84.  
  85. // Sort Right Eigenvectors and Eigenvalues
  86.    numr=0;
  87.    numc=0;
  88.    for (i in 1:n) {
  89.         if (abs(imag(PHI[i])) <= 1.e-12) {
  90.             numr=numr+1;
  91.             indreal[numr]=i;
  92.             lamreal[numr]=PHI[i];
  93.         else if (imag(PHI[i]) > 1.0e-12) {
  94.             numc=numc+1;
  95.             indcompx[numc]=i;
  96.             lamcompx[numc]=PHI[i];
  97.         }}
  98.    }
  99.  
  100. // Sort Real and Complex Right Eigenvalues
  101.    lamreal=lamreal[1:numr];
  102.    lamreal=real(lamreal);
  103.    lamcompx=lamcompx[1:numc];
  104.    indreals=sort(lamreal).ind;
  105.    indcompxs=sort(lamcompx).ind;
  106.  
  107. // Extract Sorted Right Eigenvectors and put them in output matrix
  108.    ic=1;
  109.    for (i in 1:(numr+numc)) {
  110.         if (i <= numr) {
  111.             phi[;i]=real(LAMR[;indreal[indreals[i]]]);
  112.             lambda[i]=real(PHI[indreal[indreals[i]]]);
  113.             ic=ic+1;
  114.         else
  115.             ip=i-numr;
  116.             phi[;ic]=LAMR[;indcompx[indcompxs[ip]]];
  117.             phi[;ic+1]=conj(phi[;ic]);
  118.             lambda[ic]=PHI[indcompx[indcompxs[ip]]];
  119.             lambda[ic+1]=conj(lambda[ic]);
  120.             ic=ic+2;
  121.         }
  122.    }
  123.  
  124. // Initialize sorted storage
  125. // Note: This RLaB from having to resize as elements are added.
  126.    indreal=zeros(n,1);
  127.    indcompx=zeros(n,1);
  128.    lamreal=zeros(n,1);
  129.    lamcompx=zeros(n,1);
  130.  
  131. // Sort Left Eigenvectors and "left" Eigenvalues.
  132.    numr=0;
  133.    numc=0;
  134.    for (i in 1:n) {
  135.         if (abs(imag(PSI[i])) <= 1.0e-12) {
  136.             numr=numr+1;
  137.             indreal[numr]=i;
  138.             lamreal[numr]=PSI[i];
  139.         else if (imag(PSI[i]) > 1.0e-12) {
  140.             numc=numc+1;
  141.             indcompx[numc]=i;
  142.             lamcompx[numc]=PSI[i];
  143.         }}
  144.    }
  145.  
  146. // Sort Real and Complex Left Eigenvalues
  147.    lamreal=lamreal[1:numr];
  148.    lamreal=real(lamreal);
  149.    lamcompx=lamcompx[1:numc];
  150.    indreals=sort(lamreal).ind;
  151.    indcompxs=sort(lamcompx).ind;
  152.  
  153. // Extract Sorted Left Eigenvectors and put them in output matrix
  154.    ic=1;
  155.    for (i in 1:numr+numc) {
  156.        if (i <= numr) {
  157.            psi[;i]=real(LAML[;indreal[indreals[i]]]);
  158.            ic=ic+1;
  159.        else
  160.            ip=i-numr;
  161.            psi[;ic]=LAML[;indcompx[indcompxs[ip]]];
  162.            psi[;ic+1]=conj(psi[;ic]);
  163.            ic=ic+2;
  164.       }
  165.    }
  166.  
  167. // Normalize the Sorted eigenvectors
  168.    for (i in 1:n) {
  169.         phii=phi[;i];
  170.         psii=psi[;i];
  171.         term1=conj(phii')*B*phii;
  172.         phi[;i]=phi[;i]/sqrt(term1);
  173.         term1=conj(psii')*B*phi[;i];
  174.         psi[;i]=phi[;i]/term1;
  175.    }
  176.  
  177.    return << lambda=lambda; phi=phi; psi=psi >>
  178. };
  179.