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


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:56:06