001:       SUBROUTINE ZHPEVD( JOBZ, UPLO, N, AP, W, Z, LDZ, WORK, LWORK,
002:      $                   RWORK, LRWORK, IWORK, LIWORK, INFO )
003: *
004: *  -- LAPACK driver routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       CHARACTER          JOBZ, UPLO
010:       INTEGER            INFO, LDZ, LIWORK, LRWORK, LWORK, N
011: *     ..
012: *     .. Array Arguments ..
013:       INTEGER            IWORK( * )
014:       DOUBLE PRECISION   RWORK( * ), W( * )
015:       COMPLEX*16         AP( * ), WORK( * ), Z( LDZ, * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  ZHPEVD computes all the eigenvalues and, optionally, eigenvectors of
022: *  a complex Hermitian matrix A in packed storage.  If eigenvectors are
023: *  desired, it uses a divide and conquer algorithm.
024: *
025: *  The divide and conquer algorithm makes very mild assumptions about
026: *  floating point arithmetic. It will work on machines with a guard
027: *  digit in add/subtract, or on those binary machines without guard
028: *  digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or
029: *  Cray-2. It could conceivably fail on hexadecimal or decimal machines
030: *  without guard digits, but we know of none.
031: *
032: *  Arguments
033: *  =========
034: *
035: *  JOBZ    (input) CHARACTER*1
036: *          = 'N':  Compute eigenvalues only;
037: *          = 'V':  Compute eigenvalues and eigenvectors.
038: *
039: *  UPLO    (input) CHARACTER*1
040: *          = 'U':  Upper triangle of A is stored;
041: *          = 'L':  Lower triangle of A is stored.
042: *
043: *  N       (input) INTEGER
044: *          The order of the matrix A.  N >= 0.
045: *
046: *  AP      (input/output) COMPLEX*16 array, dimension (N*(N+1)/2)
047: *          On entry, the upper or lower triangle of the Hermitian matrix
048: *          A, packed columnwise in a linear array.  The j-th column of A
049: *          is stored in the array AP as follows:
050: *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
051: *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
052: *
053: *          On exit, AP is overwritten by values generated during the
054: *          reduction to tridiagonal form.  If UPLO = 'U', the diagonal
055: *          and first superdiagonal of the tridiagonal matrix T overwrite
056: *          the corresponding elements of A, and if UPLO = 'L', the
057: *          diagonal and first subdiagonal of T overwrite the
058: *          corresponding elements of A.
059: *
060: *  W       (output) DOUBLE PRECISION array, dimension (N)
061: *          If INFO = 0, the eigenvalues in ascending order.
062: *
063: *  Z       (output) COMPLEX*16 array, dimension (LDZ, N)
064: *          If JOBZ = 'V', then if INFO = 0, Z contains the orthonormal
065: *          eigenvectors of the matrix A, with the i-th column of Z
066: *          holding the eigenvector associated with W(i).
067: *          If JOBZ = 'N', then Z is not referenced.
068: *
069: *  LDZ     (input) INTEGER
070: *          The leading dimension of the array Z.  LDZ >= 1, and if
071: *          JOBZ = 'V', LDZ >= max(1,N).
072: *
073: *  WORK    (workspace/output) COMPLEX*16 array, dimension (MAX(1,LWORK))
074: *          On exit, if INFO = 0, WORK(1) returns the required LWORK.
075: *
076: *  LWORK   (input) INTEGER
077: *          The dimension of array WORK.
078: *          If N <= 1,               LWORK must be at least 1.
079: *          If JOBZ = 'N' and N > 1, LWORK must be at least N.
080: *          If JOBZ = 'V' and N > 1, LWORK must be at least 2*N.
081: *
082: *          If LWORK = -1, then a workspace query is assumed; the routine
083: *          only calculates the required sizes of the WORK, RWORK and
084: *          IWORK arrays, returns these values as the first entries of
085: *          the WORK, RWORK and IWORK arrays, and no error message
086: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
087: *
088: *  RWORK   (workspace/output) DOUBLE PRECISION array,
089: *                                         dimension (LRWORK)
090: *          On exit, if INFO = 0, RWORK(1) returns the required LRWORK.
091: *
092: *  LRWORK  (input) INTEGER
093: *          The dimension of array RWORK.
094: *          If N <= 1,               LRWORK must be at least 1.
095: *          If JOBZ = 'N' and N > 1, LRWORK must be at least N.
096: *          If JOBZ = 'V' and N > 1, LRWORK must be at least
097: *                    1 + 5*N + 2*N**2.
098: *
099: *          If LRWORK = -1, then a workspace query is assumed; the
100: *          routine only calculates the required sizes of the WORK, RWORK
101: *          and IWORK arrays, returns these values as the first entries
102: *          of the WORK, RWORK and IWORK arrays, and no error message
103: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
104: *
105: *  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK))
106: *          On exit, if INFO = 0, IWORK(1) returns the required LIWORK.
107: *
108: *  LIWORK  (input) INTEGER
109: *          The dimension of array IWORK.
110: *          If JOBZ  = 'N' or N <= 1, LIWORK must be at least 1.
111: *          If JOBZ  = 'V' and N > 1, LIWORK must be at least 3 + 5*N.
112: *
113: *          If LIWORK = -1, then a workspace query is assumed; the
114: *          routine only calculates the required sizes of the WORK, RWORK
115: *          and IWORK arrays, returns these values as the first entries
116: *          of the WORK, RWORK and IWORK arrays, and no error message
117: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
118: *
119: *  INFO    (output) INTEGER
120: *          = 0:  successful exit
121: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
122: *          > 0:  if INFO = i, the algorithm failed to converge; i
123: *                off-diagonal elements of an intermediate tridiagonal
124: *                form did not converge to zero.
125: *
126: *  =====================================================================
127: *
128: *     .. Parameters ..
129:       DOUBLE PRECISION   ZERO, ONE
130:       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+0 )
131:       COMPLEX*16         CONE
132:       PARAMETER          ( CONE = ( 1.0D+0, 0.0D+0 ) )
133: *     ..
134: *     .. Local Scalars ..
135:       LOGICAL            LQUERY, WANTZ
136:       INTEGER            IINFO, IMAX, INDE, INDRWK, INDTAU, INDWRK,
137:      $                   ISCALE, LIWMIN, LLRWK, LLWRK, LRWMIN, LWMIN
138:       DOUBLE PRECISION   ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA,
139:      $                   SMLNUM
140: *     ..
141: *     .. External Functions ..
142:       LOGICAL            LSAME
143:       DOUBLE PRECISION   DLAMCH, ZLANHP
144:       EXTERNAL           LSAME, DLAMCH, ZLANHP
145: *     ..
146: *     .. External Subroutines ..
147:       EXTERNAL           DSCAL, DSTERF, XERBLA, ZDSCAL, ZHPTRD, ZSTEDC,
148:      $                   ZUPMTR
149: *     ..
150: *     .. Intrinsic Functions ..
151:       INTRINSIC          SQRT
152: *     ..
153: *     .. Executable Statements ..
154: *
155: *     Test the input parameters.
156: *
157:       WANTZ = LSAME( JOBZ, 'V' )
158:       LQUERY = ( LWORK.EQ.-1 .OR. LRWORK.EQ.-1 .OR. LIWORK.EQ.-1 )
159: *
160:       INFO = 0
161:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
162:          INFO = -1
163:       ELSE IF( .NOT.( LSAME( UPLO, 'L' ) .OR. LSAME( UPLO, 'U' ) ) )
164:      $          THEN
165:          INFO = -2
166:       ELSE IF( N.LT.0 ) THEN
167:          INFO = -3
168:       ELSE IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
169:          INFO = -7
170:       END IF
171: *
172:       IF( INFO.EQ.0 ) THEN
173:          IF( N.LE.1 ) THEN
174:             LWMIN = 1
175:             LIWMIN = 1
176:             LRWMIN = 1
177:          ELSE
178:             IF( WANTZ ) THEN
179:                LWMIN = 2*N
180:                LRWMIN = 1 + 5*N + 2*N**2
181:                LIWMIN = 3 + 5*N
182:             ELSE
183:                LWMIN = N
184:                LRWMIN = N
185:                LIWMIN = 1
186:             END IF
187:          END IF
188:          WORK( 1 ) = LWMIN
189:          RWORK( 1 ) = LRWMIN
190:          IWORK( 1 ) = LIWMIN
191: *
192:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
193:             INFO = -9
194:          ELSE IF( LRWORK.LT.LRWMIN .AND. .NOT.LQUERY ) THEN
195:             INFO = -11
196:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
197:             INFO = -13
198:          END IF
199:       END IF
200: *
201:       IF( INFO.NE.0 ) THEN
202:          CALL XERBLA( 'ZHPEVD', -INFO )
203:          RETURN
204:       ELSE IF( LQUERY ) THEN
205:          RETURN
206:       END IF
207: *
208: *     Quick return if possible
209: *
210:       IF( N.EQ.0 )
211:      $   RETURN
212: *
213:       IF( N.EQ.1 ) THEN
214:          W( 1 ) = AP( 1 )
215:          IF( WANTZ )
216:      $      Z( 1, 1 ) = CONE
217:          RETURN
218:       END IF
219: *
220: *     Get machine constants.
221: *
222:       SAFMIN = DLAMCH( 'Safe minimum' )
223:       EPS = DLAMCH( 'Precision' )
224:       SMLNUM = SAFMIN / EPS
225:       BIGNUM = ONE / SMLNUM
226:       RMIN = SQRT( SMLNUM )
227:       RMAX = SQRT( BIGNUM )
228: *
229: *     Scale matrix to allowable range, if necessary.
230: *
231:       ANRM = ZLANHP( 'M', UPLO, N, AP, RWORK )
232:       ISCALE = 0
233:       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
234:          ISCALE = 1
235:          SIGMA = RMIN / ANRM
236:       ELSE IF( ANRM.GT.RMAX ) THEN
237:          ISCALE = 1
238:          SIGMA = RMAX / ANRM
239:       END IF
240:       IF( ISCALE.EQ.1 ) THEN
241:          CALL ZDSCAL( ( N*( N+1 ) ) / 2, SIGMA, AP, 1 )
242:       END IF
243: *
244: *     Call ZHPTRD to reduce Hermitian packed matrix to tridiagonal form.
245: *
246:       INDE = 1
247:       INDTAU = 1
248:       INDRWK = INDE + N
249:       INDWRK = INDTAU + N
250:       LLWRK = LWORK - INDWRK + 1
251:       LLRWK = LRWORK - INDRWK + 1
252:       CALL ZHPTRD( UPLO, N, AP, W, RWORK( INDE ), WORK( INDTAU ),
253:      $             IINFO )
254: *
255: *     For eigenvalues only, call DSTERF.  For eigenvectors, first call
256: *     ZUPGTR to generate the orthogonal matrix, then call ZSTEDC.
257: *
258:       IF( .NOT.WANTZ ) THEN
259:          CALL DSTERF( N, W, RWORK( INDE ), INFO )
260:       ELSE
261:          CALL ZSTEDC( 'I', N, W, RWORK( INDE ), Z, LDZ, WORK( INDWRK ),
262:      $                LLWRK, RWORK( INDRWK ), LLRWK, IWORK, LIWORK,
263:      $                INFO )
264:          CALL ZUPMTR( 'L', UPLO, 'N', N, N, AP, WORK( INDTAU ), Z, LDZ,
265:      $                WORK( INDWRK ), IINFO )
266:       END IF
267: *
268: *     If matrix was scaled, then rescale eigenvalues appropriately.
269: *
270:       IF( ISCALE.EQ.1 ) THEN
271:          IF( INFO.EQ.0 ) THEN
272:             IMAX = N
273:          ELSE
274:             IMAX = INFO - 1
275:          END IF
276:          CALL DSCAL( IMAX, ONE / SIGMA, W, 1 )
277:       END IF
278: *
279:       WORK( 1 ) = LWMIN
280:       RWORK( 1 ) = LRWMIN
281:       IWORK( 1 ) = LIWMIN
282:       RETURN
283: *
284: *     End of ZHPEVD
285: *
286:       END
287: