001:       SUBROUTINE ZHPEVX( JOBZ, RANGE, UPLO, N, AP, VL, VU, IL, IU,
002:      $                   ABSTOL, M, W, Z, LDZ, WORK, RWORK, IWORK,
003:      $                   IFAIL, INFO )
004: *
005: *  -- LAPACK driver routine (version 3.2) --
006: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       CHARACTER          JOBZ, RANGE, UPLO
011:       INTEGER            IL, INFO, IU, LDZ, M, N
012:       DOUBLE PRECISION   ABSTOL, VL, VU
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IFAIL( * ), IWORK( * )
016:       DOUBLE PRECISION   RWORK( * ), W( * )
017:       COMPLEX*16         AP( * ), WORK( * ), Z( LDZ, * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  ZHPEVX computes selected eigenvalues and, optionally, eigenvectors
024: *  of a complex Hermitian matrix A in packed storage.
025: *  Eigenvalues/vectors can be selected by specifying either a range of
026: *  values or a range of indices for the desired eigenvalues.
027: *
028: *  Arguments
029: *  =========
030: *
031: *  JOBZ    (input) CHARACTER*1
032: *          = 'N':  Compute eigenvalues only;
033: *          = 'V':  Compute eigenvalues and eigenvectors.
034: *
035: *  RANGE   (input) CHARACTER*1
036: *          = 'A': all eigenvalues will be found;
037: *          = 'V': all eigenvalues in the half-open interval (VL,VU]
038: *                 will be found;
039: *          = 'I': the IL-th through IU-th eigenvalues will be found.
040: *
041: *  UPLO    (input) CHARACTER*1
042: *          = 'U':  Upper triangle of A is stored;
043: *          = 'L':  Lower triangle of A is stored.
044: *
045: *  N       (input) INTEGER
046: *          The order of the matrix A.  N >= 0.
047: *
048: *  AP      (input/output) COMPLEX*16 array, dimension (N*(N+1)/2)
049: *          On entry, the upper or lower triangle of the Hermitian matrix
050: *          A, packed columnwise in a linear array.  The j-th column of A
051: *          is stored in the array AP as follows:
052: *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
053: *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
054: *
055: *          On exit, AP is overwritten by values generated during the
056: *          reduction to tridiagonal form.  If UPLO = 'U', the diagonal
057: *          and first superdiagonal of the tridiagonal matrix T overwrite
058: *          the corresponding elements of A, and if UPLO = 'L', the
059: *          diagonal and first subdiagonal of T overwrite the
060: *          corresponding elements of A.
061: *
062: *  VL      (input) DOUBLE PRECISION
063: *  VU      (input) DOUBLE PRECISION
064: *          If RANGE='V', the lower and upper bounds of the interval to
065: *          be searched for eigenvalues. VL < VU.
066: *          Not referenced if RANGE = 'A' or 'I'.
067: *
068: *  IL      (input) INTEGER
069: *  IU      (input) INTEGER
070: *          If RANGE='I', the indices (in ascending order) of the
071: *          smallest and largest eigenvalues to be returned.
072: *          1 <= IL <= IU <= N, if N > 0; IL = 1 and IU = 0 if N = 0.
073: *          Not referenced if RANGE = 'A' or 'V'.
074: *
075: *  ABSTOL  (input) DOUBLE PRECISION
076: *          The absolute error tolerance for the eigenvalues.
077: *          An approximate eigenvalue is accepted as converged
078: *          when it is determined to lie in an interval [a,b]
079: *          of width less than or equal to
080: *
081: *                  ABSTOL + EPS *   max( |a|,|b| ) ,
082: *
083: *          where EPS is the machine precision.  If ABSTOL is less than
084: *          or equal to zero, then  EPS*|T|  will be used in its place,
085: *          where |T| is the 1-norm of the tridiagonal matrix obtained
086: *          by reducing AP to tridiagonal form.
087: *
088: *          Eigenvalues will be computed most accurately when ABSTOL is
089: *          set to twice the underflow threshold 2*DLAMCH('S'), not zero.
090: *          If this routine returns with INFO>0, indicating that some
091: *          eigenvectors did not converge, try setting ABSTOL to
092: *          2*DLAMCH('S').
093: *
094: *          See "Computing Small Singular Values of Bidiagonal Matrices
095: *          with Guaranteed High Relative Accuracy," by Demmel and
096: *          Kahan, LAPACK Working Note #3.
097: *
098: *  M       (output) INTEGER
099: *          The total number of eigenvalues found.  0 <= M <= N.
100: *          If RANGE = 'A', M = N, and if RANGE = 'I', M = IU-IL+1.
101: *
102: *  W       (output) DOUBLE PRECISION array, dimension (N)
103: *          If INFO = 0, the selected eigenvalues in ascending order.
104: *
105: *  Z       (output) COMPLEX*16 array, dimension (LDZ, max(1,M))
106: *          If JOBZ = 'V', then if INFO = 0, the first M columns of Z
107: *          contain the orthonormal eigenvectors of the matrix A
108: *          corresponding to the selected eigenvalues, with the i-th
109: *          column of Z holding the eigenvector associated with W(i).
110: *          If an eigenvector fails to converge, then that column of Z
111: *          contains the latest approximation to the eigenvector, and
112: *          the index of the eigenvector is returned in IFAIL.
113: *          If JOBZ = 'N', then Z is not referenced.
114: *          Note: the user must ensure that at least max(1,M) columns are
115: *          supplied in the array Z; if RANGE = 'V', the exact value of M
116: *          is not known in advance and an upper bound must be used.
117: *
118: *  LDZ     (input) INTEGER
119: *          The leading dimension of the array Z.  LDZ >= 1, and if
120: *          JOBZ = 'V', LDZ >= max(1,N).
121: *
122: *  WORK    (workspace) COMPLEX*16 array, dimension (2*N)
123: *
124: *  RWORK   (workspace) DOUBLE PRECISION array, dimension (7*N)
125: *
126: *  IWORK   (workspace) INTEGER array, dimension (5*N)
127: *
128: *  IFAIL   (output) INTEGER array, dimension (N)
129: *          If JOBZ = 'V', then if INFO = 0, the first M elements of
130: *          IFAIL are zero.  If INFO > 0, then IFAIL contains the
131: *          indices of the eigenvectors that failed to converge.
132: *          If JOBZ = 'N', then IFAIL is not referenced.
133: *
134: *  INFO    (output) INTEGER
135: *          = 0:  successful exit
136: *          < 0:  if INFO = -i, the i-th argument had an illegal value
137: *          > 0:  if INFO = i, then i eigenvectors failed to converge.
138: *                Their indices are stored in array IFAIL.
139: *
140: *  =====================================================================
141: *
142: *     .. Parameters ..
143:       DOUBLE PRECISION   ZERO, ONE
144:       PARAMETER          ( ZERO = 0.0D0, ONE = 1.0D0 )
145:       COMPLEX*16         CONE
146:       PARAMETER          ( CONE = ( 1.0D0, 0.0D0 ) )
147: *     ..
148: *     .. Local Scalars ..
149:       LOGICAL            ALLEIG, INDEIG, TEST, VALEIG, WANTZ
150:       CHARACTER          ORDER
151:       INTEGER            I, IINFO, IMAX, INDD, INDE, INDEE, INDIBL,
152:      $                   INDISP, INDIWK, INDRWK, INDTAU, INDWRK, ISCALE,
153:      $                   ITMP1, J, JJ, NSPLIT
154:       DOUBLE PRECISION   ABSTLL, ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN,
155:      $                   SIGMA, SMLNUM, TMP1, VLL, VUU
156: *     ..
157: *     .. External Functions ..
158:       LOGICAL            LSAME
159:       DOUBLE PRECISION   DLAMCH, ZLANHP
160:       EXTERNAL           LSAME, DLAMCH, ZLANHP
161: *     ..
162: *     .. External Subroutines ..
163:       EXTERNAL           DCOPY, DSCAL, DSTEBZ, DSTERF, XERBLA, ZDSCAL,
164:      $                   ZHPTRD, ZSTEIN, ZSTEQR, ZSWAP, ZUPGTR, ZUPMTR
165: *     ..
166: *     .. Intrinsic Functions ..
167:       INTRINSIC          DBLE, MAX, MIN, SQRT
168: *     ..
169: *     .. Executable Statements ..
170: *
171: *     Test the input parameters.
172: *
173:       WANTZ = LSAME( JOBZ, 'V' )
174:       ALLEIG = LSAME( RANGE, 'A' )
175:       VALEIG = LSAME( RANGE, 'V' )
176:       INDEIG = LSAME( RANGE, 'I' )
177: *
178:       INFO = 0
179:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
180:          INFO = -1
181:       ELSE IF( .NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
182:          INFO = -2
183:       ELSE IF( .NOT.( LSAME( UPLO, 'L' ) .OR. LSAME( UPLO, 'U' ) ) )
184:      $          THEN
185:          INFO = -3
186:       ELSE IF( N.LT.0 ) THEN
187:          INFO = -4
188:       ELSE
189:          IF( VALEIG ) THEN
190:             IF( N.GT.0 .AND. VU.LE.VL )
191:      $         INFO = -7
192:          ELSE IF( INDEIG ) THEN
193:             IF( IL.LT.1 .OR. IL.GT.MAX( 1, N ) ) THEN
194:                INFO = -8
195:             ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
196:                INFO = -9
197:             END IF
198:          END IF
199:       END IF
200:       IF( INFO.EQ.0 ) THEN
201:          IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) )
202:      $      INFO = -14
203:       END IF
204: *
205:       IF( INFO.NE.0 ) THEN
206:          CALL XERBLA( 'ZHPEVX', -INFO )
207:          RETURN
208:       END IF
209: *
210: *     Quick return if possible
211: *
212:       M = 0
213:       IF( N.EQ.0 )
214:      $   RETURN
215: *
216:       IF( N.EQ.1 ) THEN
217:          IF( ALLEIG .OR. INDEIG ) THEN
218:             M = 1
219:             W( 1 ) = AP( 1 )
220:          ELSE
221:             IF( VL.LT.DBLE( AP( 1 ) ) .AND. VU.GE.DBLE( AP( 1 ) ) ) THEN
222:                M = 1
223:                W( 1 ) = AP( 1 )
224:             END IF
225:          END IF
226:          IF( WANTZ )
227:      $      Z( 1, 1 ) = CONE
228:          RETURN
229:       END IF
230: *
231: *     Get machine constants.
232: *
233:       SAFMIN = DLAMCH( 'Safe minimum' )
234:       EPS = DLAMCH( 'Precision' )
235:       SMLNUM = SAFMIN / EPS
236:       BIGNUM = ONE / SMLNUM
237:       RMIN = SQRT( SMLNUM )
238:       RMAX = MIN( SQRT( BIGNUM ), ONE / SQRT( SQRT( SAFMIN ) ) )
239: *
240: *     Scale matrix to allowable range, if necessary.
241: *
242:       ISCALE = 0
243:       ABSTLL = ABSTOL
244:       IF( VALEIG ) THEN
245:          VLL = VL
246:          VUU = VU
247:       ELSE
248:          VLL = ZERO
249:          VUU = ZERO
250:       END IF
251:       ANRM = ZLANHP( 'M', UPLO, N, AP, RWORK )
252:       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
253:          ISCALE = 1
254:          SIGMA = RMIN / ANRM
255:       ELSE IF( ANRM.GT.RMAX ) THEN
256:          ISCALE = 1
257:          SIGMA = RMAX / ANRM
258:       END IF
259:       IF( ISCALE.EQ.1 ) THEN
260:          CALL ZDSCAL( ( N*( N+1 ) ) / 2, SIGMA, AP, 1 )
261:          IF( ABSTOL.GT.0 )
262:      $      ABSTLL = ABSTOL*SIGMA
263:          IF( VALEIG ) THEN
264:             VLL = VL*SIGMA
265:             VUU = VU*SIGMA
266:          END IF
267:       END IF
268: *
269: *     Call ZHPTRD to reduce Hermitian packed matrix to tridiagonal form.
270: *
271:       INDD = 1
272:       INDE = INDD + N
273:       INDRWK = INDE + N
274:       INDTAU = 1
275:       INDWRK = INDTAU + N
276:       CALL ZHPTRD( UPLO, N, AP, RWORK( INDD ), RWORK( INDE ),
277:      $             WORK( INDTAU ), IINFO )
278: *
279: *     If all eigenvalues are desired and ABSTOL is less than or equal
280: *     to zero, then call DSTERF or ZUPGTR and ZSTEQR.  If this fails
281: *     for some eigenvalue, then try DSTEBZ.
282: *
283:       TEST = .FALSE.
284:       IF (INDEIG) THEN
285:          IF (IL.EQ.1 .AND. IU.EQ.N) THEN
286:             TEST = .TRUE.
287:          END IF
288:       END IF
289:       IF ((ALLEIG .OR. TEST) .AND. (ABSTOL.LE.ZERO)) THEN
290:          CALL DCOPY( N, RWORK( INDD ), 1, W, 1 )
291:          INDEE = INDRWK + 2*N
292:          IF( .NOT.WANTZ ) THEN
293:             CALL DCOPY( N-1, RWORK( INDE ), 1, RWORK( INDEE ), 1 )
294:             CALL DSTERF( N, W, RWORK( INDEE ), INFO )
295:          ELSE
296:             CALL ZUPGTR( UPLO, N, AP, WORK( INDTAU ), Z, LDZ,
297:      $                   WORK( INDWRK ), IINFO )
298:             CALL DCOPY( N-1, RWORK( INDE ), 1, RWORK( INDEE ), 1 )
299:             CALL ZSTEQR( JOBZ, N, W, RWORK( INDEE ), Z, LDZ,
300:      $                   RWORK( INDRWK ), INFO )
301:             IF( INFO.EQ.0 ) THEN
302:                DO 10 I = 1, N
303:                   IFAIL( I ) = 0
304:    10          CONTINUE
305:             END IF
306:          END IF
307:          IF( INFO.EQ.0 ) THEN
308:             M = N
309:             GO TO 20
310:          END IF
311:          INFO = 0
312:       END IF
313: *
314: *     Otherwise, call DSTEBZ and, if eigenvectors are desired, ZSTEIN.
315: *
316:       IF( WANTZ ) THEN
317:          ORDER = 'B'
318:       ELSE
319:          ORDER = 'E'
320:       END IF
321:       INDIBL = 1
322:       INDISP = INDIBL + N
323:       INDIWK = INDISP + N
324:       CALL DSTEBZ( RANGE, ORDER, N, VLL, VUU, IL, IU, ABSTLL,
325:      $             RWORK( INDD ), RWORK( INDE ), M, NSPLIT, W,
326:      $             IWORK( INDIBL ), IWORK( INDISP ), RWORK( INDRWK ),
327:      $             IWORK( INDIWK ), INFO )
328: *
329:       IF( WANTZ ) THEN
330:          CALL ZSTEIN( N, RWORK( INDD ), RWORK( INDE ), M, W,
331:      $                IWORK( INDIBL ), IWORK( INDISP ), Z, LDZ,
332:      $                RWORK( INDRWK ), IWORK( INDIWK ), IFAIL, INFO )
333: *
334: *        Apply unitary matrix used in reduction to tridiagonal
335: *        form to eigenvectors returned by ZSTEIN.
336: *
337:          INDWRK = INDTAU + N
338:          CALL ZUPMTR( 'L', UPLO, 'N', N, M, AP, WORK( INDTAU ), Z, LDZ,
339:      $                WORK( INDWRK ), IINFO )
340:       END IF
341: *
342: *     If matrix was scaled, then rescale eigenvalues appropriately.
343: *
344:    20 CONTINUE
345:       IF( ISCALE.EQ.1 ) THEN
346:          IF( INFO.EQ.0 ) THEN
347:             IMAX = M
348:          ELSE
349:             IMAX = INFO - 1
350:          END IF
351:          CALL DSCAL( IMAX, ONE / SIGMA, W, 1 )
352:       END IF
353: *
354: *     If eigenvalues are not in order, then sort them, along with
355: *     eigenvectors.
356: *
357:       IF( WANTZ ) THEN
358:          DO 40 J = 1, M - 1
359:             I = 0
360:             TMP1 = W( J )
361:             DO 30 JJ = J + 1, M
362:                IF( W( JJ ).LT.TMP1 ) THEN
363:                   I = JJ
364:                   TMP1 = W( JJ )
365:                END IF
366:    30       CONTINUE
367: *
368:             IF( I.NE.0 ) THEN
369:                ITMP1 = IWORK( INDIBL+I-1 )
370:                W( I ) = W( J )
371:                IWORK( INDIBL+I-1 ) = IWORK( INDIBL+J-1 )
372:                W( J ) = TMP1
373:                IWORK( INDIBL+J-1 ) = ITMP1
374:                CALL ZSWAP( N, Z( 1, I ), 1, Z( 1, J ), 1 )
375:                IF( INFO.NE.0 ) THEN
376:                   ITMP1 = IFAIL( I )
377:                   IFAIL( I ) = IFAIL( J )
378:                   IFAIL( J ) = ITMP1
379:                END IF
380:             END IF
381:    40    CONTINUE
382:       END IF
383: *
384:       RETURN
385: *
386: *     End of ZHPEVX
387: *
388:       END
389: