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


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