cgeev.c
Go to the documentation of this file.
00001 /* cgeev.f -- translated by f2c (version 20061008).
00002    You must link the resulting object file with libf2c:
00003         on Microsoft Windows system, link with libf2c.lib;
00004         on Linux or Unix systems, link with .../path/to/libf2c.a -lm
00005         or, if you install libf2c.a in a standard place, with -lf2c -lm
00006         -- in that order, at the end of the command line, as in
00007                 cc *.o -lf2c -lm
00008         Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
00009 
00010                 http://www.netlib.org/f2c/libf2c.zip
00011 */
00012 
00013 #include "f2c.h"
00014 #include "blaswrap.h"
00015 
00016 /* Table of constant values */
00017 
00018 static integer c__1 = 1;
00019 static integer c__0 = 0;
00020 static integer c_n1 = -1;
00021 
00022 /* Subroutine */ int cgeev_(char *jobvl, char *jobvr, integer *n, complex *a, 
00023         integer *lda, complex *w, complex *vl, integer *ldvl, complex *vr, 
00024         integer *ldvr, complex *work, integer *lwork, real *rwork, integer *
00025         info)
00026 {
00027     /* System generated locals */
00028     integer a_dim1, a_offset, vl_dim1, vl_offset, vr_dim1, vr_offset, i__1, 
00029             i__2, i__3;
00030     real r__1, r__2;
00031     complex q__1, q__2;
00032 
00033     /* Builtin functions */
00034     double sqrt(doublereal), r_imag(complex *);
00035     void r_cnjg(complex *, complex *);
00036 
00037     /* Local variables */
00038     integer i__, k, ihi;
00039     real scl;
00040     integer ilo;
00041     real dum[1], eps;
00042     complex tmp;
00043     integer ibal;
00044     char side[1];
00045     real anrm;
00046     integer ierr, itau, iwrk, nout;
00047     extern /* Subroutine */ int cscal_(integer *, complex *, complex *, 
00048             integer *);
00049     extern logical lsame_(char *, char *);
00050     extern doublereal scnrm2_(integer *, complex *, integer *);
00051     extern /* Subroutine */ int cgebak_(char *, char *, integer *, integer *, 
00052             integer *, real *, integer *, complex *, integer *, integer *), cgebal_(char *, integer *, complex *, integer *, 
00053             integer *, integer *, real *, integer *), slabad_(real *, 
00054             real *);
00055     logical scalea;
00056     extern doublereal clange_(char *, integer *, integer *, complex *, 
00057             integer *, real *);
00058     real cscale;
00059     extern /* Subroutine */ int cgehrd_(integer *, integer *, integer *, 
00060             complex *, integer *, complex *, complex *, integer *, integer *),
00061              clascl_(char *, integer *, integer *, real *, real *, integer *, 
00062             integer *, complex *, integer *, integer *);
00063     extern doublereal slamch_(char *);
00064     extern /* Subroutine */ int csscal_(integer *, real *, complex *, integer 
00065             *), clacpy_(char *, integer *, integer *, complex *, integer *, 
00066             complex *, integer *), xerbla_(char *, integer *);
00067     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
00068             integer *, integer *);
00069     logical select[1];
00070     real bignum;
00071     extern integer isamax_(integer *, real *, integer *);
00072     extern /* Subroutine */ int chseqr_(char *, char *, integer *, integer *, 
00073             integer *, complex *, integer *, complex *, complex *, integer *, 
00074             complex *, integer *, integer *), ctrevc_(char *, 
00075             char *, logical *, integer *, complex *, integer *, complex *, 
00076             integer *, complex *, integer *, integer *, integer *, complex *, 
00077             real *, integer *), cunghr_(integer *, integer *, 
00078             integer *, complex *, integer *, complex *, complex *, integer *, 
00079             integer *);
00080     integer minwrk, maxwrk;
00081     logical wantvl;
00082     real smlnum;
00083     integer hswork, irwork;
00084     logical lquery, wantvr;
00085 
00086 
00087 /*  -- LAPACK driver routine (version 3.2) -- */
00088 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00089 /*     November 2006 */
00090 
00091 /*     .. Scalar Arguments .. */
00092 /*     .. */
00093 /*     .. Array Arguments .. */
00094 /*     .. */
00095 
00096 /*  Purpose */
00097 /*  ======= */
00098 
00099 /*  CGEEV computes for an N-by-N complex nonsymmetric matrix A, the */
00100 /*  eigenvalues and, optionally, the left and/or right eigenvectors. */
00101 
00102 /*  The right eigenvector v(j) of A satisfies */
00103 /*                   A * v(j) = lambda(j) * v(j) */
00104 /*  where lambda(j) is its eigenvalue. */
00105 /*  The left eigenvector u(j) of A satisfies */
00106 /*                u(j)**H * A = lambda(j) * u(j)**H */
00107 /*  where u(j)**H denotes the conjugate transpose of u(j). */
00108 
00109 /*  The computed eigenvectors are normalized to have Euclidean norm */
00110 /*  equal to 1 and largest component real. */
00111 
00112 /*  Arguments */
00113 /*  ========= */
00114 
00115 /*  JOBVL   (input) CHARACTER*1 */
00116 /*          = 'N': left eigenvectors of A are not computed; */
00117 /*          = 'V': left eigenvectors of are computed. */
00118 
00119 /*  JOBVR   (input) CHARACTER*1 */
00120 /*          = 'N': right eigenvectors of A are not computed; */
00121 /*          = 'V': right eigenvectors of A are computed. */
00122 
00123 /*  N       (input) INTEGER */
00124 /*          The order of the matrix A. N >= 0. */
00125 
00126 /*  A       (input/output) COMPLEX array, dimension (LDA,N) */
00127 /*          On entry, the N-by-N matrix A. */
00128 /*          On exit, A has been overwritten. */
00129 
00130 /*  LDA     (input) INTEGER */
00131 /*          The leading dimension of the array A.  LDA >= max(1,N). */
00132 
00133 /*  W       (output) COMPLEX array, dimension (N) */
00134 /*          W contains the computed eigenvalues. */
00135 
00136 /*  VL      (output) COMPLEX array, dimension (LDVL,N) */
00137 /*          If JOBVL = 'V', the left eigenvectors u(j) are stored one */
00138 /*          after another in the columns of VL, in the same order */
00139 /*          as their eigenvalues. */
00140 /*          If JOBVL = 'N', VL is not referenced. */
00141 /*          u(j) = VL(:,j), the j-th column of VL. */
00142 
00143 /*  LDVL    (input) INTEGER */
00144 /*          The leading dimension of the array VL.  LDVL >= 1; if */
00145 /*          JOBVL = 'V', LDVL >= N. */
00146 
00147 /*  VR      (output) COMPLEX array, dimension (LDVR,N) */
00148 /*          If JOBVR = 'V', the right eigenvectors v(j) are stored one */
00149 /*          after another in the columns of VR, in the same order */
00150 /*          as their eigenvalues. */
00151 /*          If JOBVR = 'N', VR is not referenced. */
00152 /*          v(j) = VR(:,j), the j-th column of VR. */
00153 
00154 /*  LDVR    (input) INTEGER */
00155 /*          The leading dimension of the array VR.  LDVR >= 1; if */
00156 /*          JOBVR = 'V', LDVR >= N. */
00157 
00158 /*  WORK    (workspace/output) COMPLEX array, dimension (MAX(1,LWORK)) */
00159 /*          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
00160 
00161 /*  LWORK   (input) INTEGER */
00162 /*          The dimension of the array WORK.  LWORK >= max(1,2*N). */
00163 /*          For good performance, LWORK must generally be larger. */
00164 
00165 /*          If LWORK = -1, then a workspace query is assumed; the routine */
00166 /*          only calculates the optimal size of the WORK array, returns */
00167 /*          this value as the first entry of the WORK array, and no error */
00168 /*          message related to LWORK is issued by XERBLA. */
00169 
00170 /*  RWORK   (workspace) REAL array, dimension (2*N) */
00171 
00172 /*  INFO    (output) INTEGER */
00173 /*          = 0:  successful exit */
00174 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
00175 /*          > 0:  if INFO = i, the QR algorithm failed to compute all the */
00176 /*                eigenvalues, and no eigenvectors have been computed; */
00177 /*                elements and i+1:N of W contain eigenvalues which have */
00178 /*                converged. */
00179 
00180 /*  ===================================================================== */
00181 
00182 /*     .. Parameters .. */
00183 /*     .. */
00184 /*     .. Local Scalars .. */
00185 /*     .. */
00186 /*     .. Local Arrays .. */
00187 /*     .. */
00188 /*     .. External Subroutines .. */
00189 /*     .. */
00190 /*     .. External Functions .. */
00191 /*     .. */
00192 /*     .. Intrinsic Functions .. */
00193 /*     .. */
00194 /*     .. Executable Statements .. */
00195 
00196 /*     Test the input arguments */
00197 
00198     /* Parameter adjustments */
00199     a_dim1 = *lda;
00200     a_offset = 1 + a_dim1;
00201     a -= a_offset;
00202     --w;
00203     vl_dim1 = *ldvl;
00204     vl_offset = 1 + vl_dim1;
00205     vl -= vl_offset;
00206     vr_dim1 = *ldvr;
00207     vr_offset = 1 + vr_dim1;
00208     vr -= vr_offset;
00209     --work;
00210     --rwork;
00211 
00212     /* Function Body */
00213     *info = 0;
00214     lquery = *lwork == -1;
00215     wantvl = lsame_(jobvl, "V");
00216     wantvr = lsame_(jobvr, "V");
00217     if (! wantvl && ! lsame_(jobvl, "N")) {
00218         *info = -1;
00219     } else if (! wantvr && ! lsame_(jobvr, "N")) {
00220         *info = -2;
00221     } else if (*n < 0) {
00222         *info = -3;
00223     } else if (*lda < max(1,*n)) {
00224         *info = -5;
00225     } else if (*ldvl < 1 || wantvl && *ldvl < *n) {
00226         *info = -8;
00227     } else if (*ldvr < 1 || wantvr && *ldvr < *n) {
00228         *info = -10;
00229     }
00230 
00231 /*     Compute workspace */
00232 /*      (Note: Comments in the code beginning "Workspace:" describe the */
00233 /*       minimal amount of workspace needed at that point in the code, */
00234 /*       as well as the preferred amount for good performance. */
00235 /*       CWorkspace refers to complex workspace, and RWorkspace to real */
00236 /*       workspace. NB refers to the optimal block size for the */
00237 /*       immediately following subroutine, as returned by ILAENV. */
00238 /*       HSWORK refers to the workspace preferred by CHSEQR, as */
00239 /*       calculated below. HSWORK is computed assuming ILO=1 and IHI=N, */
00240 /*       the worst case.) */
00241 
00242     if (*info == 0) {
00243         if (*n == 0) {
00244             minwrk = 1;
00245             maxwrk = 1;
00246         } else {
00247             maxwrk = *n + *n * ilaenv_(&c__1, "CGEHRD", " ", n, &c__1, n, &
00248                     c__0);
00249             minwrk = *n << 1;
00250             if (wantvl) {
00251 /* Computing MAX */
00252                 i__1 = maxwrk, i__2 = *n + (*n - 1) * ilaenv_(&c__1, "CUNGHR", 
00253                          " ", n, &c__1, n, &c_n1);
00254                 maxwrk = max(i__1,i__2);
00255                 chseqr_("S", "V", n, &c__1, n, &a[a_offset], lda, &w[1], &vl[
00256                         vl_offset], ldvl, &work[1], &c_n1, info);
00257             } else if (wantvr) {
00258 /* Computing MAX */
00259                 i__1 = maxwrk, i__2 = *n + (*n - 1) * ilaenv_(&c__1, "CUNGHR", 
00260                          " ", n, &c__1, n, &c_n1);
00261                 maxwrk = max(i__1,i__2);
00262                 chseqr_("S", "V", n, &c__1, n, &a[a_offset], lda, &w[1], &vr[
00263                         vr_offset], ldvr, &work[1], &c_n1, info);
00264             } else {
00265                 chseqr_("E", "N", n, &c__1, n, &a[a_offset], lda, &w[1], &vr[
00266                         vr_offset], ldvr, &work[1], &c_n1, info);
00267             }
00268             hswork = work[1].r;
00269 /* Computing MAX */
00270             i__1 = max(maxwrk,hswork);
00271             maxwrk = max(i__1,minwrk);
00272         }
00273         work[1].r = (real) maxwrk, work[1].i = 0.f;
00274 
00275         if (*lwork < minwrk && ! lquery) {
00276             *info = -12;
00277         }
00278     }
00279 
00280     if (*info != 0) {
00281         i__1 = -(*info);
00282         xerbla_("CGEEV ", &i__1);
00283         return 0;
00284     } else if (lquery) {
00285         return 0;
00286     }
00287 
00288 /*     Quick return if possible */
00289 
00290     if (*n == 0) {
00291         return 0;
00292     }
00293 
00294 /*     Get machine constants */
00295 
00296     eps = slamch_("P");
00297     smlnum = slamch_("S");
00298     bignum = 1.f / smlnum;
00299     slabad_(&smlnum, &bignum);
00300     smlnum = sqrt(smlnum) / eps;
00301     bignum = 1.f / smlnum;
00302 
00303 /*     Scale A if max element outside range [SMLNUM,BIGNUM] */
00304 
00305     anrm = clange_("M", n, n, &a[a_offset], lda, dum);
00306     scalea = FALSE_;
00307     if (anrm > 0.f && anrm < smlnum) {
00308         scalea = TRUE_;
00309         cscale = smlnum;
00310     } else if (anrm > bignum) {
00311         scalea = TRUE_;
00312         cscale = bignum;
00313     }
00314     if (scalea) {
00315         clascl_("G", &c__0, &c__0, &anrm, &cscale, n, n, &a[a_offset], lda, &
00316                 ierr);
00317     }
00318 
00319 /*     Balance the matrix */
00320 /*     (CWorkspace: none) */
00321 /*     (RWorkspace: need N) */
00322 
00323     ibal = 1;
00324     cgebal_("B", n, &a[a_offset], lda, &ilo, &ihi, &rwork[ibal], &ierr);
00325 
00326 /*     Reduce to upper Hessenberg form */
00327 /*     (CWorkspace: need 2*N, prefer N+N*NB) */
00328 /*     (RWorkspace: none) */
00329 
00330     itau = 1;
00331     iwrk = itau + *n;
00332     i__1 = *lwork - iwrk + 1;
00333     cgehrd_(n, &ilo, &ihi, &a[a_offset], lda, &work[itau], &work[iwrk], &i__1, 
00334              &ierr);
00335 
00336     if (wantvl) {
00337 
00338 /*        Want left eigenvectors */
00339 /*        Copy Householder vectors to VL */
00340 
00341         *(unsigned char *)side = 'L';
00342         clacpy_("L", n, n, &a[a_offset], lda, &vl[vl_offset], ldvl)
00343                 ;
00344 
00345 /*        Generate unitary matrix in VL */
00346 /*        (CWorkspace: need 2*N-1, prefer N+(N-1)*NB) */
00347 /*        (RWorkspace: none) */
00348 
00349         i__1 = *lwork - iwrk + 1;
00350         cunghr_(n, &ilo, &ihi, &vl[vl_offset], ldvl, &work[itau], &work[iwrk], 
00351                  &i__1, &ierr);
00352 
00353 /*        Perform QR iteration, accumulating Schur vectors in VL */
00354 /*        (CWorkspace: need 1, prefer HSWORK (see comments) ) */
00355 /*        (RWorkspace: none) */
00356 
00357         iwrk = itau;
00358         i__1 = *lwork - iwrk + 1;
00359         chseqr_("S", "V", n, &ilo, &ihi, &a[a_offset], lda, &w[1], &vl[
00360                 vl_offset], ldvl, &work[iwrk], &i__1, info);
00361 
00362         if (wantvr) {
00363 
00364 /*           Want left and right eigenvectors */
00365 /*           Copy Schur vectors to VR */
00366 
00367             *(unsigned char *)side = 'B';
00368             clacpy_("F", n, n, &vl[vl_offset], ldvl, &vr[vr_offset], ldvr);
00369         }
00370 
00371     } else if (wantvr) {
00372 
00373 /*        Want right eigenvectors */
00374 /*        Copy Householder vectors to VR */
00375 
00376         *(unsigned char *)side = 'R';
00377         clacpy_("L", n, n, &a[a_offset], lda, &vr[vr_offset], ldvr)
00378                 ;
00379 
00380 /*        Generate unitary matrix in VR */
00381 /*        (CWorkspace: need 2*N-1, prefer N+(N-1)*NB) */
00382 /*        (RWorkspace: none) */
00383 
00384         i__1 = *lwork - iwrk + 1;
00385         cunghr_(n, &ilo, &ihi, &vr[vr_offset], ldvr, &work[itau], &work[iwrk], 
00386                  &i__1, &ierr);
00387 
00388 /*        Perform QR iteration, accumulating Schur vectors in VR */
00389 /*        (CWorkspace: need 1, prefer HSWORK (see comments) ) */
00390 /*        (RWorkspace: none) */
00391 
00392         iwrk = itau;
00393         i__1 = *lwork - iwrk + 1;
00394         chseqr_("S", "V", n, &ilo, &ihi, &a[a_offset], lda, &w[1], &vr[
00395                 vr_offset], ldvr, &work[iwrk], &i__1, info);
00396 
00397     } else {
00398 
00399 /*        Compute eigenvalues only */
00400 /*        (CWorkspace: need 1, prefer HSWORK (see comments) ) */
00401 /*        (RWorkspace: none) */
00402 
00403         iwrk = itau;
00404         i__1 = *lwork - iwrk + 1;
00405         chseqr_("E", "N", n, &ilo, &ihi, &a[a_offset], lda, &w[1], &vr[
00406                 vr_offset], ldvr, &work[iwrk], &i__1, info);
00407     }
00408 
00409 /*     If INFO > 0 from CHSEQR, then quit */
00410 
00411     if (*info > 0) {
00412         goto L50;
00413     }
00414 
00415     if (wantvl || wantvr) {
00416 
00417 /*        Compute left and/or right eigenvectors */
00418 /*        (CWorkspace: need 2*N) */
00419 /*        (RWorkspace: need 2*N) */
00420 
00421         irwork = ibal + *n;
00422         ctrevc_(side, "B", select, n, &a[a_offset], lda, &vl[vl_offset], ldvl, 
00423                  &vr[vr_offset], ldvr, n, &nout, &work[iwrk], &rwork[irwork], 
00424                 &ierr);
00425     }
00426 
00427     if (wantvl) {
00428 
00429 /*        Undo balancing of left eigenvectors */
00430 /*        (CWorkspace: none) */
00431 /*        (RWorkspace: need N) */
00432 
00433         cgebak_("B", "L", n, &ilo, &ihi, &rwork[ibal], n, &vl[vl_offset], 
00434                 ldvl, &ierr);
00435 
00436 /*        Normalize left eigenvectors and make largest component real */
00437 
00438         i__1 = *n;
00439         for (i__ = 1; i__ <= i__1; ++i__) {
00440             scl = 1.f / scnrm2_(n, &vl[i__ * vl_dim1 + 1], &c__1);
00441             csscal_(n, &scl, &vl[i__ * vl_dim1 + 1], &c__1);
00442             i__2 = *n;
00443             for (k = 1; k <= i__2; ++k) {
00444                 i__3 = k + i__ * vl_dim1;
00445 /* Computing 2nd power */
00446                 r__1 = vl[i__3].r;
00447 /* Computing 2nd power */
00448                 r__2 = r_imag(&vl[k + i__ * vl_dim1]);
00449                 rwork[irwork + k - 1] = r__1 * r__1 + r__2 * r__2;
00450 /* L10: */
00451             }
00452             k = isamax_(n, &rwork[irwork], &c__1);
00453             r_cnjg(&q__2, &vl[k + i__ * vl_dim1]);
00454             r__1 = sqrt(rwork[irwork + k - 1]);
00455             q__1.r = q__2.r / r__1, q__1.i = q__2.i / r__1;
00456             tmp.r = q__1.r, tmp.i = q__1.i;
00457             cscal_(n, &tmp, &vl[i__ * vl_dim1 + 1], &c__1);
00458             i__2 = k + i__ * vl_dim1;
00459             i__3 = k + i__ * vl_dim1;
00460             r__1 = vl[i__3].r;
00461             q__1.r = r__1, q__1.i = 0.f;
00462             vl[i__2].r = q__1.r, vl[i__2].i = q__1.i;
00463 /* L20: */
00464         }
00465     }
00466 
00467     if (wantvr) {
00468 
00469 /*        Undo balancing of right eigenvectors */
00470 /*        (CWorkspace: none) */
00471 /*        (RWorkspace: need N) */
00472 
00473         cgebak_("B", "R", n, &ilo, &ihi, &rwork[ibal], n, &vr[vr_offset], 
00474                 ldvr, &ierr);
00475 
00476 /*        Normalize right eigenvectors and make largest component real */
00477 
00478         i__1 = *n;
00479         for (i__ = 1; i__ <= i__1; ++i__) {
00480             scl = 1.f / scnrm2_(n, &vr[i__ * vr_dim1 + 1], &c__1);
00481             csscal_(n, &scl, &vr[i__ * vr_dim1 + 1], &c__1);
00482             i__2 = *n;
00483             for (k = 1; k <= i__2; ++k) {
00484                 i__3 = k + i__ * vr_dim1;
00485 /* Computing 2nd power */
00486                 r__1 = vr[i__3].r;
00487 /* Computing 2nd power */
00488                 r__2 = r_imag(&vr[k + i__ * vr_dim1]);
00489                 rwork[irwork + k - 1] = r__1 * r__1 + r__2 * r__2;
00490 /* L30: */
00491             }
00492             k = isamax_(n, &rwork[irwork], &c__1);
00493             r_cnjg(&q__2, &vr[k + i__ * vr_dim1]);
00494             r__1 = sqrt(rwork[irwork + k - 1]);
00495             q__1.r = q__2.r / r__1, q__1.i = q__2.i / r__1;
00496             tmp.r = q__1.r, tmp.i = q__1.i;
00497             cscal_(n, &tmp, &vr[i__ * vr_dim1 + 1], &c__1);
00498             i__2 = k + i__ * vr_dim1;
00499             i__3 = k + i__ * vr_dim1;
00500             r__1 = vr[i__3].r;
00501             q__1.r = r__1, q__1.i = 0.f;
00502             vr[i__2].r = q__1.r, vr[i__2].i = q__1.i;
00503 /* L40: */
00504         }
00505     }
00506 
00507 /*     Undo scaling if necessary */
00508 
00509 L50:
00510     if (scalea) {
00511         i__1 = *n - *info;
00512 /* Computing MAX */
00513         i__3 = *n - *info;
00514         i__2 = max(i__3,1);
00515         clascl_("G", &c__0, &c__0, &cscale, &anrm, &i__1, &c__1, &w[*info + 1]
00516 , &i__2, &ierr);
00517         if (*info > 0) {
00518             i__1 = ilo - 1;
00519             clascl_("G", &c__0, &c__0, &cscale, &anrm, &i__1, &c__1, &w[1], n, 
00520                      &ierr);
00521         }
00522     }
00523 
00524     work[1].r = (real) maxwrk, work[1].i = 0.f;
00525     return 0;
00526 
00527 /*     End of CGEEV */
00528 
00529 } /* cgeev_ */


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:55:26