cstedc.c
Go to the documentation of this file.
00001 /* cstedc.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__9 = 9;
00019 static integer c__0 = 0;
00020 static integer c__2 = 2;
00021 static real c_b17 = 0.f;
00022 static real c_b18 = 1.f;
00023 static integer c__1 = 1;
00024 
00025 /* Subroutine */ int cstedc_(char *compz, integer *n, real *d__, real *e, 
00026         complex *z__, integer *ldz, complex *work, integer *lwork, real *
00027         rwork, integer *lrwork, integer *iwork, integer *liwork, integer *
00028         info)
00029 {
00030     /* System generated locals */
00031     integer z_dim1, z_offset, i__1, i__2, i__3, i__4;
00032     real r__1, r__2;
00033 
00034     /* Builtin functions */
00035     double log(doublereal);
00036     integer pow_ii(integer *, integer *);
00037     double sqrt(doublereal);
00038 
00039     /* Local variables */
00040     integer i__, j, k, m;
00041     real p;
00042     integer ii, ll, lgn;
00043     real eps, tiny;
00044     extern logical lsame_(char *, char *);
00045     extern /* Subroutine */ int cswap_(integer *, complex *, integer *, 
00046             complex *, integer *);
00047     integer lwmin;
00048     extern /* Subroutine */ int claed0_(integer *, integer *, real *, real *, 
00049             complex *, integer *, complex *, integer *, real *, integer *, 
00050             integer *);
00051     integer start;
00052     extern /* Subroutine */ int clacrm_(integer *, integer *, complex *, 
00053             integer *, real *, integer *, complex *, integer *, real *);
00054     extern doublereal slamch_(char *);
00055     extern /* Subroutine */ int clacpy_(char *, integer *, integer *, complex 
00056             *, integer *, complex *, integer *), xerbla_(char *, 
00057             integer *);
00058     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
00059             integer *, integer *);
00060     integer finish;
00061     extern /* Subroutine */ int slascl_(char *, integer *, integer *, real *, 
00062             real *, integer *, integer *, real *, integer *, integer *), sstedc_(char *, integer *, real *, real *, real *, 
00063             integer *, real *, integer *, integer *, integer *, integer *), slaset_(char *, integer *, integer *, real *, real *, 
00064             real *, integer *);
00065     integer liwmin, icompz;
00066     extern /* Subroutine */ int csteqr_(char *, integer *, real *, real *, 
00067             complex *, integer *, real *, integer *);
00068     real orgnrm;
00069     extern doublereal slanst_(char *, integer *, real *, real *);
00070     extern /* Subroutine */ int ssterf_(integer *, real *, real *, integer *);
00071     integer lrwmin;
00072     logical lquery;
00073     integer smlsiz;
00074     extern /* Subroutine */ int ssteqr_(char *, integer *, real *, real *, 
00075             real *, integer *, real *, integer *);
00076 
00077 
00078 /*  -- LAPACK routine (version 3.2) -- */
00079 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00080 /*     November 2006 */
00081 
00082 /*     .. Scalar Arguments .. */
00083 /*     .. */
00084 /*     .. Array Arguments .. */
00085 /*     .. */
00086 
00087 /*  Purpose */
00088 /*  ======= */
00089 
00090 /*  CSTEDC computes all eigenvalues and, optionally, eigenvectors of a */
00091 /*  symmetric tridiagonal matrix using the divide and conquer method. */
00092 /*  The eigenvectors of a full or band complex Hermitian matrix can also */
00093 /*  be found if CHETRD or CHPTRD or CHBTRD has been used to reduce this */
00094 /*  matrix to tridiagonal form. */
00095 
00096 /*  This code makes very mild assumptions about floating point */
00097 /*  arithmetic. It will work on machines with a guard digit in */
00098 /*  add/subtract, or on those binary machines without guard digits */
00099 /*  which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or Cray-2. */
00100 /*  It could conceivably fail on hexadecimal or decimal machines */
00101 /*  without guard digits, but we know of none.  See SLAED3 for details. */
00102 
00103 /*  Arguments */
00104 /*  ========= */
00105 
00106 /*  COMPZ   (input) CHARACTER*1 */
00107 /*          = 'N':  Compute eigenvalues only. */
00108 /*          = 'I':  Compute eigenvectors of tridiagonal matrix also. */
00109 /*          = 'V':  Compute eigenvectors of original Hermitian matrix */
00110 /*                  also.  On entry, Z contains the unitary matrix used */
00111 /*                  to reduce the original matrix to tridiagonal form. */
00112 
00113 /*  N       (input) INTEGER */
00114 /*          The dimension of the symmetric tridiagonal matrix.  N >= 0. */
00115 
00116 /*  D       (input/output) REAL array, dimension (N) */
00117 /*          On entry, the diagonal elements of the tridiagonal matrix. */
00118 /*          On exit, if INFO = 0, the eigenvalues in ascending order. */
00119 
00120 /*  E       (input/output) REAL array, dimension (N-1) */
00121 /*          On entry, the subdiagonal elements of the tridiagonal matrix. */
00122 /*          On exit, E has been destroyed. */
00123 
00124 /*  Z       (input/output) COMPLEX array, dimension (LDZ,N) */
00125 /*          On entry, if COMPZ = 'V', then Z contains the unitary */
00126 /*          matrix used in the reduction to tridiagonal form. */
00127 /*          On exit, if INFO = 0, then if COMPZ = 'V', Z contains the */
00128 /*          orthonormal eigenvectors of the original Hermitian matrix, */
00129 /*          and if COMPZ = 'I', Z contains the orthonormal eigenvectors */
00130 /*          of the symmetric tridiagonal matrix. */
00131 /*          If  COMPZ = 'N', then Z is not referenced. */
00132 
00133 /*  LDZ     (input) INTEGER */
00134 /*          The leading dimension of the array Z.  LDZ >= 1. */
00135 /*          If eigenvectors are desired, then LDZ >= max(1,N). */
00136 
00137 /*  WORK    (workspace/output) COMPLEX    array, dimension (MAX(1,LWORK)) */
00138 /*          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
00139 
00140 /*  LWORK   (input) INTEGER */
00141 /*          The dimension of the array WORK. */
00142 /*          If COMPZ = 'N' or 'I', or N <= 1, LWORK must be at least 1. */
00143 /*          If COMPZ = 'V' and N > 1, LWORK must be at least N*N. */
00144 /*          Note that for COMPZ = 'V', then if N is less than or */
00145 /*          equal to the minimum divide size, usually 25, then LWORK need */
00146 /*          only be 1. */
00147 
00148 /*          If LWORK = -1, then a workspace query is assumed; the routine */
00149 /*          only calculates the optimal sizes of the WORK, RWORK and */
00150 /*          IWORK arrays, returns these values as the first entries of */
00151 /*          the WORK, RWORK and IWORK arrays, and no error message */
00152 /*          related to LWORK or LRWORK or LIWORK is issued by XERBLA. */
00153 
00154 /*  RWORK   (workspace/output) REAL array, dimension (MAX(1,LRWORK)) */
00155 /*          On exit, if INFO = 0, RWORK(1) returns the optimal LRWORK. */
00156 
00157 /*  LRWORK  (input) INTEGER */
00158 /*          The dimension of the array RWORK. */
00159 /*          If COMPZ = 'N' or N <= 1, LRWORK must be at least 1. */
00160 /*          If COMPZ = 'V' and N > 1, LRWORK must be at least */
00161 /*                         1 + 3*N + 2*N*lg N + 3*N**2 , */
00162 /*                         where lg( N ) = smallest integer k such */
00163 /*                         that 2**k >= N. */
00164 /*          If COMPZ = 'I' and N > 1, LRWORK must be at least */
00165 /*                         1 + 4*N + 2*N**2 . */
00166 /*          Note that for COMPZ = 'I' or 'V', then if N is less than or */
00167 /*          equal to the minimum divide size, usually 25, then LRWORK */
00168 /*          need only be max(1,2*(N-1)). */
00169 
00170 /*          If LRWORK = -1, then a workspace query is assumed; the */
00171 /*          routine only calculates the optimal sizes of the WORK, RWORK */
00172 /*          and IWORK arrays, returns these values as the first entries */
00173 /*          of the WORK, RWORK and IWORK arrays, and no error message */
00174 /*          related to LWORK or LRWORK or LIWORK is issued by XERBLA. */
00175 
00176 /*  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK)) */
00177 /*          On exit, if INFO = 0, IWORK(1) returns the optimal LIWORK. */
00178 
00179 /*  LIWORK  (input) INTEGER */
00180 /*          The dimension of the array IWORK. */
00181 /*          If COMPZ = 'N' or N <= 1, LIWORK must be at least 1. */
00182 /*          If COMPZ = 'V' or N > 1,  LIWORK must be at least */
00183 /*                                    6 + 6*N + 5*N*lg N. */
00184 /*          If COMPZ = 'I' or N > 1,  LIWORK must be at least */
00185 /*                                    3 + 5*N . */
00186 /*          Note that for COMPZ = 'I' or 'V', then if N is less than or */
00187 /*          equal to the minimum divide size, usually 25, then LIWORK */
00188 /*          need only be 1. */
00189 
00190 /*          If LIWORK = -1, then a workspace query is assumed; the */
00191 /*          routine only calculates the optimal sizes of the WORK, RWORK */
00192 /*          and IWORK arrays, returns these values as the first entries */
00193 /*          of the WORK, RWORK and IWORK arrays, and no error message */
00194 /*          related to LWORK or LRWORK or LIWORK is issued by XERBLA. */
00195 
00196 /*  INFO    (output) INTEGER */
00197 /*          = 0:  successful exit. */
00198 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
00199 /*          > 0:  The algorithm failed to compute an eigenvalue while */
00200 /*                working on the submatrix lying in rows and columns */
00201 /*                INFO/(N+1) through mod(INFO,N+1). */
00202 
00203 /*  Further Details */
00204 /*  =============== */
00205 
00206 /*  Based on contributions by */
00207 /*     Jeff Rutter, Computer Science Division, University of California */
00208 /*     at Berkeley, USA */
00209 
00210 /*  ===================================================================== */
00211 
00212 /*     .. Parameters .. */
00213 /*     .. */
00214 /*     .. Local Scalars .. */
00215 /*     .. */
00216 /*     .. External Functions .. */
00217 /*     .. */
00218 /*     .. External Subroutines .. */
00219 /*     .. */
00220 /*     .. Intrinsic Functions .. */
00221 /*     .. */
00222 /*     .. Executable Statements .. */
00223 
00224 /*     Test the input parameters. */
00225 
00226     /* Parameter adjustments */
00227     --d__;
00228     --e;
00229     z_dim1 = *ldz;
00230     z_offset = 1 + z_dim1;
00231     z__ -= z_offset;
00232     --work;
00233     --rwork;
00234     --iwork;
00235 
00236     /* Function Body */
00237     *info = 0;
00238     lquery = *lwork == -1 || *lrwork == -1 || *liwork == -1;
00239 
00240     if (lsame_(compz, "N")) {
00241         icompz = 0;
00242     } else if (lsame_(compz, "V")) {
00243         icompz = 1;
00244     } else if (lsame_(compz, "I")) {
00245         icompz = 2;
00246     } else {
00247         icompz = -1;
00248     }
00249     if (icompz < 0) {
00250         *info = -1;
00251     } else if (*n < 0) {
00252         *info = -2;
00253     } else if (*ldz < 1 || icompz > 0 && *ldz < max(1,*n)) {
00254         *info = -6;
00255     }
00256 
00257     if (*info == 0) {
00258 
00259 /*        Compute the workspace requirements */
00260 
00261         smlsiz = ilaenv_(&c__9, "CSTEDC", " ", &c__0, &c__0, &c__0, &c__0);
00262         if (*n <= 1 || icompz == 0) {
00263             lwmin = 1;
00264             liwmin = 1;
00265             lrwmin = 1;
00266         } else if (*n <= smlsiz) {
00267             lwmin = 1;
00268             liwmin = 1;
00269             lrwmin = *n - 1 << 1;
00270         } else if (icompz == 1) {
00271             lgn = (integer) (log((real) (*n)) / log(2.f));
00272             if (pow_ii(&c__2, &lgn) < *n) {
00273                 ++lgn;
00274             }
00275             if (pow_ii(&c__2, &lgn) < *n) {
00276                 ++lgn;
00277             }
00278             lwmin = *n * *n;
00279 /* Computing 2nd power */
00280             i__1 = *n;
00281             lrwmin = *n * 3 + 1 + (*n << 1) * lgn + i__1 * i__1 * 3;
00282             liwmin = *n * 6 + 6 + *n * 5 * lgn;
00283         } else if (icompz == 2) {
00284             lwmin = 1;
00285 /* Computing 2nd power */
00286             i__1 = *n;
00287             lrwmin = (*n << 2) + 1 + (i__1 * i__1 << 1);
00288             liwmin = *n * 5 + 3;
00289         }
00290         work[1].r = (real) lwmin, work[1].i = 0.f;
00291         rwork[1] = (real) lrwmin;
00292         iwork[1] = liwmin;
00293 
00294         if (*lwork < lwmin && ! lquery) {
00295             *info = -8;
00296         } else if (*lrwork < lrwmin && ! lquery) {
00297             *info = -10;
00298         } else if (*liwork < liwmin && ! lquery) {
00299             *info = -12;
00300         }
00301     }
00302 
00303     if (*info != 0) {
00304         i__1 = -(*info);
00305         xerbla_("CSTEDC", &i__1);
00306         return 0;
00307     } else if (lquery) {
00308         return 0;
00309     }
00310 
00311 /*     Quick return if possible */
00312 
00313     if (*n == 0) {
00314         return 0;
00315     }
00316     if (*n == 1) {
00317         if (icompz != 0) {
00318             i__1 = z_dim1 + 1;
00319             z__[i__1].r = 1.f, z__[i__1].i = 0.f;
00320         }
00321         return 0;
00322     }
00323 
00324 /*     If the following conditional clause is removed, then the routine */
00325 /*     will use the Divide and Conquer routine to compute only the */
00326 /*     eigenvalues, which requires (3N + 3N**2) real workspace and */
00327 /*     (2 + 5N + 2N lg(N)) integer workspace. */
00328 /*     Since on many architectures SSTERF is much faster than any other */
00329 /*     algorithm for finding eigenvalues only, it is used here */
00330 /*     as the default. If the conditional clause is removed, then */
00331 /*     information on the size of workspace needs to be changed. */
00332 
00333 /*     If COMPZ = 'N', use SSTERF to compute the eigenvalues. */
00334 
00335     if (icompz == 0) {
00336         ssterf_(n, &d__[1], &e[1], info);
00337         goto L70;
00338     }
00339 
00340 /*     If N is smaller than the minimum divide size (SMLSIZ+1), then */
00341 /*     solve the problem with another solver. */
00342 
00343     if (*n <= smlsiz) {
00344 
00345         csteqr_(compz, n, &d__[1], &e[1], &z__[z_offset], ldz, &rwork[1], 
00346                 info);
00347 
00348     } else {
00349 
00350 /*        If COMPZ = 'I', we simply call SSTEDC instead. */
00351 
00352         if (icompz == 2) {
00353             slaset_("Full", n, n, &c_b17, &c_b18, &rwork[1], n);
00354             ll = *n * *n + 1;
00355             i__1 = *lrwork - ll + 1;
00356             sstedc_("I", n, &d__[1], &e[1], &rwork[1], n, &rwork[ll], &i__1, &
00357                     iwork[1], liwork, info);
00358             i__1 = *n;
00359             for (j = 1; j <= i__1; ++j) {
00360                 i__2 = *n;
00361                 for (i__ = 1; i__ <= i__2; ++i__) {
00362                     i__3 = i__ + j * z_dim1;
00363                     i__4 = (j - 1) * *n + i__;
00364                     z__[i__3].r = rwork[i__4], z__[i__3].i = 0.f;
00365 /* L10: */
00366                 }
00367 /* L20: */
00368             }
00369             goto L70;
00370         }
00371 
00372 /*        From now on, only option left to be handled is COMPZ = 'V', */
00373 /*        i.e. ICOMPZ = 1. */
00374 
00375 /*        Scale. */
00376 
00377         orgnrm = slanst_("M", n, &d__[1], &e[1]);
00378         if (orgnrm == 0.f) {
00379             goto L70;
00380         }
00381 
00382         eps = slamch_("Epsilon");
00383 
00384         start = 1;
00385 
00386 /*        while ( START <= N ) */
00387 
00388 L30:
00389         if (start <= *n) {
00390 
00391 /*           Let FINISH be the position of the next subdiagonal entry */
00392 /*           such that E( FINISH ) <= TINY or FINISH = N if no such */
00393 /*           subdiagonal exists.  The matrix identified by the elements */
00394 /*           between START and FINISH constitutes an independent */
00395 /*           sub-problem. */
00396 
00397             finish = start;
00398 L40:
00399             if (finish < *n) {
00400                 tiny = eps * sqrt((r__1 = d__[finish], dabs(r__1))) * sqrt((
00401                         r__2 = d__[finish + 1], dabs(r__2)));
00402                 if ((r__1 = e[finish], dabs(r__1)) > tiny) {
00403                     ++finish;
00404                     goto L40;
00405                 }
00406             }
00407 
00408 /*           (Sub) Problem determined.  Compute its size and solve it. */
00409 
00410             m = finish - start + 1;
00411             if (m > smlsiz) {
00412 
00413 /*              Scale. */
00414 
00415                 orgnrm = slanst_("M", &m, &d__[start], &e[start]);
00416                 slascl_("G", &c__0, &c__0, &orgnrm, &c_b18, &m, &c__1, &d__[
00417                         start], &m, info);
00418                 i__1 = m - 1;
00419                 i__2 = m - 1;
00420                 slascl_("G", &c__0, &c__0, &orgnrm, &c_b18, &i__1, &c__1, &e[
00421                         start], &i__2, info);
00422 
00423                 claed0_(n, &m, &d__[start], &e[start], &z__[start * z_dim1 + 
00424                         1], ldz, &work[1], n, &rwork[1], &iwork[1], info);
00425                 if (*info > 0) {
00426                     *info = (*info / (m + 1) + start - 1) * (*n + 1) + *info %
00427                              (m + 1) + start - 1;
00428                     goto L70;
00429                 }
00430 
00431 /*              Scale back. */
00432 
00433                 slascl_("G", &c__0, &c__0, &c_b18, &orgnrm, &m, &c__1, &d__[
00434                         start], &m, info);
00435 
00436             } else {
00437                 ssteqr_("I", &m, &d__[start], &e[start], &rwork[1], &m, &
00438                         rwork[m * m + 1], info);
00439                 clacrm_(n, &m, &z__[start * z_dim1 + 1], ldz, &rwork[1], &m, &
00440                         work[1], n, &rwork[m * m + 1]);
00441                 clacpy_("A", n, &m, &work[1], n, &z__[start * z_dim1 + 1], 
00442                         ldz);
00443                 if (*info > 0) {
00444                     *info = start * (*n + 1) + finish;
00445                     goto L70;
00446                 }
00447             }
00448 
00449             start = finish + 1;
00450             goto L30;
00451         }
00452 
00453 /*        endwhile */
00454 
00455 /*        If the problem split any number of times, then the eigenvalues */
00456 /*        will not be properly ordered.  Here we permute the eigenvalues */
00457 /*        (and the associated eigenvectors) into ascending order. */
00458 
00459         if (m != *n) {
00460 
00461 /*           Use Selection Sort to minimize swaps of eigenvectors */
00462 
00463             i__1 = *n;
00464             for (ii = 2; ii <= i__1; ++ii) {
00465                 i__ = ii - 1;
00466                 k = i__;
00467                 p = d__[i__];
00468                 i__2 = *n;
00469                 for (j = ii; j <= i__2; ++j) {
00470                     if (d__[j] < p) {
00471                         k = j;
00472                         p = d__[j];
00473                     }
00474 /* L50: */
00475                 }
00476                 if (k != i__) {
00477                     d__[k] = d__[i__];
00478                     d__[i__] = p;
00479                     cswap_(n, &z__[i__ * z_dim1 + 1], &c__1, &z__[k * z_dim1 
00480                             + 1], &c__1);
00481                 }
00482 /* L60: */
00483             }
00484         }
00485     }
00486 
00487 L70:
00488     work[1].r = (real) lwmin, work[1].i = 0.f;
00489     rwork[1] = (real) lrwmin;
00490     iwork[1] = liwmin;
00491 
00492     return 0;
00493 
00494 /*     End of CSTEDC */
00495 
00496 } /* cstedc_ */


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