001:       SUBROUTINE CHPEVD( 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:       REAL               RWORK( * ), W( * )
015:       COMPLEX            AP( * ), WORK( * ), Z( LDZ, * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  CHPEVD 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 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) REAL array, dimension (N)
061: *          If INFO = 0, the eigenvalues in ascending order.
062: *
063: *  Z       (output) COMPLEX 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 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) REAL array, dimension (MAX(1,LRWORK))
089: *          On exit, if INFO = 0, RWORK(1) returns the required LRWORK.
090: *
091: *  LRWORK  (input) INTEGER
092: *          The dimension of array RWORK.
093: *          If N <= 1,               LRWORK must be at least 1.
094: *          If JOBZ = 'N' and N > 1, LRWORK must be at least N.
095: *          If JOBZ = 'V' and N > 1, LRWORK must be at least
096: *                    1 + 5*N + 2*N**2.
097: *
098: *          If LRWORK = -1, then a workspace query is assumed; the
099: *          routine only calculates the required sizes of the WORK, RWORK
100: *          and IWORK arrays, returns these values as the first entries
101: *          of the WORK, RWORK and IWORK arrays, and no error message
102: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
103: *
104: *  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK))
105: *          On exit, if INFO = 0, IWORK(1) returns the required LIWORK.
106: *
107: *  LIWORK  (input) INTEGER
108: *          The dimension of array IWORK.
109: *          If JOBZ  = 'N' or N <= 1, LIWORK must be at least 1.
110: *          If JOBZ  = 'V' and N > 1, LIWORK must be at least 3 + 5*N.
111: *
112: *          If LIWORK = -1, then a workspace query is assumed; the
113: *          routine only calculates the required sizes of the WORK, RWORK
114: *          and IWORK arrays, returns these values as the first entries
115: *          of the WORK, RWORK and IWORK arrays, and no error message
116: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
117: *
118: *  INFO    (output) INTEGER
119: *          = 0:  successful exit
120: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
121: *          > 0:  if INFO = i, the algorithm failed to converge; i
122: *                off-diagonal elements of an intermediate tridiagonal
123: *                form did not converge to zero.
124: *
125: *  =====================================================================
126: *
127: *     .. Parameters ..
128:       REAL               ZERO, ONE
129:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
130:       COMPLEX            CONE
131:       PARAMETER          ( CONE = ( 1.0E+0, 0.0E+0 ) )
132: *     ..
133: *     .. Local Scalars ..
134:       LOGICAL            LQUERY, WANTZ
135:       INTEGER            IINFO, IMAX, INDE, INDRWK, INDTAU, INDWRK,
136:      $                   ISCALE, LIWMIN, LLRWK, LLWRK, LRWMIN, LWMIN
137:       REAL               ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA,
138:      $                   SMLNUM
139: *     ..
140: *     .. External Functions ..
141:       LOGICAL            LSAME
142:       REAL               CLANHP, SLAMCH
143:       EXTERNAL           LSAME, CLANHP, SLAMCH
144: *     ..
145: *     .. External Subroutines ..
146:       EXTERNAL           CHPTRD, CSSCAL, CSTEDC, CUPMTR, SSCAL, SSTERF,
147:      $                   XERBLA
148: *     ..
149: *     .. Intrinsic Functions ..
150:       INTRINSIC          SQRT
151: *     ..
152: *     .. Executable Statements ..
153: *
154: *     Test the input parameters.
155: *
156:       WANTZ = LSAME( JOBZ, 'V' )
157:       LQUERY = ( LWORK.EQ.-1 .OR. LRWORK.EQ.-1 .OR. LIWORK.EQ.-1 )
158: *
159:       INFO = 0
160:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
161:          INFO = -1
162:       ELSE IF( .NOT.( LSAME( UPLO, 'L' ) .OR. LSAME( UPLO, 'U' ) ) )
163:      $          THEN
164:          INFO = -2
165:       ELSE IF( N.LT.0 ) THEN
166:          INFO = -3
167:       ELSE IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
168:          INFO = -7
169:       END IF
170: *
171:       IF( INFO.EQ.0 ) THEN
172:          IF( N.LE.1 ) THEN
173:             LWMIN = 1
174:             LIWMIN = 1
175:             LRWMIN = 1
176:          ELSE
177:             IF( WANTZ ) THEN
178:                LWMIN = 2*N
179:                LRWMIN = 1 + 5*N + 2*N**2
180:                LIWMIN = 3 + 5*N
181:             ELSE
182:                LWMIN = N
183:                LRWMIN = N
184:                LIWMIN = 1
185:             END IF
186:          END IF
187:          WORK( 1 ) = LWMIN
188:          RWORK( 1 ) = LRWMIN
189:          IWORK( 1 ) = LIWMIN
190: *
191:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
192:             INFO = -9
193:          ELSE IF( LRWORK.LT.LRWMIN .AND. .NOT.LQUERY ) THEN
194:             INFO = -11
195:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
196:             INFO = -13
197:          END IF
198:       END IF
199: *
200:       IF( INFO.NE.0 ) THEN
201:          CALL XERBLA( 'CHPEVD', -INFO )
202:          RETURN 
203:       ELSE IF( LQUERY ) THEN
204:          RETURN
205:       END IF
206: *
207: *     Quick return if possible
208: *
209:       IF( N.EQ.0 )
210:      $   RETURN
211: *
212:       IF( N.EQ.1 ) THEN
213:          W( 1 ) = AP( 1 )
214:          IF( WANTZ )
215:      $      Z( 1, 1 ) = CONE
216:          RETURN 
217:       END IF
218: *
219: *     Get machine constants.
220: *
221:       SAFMIN = SLAMCH( 'Safe minimum' )
222:       EPS = SLAMCH( 'Precision' )
223:       SMLNUM = SAFMIN / EPS
224:       BIGNUM = ONE / SMLNUM
225:       RMIN = SQRT( SMLNUM )
226:       RMAX = SQRT( BIGNUM )
227: *
228: *     Scale matrix to allowable range, if necessary.
229: *
230:       ANRM = CLANHP( 'M', UPLO, N, AP, RWORK )
231:       ISCALE = 0
232:       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
233:          ISCALE = 1
234:          SIGMA = RMIN / ANRM
235:       ELSE IF( ANRM.GT.RMAX ) THEN
236:          ISCALE = 1
237:          SIGMA = RMAX / ANRM
238:       END IF
239:       IF( ISCALE.EQ.1 ) THEN
240:          CALL CSSCAL( ( N*( N+1 ) ) / 2, SIGMA, AP, 1 )
241:       END IF
242: *
243: *     Call CHPTRD to reduce Hermitian packed matrix to tridiagonal form.
244: *
245:       INDE = 1
246:       INDTAU = 1
247:       INDRWK = INDE + N
248:       INDWRK = INDTAU + N
249:       LLWRK = LWORK - INDWRK + 1
250:       LLRWK = LRWORK - INDRWK + 1
251:       CALL CHPTRD( UPLO, N, AP, W, RWORK( INDE ), WORK( INDTAU ),
252:      $             IINFO )
253: *
254: *     For eigenvalues only, call SSTERF.  For eigenvectors, first call
255: *     CUPGTR to generate the orthogonal matrix, then call CSTEDC.
256: *
257:       IF( .NOT.WANTZ ) THEN
258:          CALL SSTERF( N, W, RWORK( INDE ), INFO )
259:       ELSE
260:          CALL CSTEDC( 'I', N, W, RWORK( INDE ), Z, LDZ, WORK( INDWRK ),
261:      $                LLWRK, RWORK( INDRWK ), LLRWK, IWORK, LIWORK,
262:      $                INFO )
263:          CALL CUPMTR( 'L', UPLO, 'N', N, N, AP, WORK( INDTAU ), Z, LDZ,
264:      $                WORK( INDWRK ), IINFO )
265:       END IF
266: *
267: *     If matrix was scaled, then rescale eigenvalues appropriately.
268: *
269:       IF( ISCALE.EQ.1 ) THEN
270:          IF( INFO.EQ.0 ) THEN
271:             IMAX = N
272:          ELSE
273:             IMAX = INFO - 1
274:          END IF
275:          CALL SSCAL( IMAX, ONE / SIGMA, W, 1 )
276:       END IF
277: *
278:       WORK( 1 ) = LWMIN
279:       RWORK( 1 ) = LRWMIN
280:       IWORK( 1 ) = LIWMIN
281:       RETURN
282: *
283: *     End of CHPEVD
284: *
285:       END
286: