dgees.c
Go to the documentation of this file.
00001 /* dgees.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 dgees_(char *jobvs, char *sort, L_fp select, integer *n, 
00023         doublereal *a, integer *lda, integer *sdim, doublereal *wr, 
00024         doublereal *wi, doublereal *vs, integer *ldvs, doublereal *work, 
00025         integer *lwork, logical *bwork, integer *info)
00026 {
00027     /* System generated locals */
00028     integer a_dim1, a_offset, vs_dim1, vs_offset, i__1, i__2, i__3;
00029 
00030     /* Builtin functions */
00031     double sqrt(doublereal);
00032 
00033     /* Local variables */
00034     integer i__;
00035     doublereal s;
00036     integer i1, i2, ip, ihi, ilo;
00037     doublereal dum[1], eps, sep;
00038     integer ibal;
00039     doublereal anrm;
00040     integer idum[1], ierr, itau, iwrk, inxt, icond, ieval;
00041     extern logical lsame_(char *, char *);
00042     extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
00043             doublereal *, integer *), dswap_(integer *, doublereal *, integer 
00044             *, doublereal *, integer *);
00045     logical cursl;
00046     extern /* Subroutine */ int dlabad_(doublereal *, doublereal *), dgebak_(
00047             char *, char *, integer *, integer *, integer *, doublereal *, 
00048             integer *, doublereal *, integer *, integer *), 
00049             dgebal_(char *, integer *, doublereal *, integer *, integer *, 
00050             integer *, doublereal *, integer *);
00051     logical lst2sl, scalea;
00052     extern doublereal dlamch_(char *);
00053     doublereal cscale;
00054     extern doublereal dlange_(char *, integer *, integer *, doublereal *, 
00055             integer *, doublereal *);
00056     extern /* Subroutine */ int dgehrd_(integer *, integer *, integer *, 
00057             doublereal *, integer *, doublereal *, doublereal *, integer *, 
00058             integer *), dlascl_(char *, integer *, integer *, doublereal *, 
00059             doublereal *, integer *, integer *, doublereal *, integer *, 
00060             integer *), dlacpy_(char *, integer *, integer *, 
00061             doublereal *, integer *, doublereal *, integer *), 
00062             xerbla_(char *, integer *);
00063     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
00064             integer *, integer *);
00065     doublereal bignum;
00066     extern /* Subroutine */ int dorghr_(integer *, integer *, integer *, 
00067             doublereal *, integer *, doublereal *, doublereal *, integer *, 
00068             integer *), dhseqr_(char *, char *, integer *, integer *, integer 
00069             *, doublereal *, integer *, doublereal *, doublereal *, 
00070             doublereal *, integer *, doublereal *, integer *, integer *), dtrsen_(char *, char *, logical *, integer *, 
00071             doublereal *, integer *, doublereal *, integer *, doublereal *, 
00072             doublereal *, integer *, doublereal *, doublereal *, doublereal *, 
00073              integer *, integer *, integer *, integer *);
00074     logical lastsl;
00075     integer minwrk, maxwrk;
00076     doublereal smlnum;
00077     integer hswork;
00078     logical wantst, lquery, wantvs;
00079 
00080 
00081 /*  -- LAPACK driver routine (version 3.2) -- */
00082 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00083 /*     November 2006 */
00084 
00085 /*     .. Scalar Arguments .. */
00086 /*     .. */
00087 /*     .. Array Arguments .. */
00088 /*     .. */
00089 /*     .. Function Arguments .. */
00090 /*     .. */
00091 
00092 /*  Purpose */
00093 /*  ======= */
00094 
00095 /*  DGEES computes for an N-by-N real nonsymmetric matrix A, the */
00096 /*  eigenvalues, the real Schur form T, and, optionally, the matrix of */
00097 /*  Schur vectors Z.  This gives the Schur factorization A = Z*T*(Z**T). */
00098 
00099 /*  Optionally, it also orders the eigenvalues on the diagonal of the */
00100 /*  real Schur form so that selected eigenvalues are at the top left. */
00101 /*  The leading columns of Z then form an orthonormal basis for the */
00102 /*  invariant subspace corresponding to the selected eigenvalues. */
00103 
00104 /*  A matrix is in real Schur form if it is upper quasi-triangular with */
00105 /*  1-by-1 and 2-by-2 blocks. 2-by-2 blocks will be standardized in the */
00106 /*  form */
00107 /*          [  a  b  ] */
00108 /*          [  c  a  ] */
00109 
00110 /*  where b*c < 0. The eigenvalues of such a block are a +- sqrt(bc). */
00111 
00112 /*  Arguments */
00113 /*  ========= */
00114 
00115 /*  JOBVS   (input) CHARACTER*1 */
00116 /*          = 'N': Schur vectors are not computed; */
00117 /*          = 'V': Schur vectors are computed. */
00118 
00119 /*  SORT    (input) CHARACTER*1 */
00120 /*          Specifies whether or not to order the eigenvalues on the */
00121 /*          diagonal of the Schur form. */
00122 /*          = 'N': Eigenvalues are not ordered; */
00123 /*          = 'S': Eigenvalues are ordered (see SELECT). */
00124 
00125 /*  SELECT  (external procedure) LOGICAL FUNCTION of two DOUBLE PRECISION arguments */
00126 /*          SELECT must be declared EXTERNAL in the calling subroutine. */
00127 /*          If SORT = 'S', SELECT is used to select eigenvalues to sort */
00128 /*          to the top left of the Schur form. */
00129 /*          If SORT = 'N', SELECT is not referenced. */
00130 /*          An eigenvalue WR(j)+sqrt(-1)*WI(j) is selected if */
00131 /*          SELECT(WR(j),WI(j)) is true; i.e., if either one of a complex */
00132 /*          conjugate pair of eigenvalues is selected, then both complex */
00133 /*          eigenvalues are selected. */
00134 /*          Note that a selected complex eigenvalue may no longer */
00135 /*          satisfy SELECT(WR(j),WI(j)) = .TRUE. after ordering, since */
00136 /*          ordering may change the value of complex eigenvalues */
00137 /*          (especially if the eigenvalue is ill-conditioned); in this */
00138 /*          case INFO is set to N+2 (see INFO below). */
00139 
00140 /*  N       (input) INTEGER */
00141 /*          The order of the matrix A. N >= 0. */
00142 
00143 /*  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
00144 /*          On entry, the N-by-N matrix A. */
00145 /*          On exit, A has been overwritten by its real Schur form T. */
00146 
00147 /*  LDA     (input) INTEGER */
00148 /*          The leading dimension of the array A.  LDA >= max(1,N). */
00149 
00150 /*  SDIM    (output) INTEGER */
00151 /*          If SORT = 'N', SDIM = 0. */
00152 /*          If SORT = 'S', SDIM = number of eigenvalues (after sorting) */
00153 /*                         for which SELECT is true. (Complex conjugate */
00154 /*                         pairs for which SELECT is true for either */
00155 /*                         eigenvalue count as 2.) */
00156 
00157 /*  WR      (output) DOUBLE PRECISION array, dimension (N) */
00158 /*  WI      (output) DOUBLE PRECISION array, dimension (N) */
00159 /*          WR and WI contain the real and imaginary parts, */
00160 /*          respectively, of the computed eigenvalues in the same order */
00161 /*          that they appear on the diagonal of the output Schur form T. */
00162 /*          Complex conjugate pairs of eigenvalues will appear */
00163 /*          consecutively with the eigenvalue having the positive */
00164 /*          imaginary part first. */
00165 
00166 /*  VS      (output) DOUBLE PRECISION array, dimension (LDVS,N) */
00167 /*          If JOBVS = 'V', VS contains the orthogonal matrix Z of Schur */
00168 /*          vectors. */
00169 /*          If JOBVS = 'N', VS is not referenced. */
00170 
00171 /*  LDVS    (input) INTEGER */
00172 /*          The leading dimension of the array VS.  LDVS >= 1; if */
00173 /*          JOBVS = 'V', LDVS >= N. */
00174 
00175 /*  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
00176 /*          On exit, if INFO = 0, WORK(1) contains the optimal LWORK. */
00177 
00178 /*  LWORK   (input) INTEGER */
00179 /*          The dimension of the array WORK.  LWORK >= max(1,3*N). */
00180 /*          For good performance, LWORK must generally be larger. */
00181 
00182 /*          If LWORK = -1, then a workspace query is assumed; the routine */
00183 /*          only calculates the optimal size of the WORK array, returns */
00184 /*          this value as the first entry of the WORK array, and no error */
00185 /*          message related to LWORK is issued by XERBLA. */
00186 
00187 /*  BWORK   (workspace) LOGICAL array, dimension (N) */
00188 /*          Not referenced if SORT = 'N'. */
00189 
00190 /*  INFO    (output) INTEGER */
00191 /*          = 0: successful exit */
00192 /*          < 0: if INFO = -i, the i-th argument had an illegal value. */
00193 /*          > 0: if INFO = i, and i is */
00194 /*             <= N: the QR algorithm failed to compute all the */
00195 /*                   eigenvalues; elements 1:ILO-1 and i+1:N of WR and WI */
00196 /*                   contain those eigenvalues which have converged; if */
00197 /*                   JOBVS = 'V', VS contains the matrix which reduces A */
00198 /*                   to its partially converged Schur form. */
00199 /*             = N+1: the eigenvalues could not be reordered because some */
00200 /*                   eigenvalues were too close to separate (the problem */
00201 /*                   is very ill-conditioned); */
00202 /*             = N+2: after reordering, roundoff changed values of some */
00203 /*                   complex eigenvalues so that leading eigenvalues in */
00204 /*                   the Schur form no longer satisfy SELECT=.TRUE.  This */
00205 /*                   could also be caused by underflow due to scaling. */
00206 
00207 /*  ===================================================================== */
00208 
00209 /*     .. Parameters .. */
00210 /*     .. */
00211 /*     .. Local Scalars .. */
00212 /*     .. */
00213 /*     .. Local Arrays .. */
00214 /*     .. */
00215 /*     .. External Subroutines .. */
00216 /*     .. */
00217 /*     .. External Functions .. */
00218 /*     .. */
00219 /*     .. Intrinsic Functions .. */
00220 /*     .. */
00221 /*     .. Executable Statements .. */
00222 
00223 /*     Test the input arguments */
00224 
00225     /* Parameter adjustments */
00226     a_dim1 = *lda;
00227     a_offset = 1 + a_dim1;
00228     a -= a_offset;
00229     --wr;
00230     --wi;
00231     vs_dim1 = *ldvs;
00232     vs_offset = 1 + vs_dim1;
00233     vs -= vs_offset;
00234     --work;
00235     --bwork;
00236 
00237     /* Function Body */
00238     *info = 0;
00239     lquery = *lwork == -1;
00240     wantvs = lsame_(jobvs, "V");
00241     wantst = lsame_(sort, "S");
00242     if (! wantvs && ! lsame_(jobvs, "N")) {
00243         *info = -1;
00244     } else if (! wantst && ! lsame_(sort, "N")) {
00245         *info = -2;
00246     } else if (*n < 0) {
00247         *info = -4;
00248     } else if (*lda < max(1,*n)) {
00249         *info = -6;
00250     } else if (*ldvs < 1 || wantvs && *ldvs < *n) {
00251         *info = -11;
00252     }
00253 
00254 /*     Compute workspace */
00255 /*      (Note: Comments in the code beginning "Workspace:" describe the */
00256 /*       minimal amount of workspace needed at that point in the code, */
00257 /*       as well as the preferred amount for good performance. */
00258 /*       NB refers to the optimal block size for the immediately */
00259 /*       following subroutine, as returned by ILAENV. */
00260 /*       HSWORK refers to the workspace preferred by DHSEQR, as */
00261 /*       calculated below. HSWORK is computed assuming ILO=1 and IHI=N, */
00262 /*       the worst case.) */
00263 
00264     if (*info == 0) {
00265         if (*n == 0) {
00266             minwrk = 1;
00267             maxwrk = 1;
00268         } else {
00269             maxwrk = (*n << 1) + *n * ilaenv_(&c__1, "DGEHRD", " ", n, &c__1, 
00270                     n, &c__0);
00271             minwrk = *n * 3;
00272 
00273             dhseqr_("S", jobvs, n, &c__1, n, &a[a_offset], lda, &wr[1], &wi[1]
00274 , &vs[vs_offset], ldvs, &work[1], &c_n1, &ieval);
00275             hswork = (integer) work[1];
00276 
00277             if (! wantvs) {
00278 /* Computing MAX */
00279                 i__1 = maxwrk, i__2 = *n + hswork;
00280                 maxwrk = max(i__1,i__2);
00281             } else {
00282 /* Computing MAX */
00283                 i__1 = maxwrk, i__2 = (*n << 1) + (*n - 1) * ilaenv_(&c__1, 
00284                         "DORGHR", " ", n, &c__1, n, &c_n1);
00285                 maxwrk = max(i__1,i__2);
00286 /* Computing MAX */
00287                 i__1 = maxwrk, i__2 = *n + hswork;
00288                 maxwrk = max(i__1,i__2);
00289             }
00290         }
00291         work[1] = (doublereal) maxwrk;
00292 
00293         if (*lwork < minwrk && ! lquery) {
00294             *info = -13;
00295         }
00296     }
00297 
00298     if (*info != 0) {
00299         i__1 = -(*info);
00300         xerbla_("DGEES ", &i__1);
00301         return 0;
00302     } else if (lquery) {
00303         return 0;
00304     }
00305 
00306 /*     Quick return if possible */
00307 
00308     if (*n == 0) {
00309         *sdim = 0;
00310         return 0;
00311     }
00312 
00313 /*     Get machine constants */
00314 
00315     eps = dlamch_("P");
00316     smlnum = dlamch_("S");
00317     bignum = 1. / smlnum;
00318     dlabad_(&smlnum, &bignum);
00319     smlnum = sqrt(smlnum) / eps;
00320     bignum = 1. / smlnum;
00321 
00322 /*     Scale A if max element outside range [SMLNUM,BIGNUM] */
00323 
00324     anrm = dlange_("M", n, n, &a[a_offset], lda, dum);
00325     scalea = FALSE_;
00326     if (anrm > 0. && anrm < smlnum) {
00327         scalea = TRUE_;
00328         cscale = smlnum;
00329     } else if (anrm > bignum) {
00330         scalea = TRUE_;
00331         cscale = bignum;
00332     }
00333     if (scalea) {
00334         dlascl_("G", &c__0, &c__0, &anrm, &cscale, n, n, &a[a_offset], lda, &
00335                 ierr);
00336     }
00337 
00338 /*     Permute the matrix to make it more nearly triangular */
00339 /*     (Workspace: need N) */
00340 
00341     ibal = 1;
00342     dgebal_("P", n, &a[a_offset], lda, &ilo, &ihi, &work[ibal], &ierr);
00343 
00344 /*     Reduce to upper Hessenberg form */
00345 /*     (Workspace: need 3*N, prefer 2*N+N*NB) */
00346 
00347     itau = *n + ibal;
00348     iwrk = *n + itau;
00349     i__1 = *lwork - iwrk + 1;
00350     dgehrd_(n, &ilo, &ihi, &a[a_offset], lda, &work[itau], &work[iwrk], &i__1, 
00351              &ierr);
00352 
00353     if (wantvs) {
00354 
00355 /*        Copy Householder vectors to VS */
00356 
00357         dlacpy_("L", n, n, &a[a_offset], lda, &vs[vs_offset], ldvs)
00358                 ;
00359 
00360 /*        Generate orthogonal matrix in VS */
00361 /*        (Workspace: need 3*N-1, prefer 2*N+(N-1)*NB) */
00362 
00363         i__1 = *lwork - iwrk + 1;
00364         dorghr_(n, &ilo, &ihi, &vs[vs_offset], ldvs, &work[itau], &work[iwrk], 
00365                  &i__1, &ierr);
00366     }
00367 
00368     *sdim = 0;
00369 
00370 /*     Perform QR iteration, accumulating Schur vectors in VS if desired */
00371 /*     (Workspace: need N+1, prefer N+HSWORK (see comments) ) */
00372 
00373     iwrk = itau;
00374     i__1 = *lwork - iwrk + 1;
00375     dhseqr_("S", jobvs, n, &ilo, &ihi, &a[a_offset], lda, &wr[1], &wi[1], &vs[
00376             vs_offset], ldvs, &work[iwrk], &i__1, &ieval);
00377     if (ieval > 0) {
00378         *info = ieval;
00379     }
00380 
00381 /*     Sort eigenvalues if desired */
00382 
00383     if (wantst && *info == 0) {
00384         if (scalea) {
00385             dlascl_("G", &c__0, &c__0, &cscale, &anrm, n, &c__1, &wr[1], n, &
00386                     ierr);
00387             dlascl_("G", &c__0, &c__0, &cscale, &anrm, n, &c__1, &wi[1], n, &
00388                     ierr);
00389         }
00390         i__1 = *n;
00391         for (i__ = 1; i__ <= i__1; ++i__) {
00392             bwork[i__] = (*select)(&wr[i__], &wi[i__]);
00393 /* L10: */
00394         }
00395 
00396 /*        Reorder eigenvalues and transform Schur vectors */
00397 /*        (Workspace: none needed) */
00398 
00399         i__1 = *lwork - iwrk + 1;
00400         dtrsen_("N", jobvs, &bwork[1], n, &a[a_offset], lda, &vs[vs_offset], 
00401                 ldvs, &wr[1], &wi[1], sdim, &s, &sep, &work[iwrk], &i__1, 
00402                 idum, &c__1, &icond);
00403         if (icond > 0) {
00404             *info = *n + icond;
00405         }
00406     }
00407 
00408     if (wantvs) {
00409 
00410 /*        Undo balancing */
00411 /*        (Workspace: need N) */
00412 
00413         dgebak_("P", "R", n, &ilo, &ihi, &work[ibal], n, &vs[vs_offset], ldvs, 
00414                  &ierr);
00415     }
00416 
00417     if (scalea) {
00418 
00419 /*        Undo scaling for the Schur form of A */
00420 
00421         dlascl_("H", &c__0, &c__0, &cscale, &anrm, n, n, &a[a_offset], lda, &
00422                 ierr);
00423         i__1 = *lda + 1;
00424         dcopy_(n, &a[a_offset], &i__1, &wr[1], &c__1);
00425         if (cscale == smlnum) {
00426 
00427 /*           If scaling back towards underflow, adjust WI if an */
00428 /*           offdiagonal element of a 2-by-2 block in the Schur form */
00429 /*           underflows. */
00430 
00431             if (ieval > 0) {
00432                 i1 = ieval + 1;
00433                 i2 = ihi - 1;
00434                 i__1 = ilo - 1;
00435 /* Computing MAX */
00436                 i__3 = ilo - 1;
00437                 i__2 = max(i__3,1);
00438                 dlascl_("G", &c__0, &c__0, &cscale, &anrm, &i__1, &c__1, &wi[
00439                         1], &i__2, &ierr);
00440             } else if (wantst) {
00441                 i1 = 1;
00442                 i2 = *n - 1;
00443             } else {
00444                 i1 = ilo;
00445                 i2 = ihi - 1;
00446             }
00447             inxt = i1 - 1;
00448             i__1 = i2;
00449             for (i__ = i1; i__ <= i__1; ++i__) {
00450                 if (i__ < inxt) {
00451                     goto L20;
00452                 }
00453                 if (wi[i__] == 0.) {
00454                     inxt = i__ + 1;
00455                 } else {
00456                     if (a[i__ + 1 + i__ * a_dim1] == 0.) {
00457                         wi[i__] = 0.;
00458                         wi[i__ + 1] = 0.;
00459                     } else if (a[i__ + 1 + i__ * a_dim1] != 0. && a[i__ + (
00460                             i__ + 1) * a_dim1] == 0.) {
00461                         wi[i__] = 0.;
00462                         wi[i__ + 1] = 0.;
00463                         if (i__ > 1) {
00464                             i__2 = i__ - 1;
00465                             dswap_(&i__2, &a[i__ * a_dim1 + 1], &c__1, &a[(
00466                                     i__ + 1) * a_dim1 + 1], &c__1);
00467                         }
00468                         if (*n > i__ + 1) {
00469                             i__2 = *n - i__ - 1;
00470                             dswap_(&i__2, &a[i__ + (i__ + 2) * a_dim1], lda, &
00471                                     a[i__ + 1 + (i__ + 2) * a_dim1], lda);
00472                         }
00473                         if (wantvs) {
00474                             dswap_(n, &vs[i__ * vs_dim1 + 1], &c__1, &vs[(i__ 
00475                                     + 1) * vs_dim1 + 1], &c__1);
00476                         }
00477                         a[i__ + (i__ + 1) * a_dim1] = a[i__ + 1 + i__ * 
00478                                 a_dim1];
00479                         a[i__ + 1 + i__ * a_dim1] = 0.;
00480                     }
00481                     inxt = i__ + 2;
00482                 }
00483 L20:
00484                 ;
00485             }
00486         }
00487 
00488 /*        Undo scaling for the imaginary part of the eigenvalues */
00489 
00490         i__1 = *n - ieval;
00491 /* Computing MAX */
00492         i__3 = *n - ieval;
00493         i__2 = max(i__3,1);
00494         dlascl_("G", &c__0, &c__0, &cscale, &anrm, &i__1, &c__1, &wi[ieval + 
00495                 1], &i__2, &ierr);
00496     }
00497 
00498     if (wantst && *info == 0) {
00499 
00500 /*        Check if reordering successful */
00501 
00502         lastsl = TRUE_;
00503         lst2sl = TRUE_;
00504         *sdim = 0;
00505         ip = 0;
00506         i__1 = *n;
00507         for (i__ = 1; i__ <= i__1; ++i__) {
00508             cursl = (*select)(&wr[i__], &wi[i__]);
00509             if (wi[i__] == 0.) {
00510                 if (cursl) {
00511                     ++(*sdim);
00512                 }
00513                 ip = 0;
00514                 if (cursl && ! lastsl) {
00515                     *info = *n + 2;
00516                 }
00517             } else {
00518                 if (ip == 1) {
00519 
00520 /*                 Last eigenvalue of conjugate pair */
00521 
00522                     cursl = cursl || lastsl;
00523                     lastsl = cursl;
00524                     if (cursl) {
00525                         *sdim += 2;
00526                     }
00527                     ip = -1;
00528                     if (cursl && ! lst2sl) {
00529                         *info = *n + 2;
00530                     }
00531                 } else {
00532 
00533 /*                 First eigenvalue of conjugate pair */
00534 
00535                     ip = 1;
00536                 }
00537             }
00538             lst2sl = lastsl;
00539             lastsl = cursl;
00540 /* L30: */
00541         }
00542     }
00543 
00544     work[1] = (doublereal) maxwrk;
00545     return 0;
00546 
00547 /*     End of DGEES */
00548 
00549 } /* dgees_ */


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