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: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
006: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       CHARACTER          JOBZ, UPLO
011:       INTEGER            INFO, LDZ, LIWORK, LRWORK, LWORK, N
012: *     ..
013: *     .. Array Arguments ..
014:       INTEGER            IWORK( * )
015:       REAL               RWORK( * ), W( * )
016:       COMPLEX            AP( * ), WORK( * ), Z( LDZ, * )
017: *     ..
018: *
019: *  Purpose
020: *  =======
021: *
022: *  CHPEVD computes all the eigenvalues and, optionally, eigenvectors of
023: *  a complex Hermitian matrix A in packed storage.  If eigenvectors are
024: *  desired, it uses a divide and conquer algorithm.
025: *
026: *  The divide and conquer algorithm makes very mild assumptions about
027: *  floating point arithmetic. It will work on machines with a guard
028: *  digit in add/subtract, or on those binary machines without guard
029: *  digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or
030: *  Cray-2. It could conceivably fail on hexadecimal or decimal machines
031: *  without guard digits, but we know of none.
032: *
033: *  Arguments
034: *  =========
035: *
036: *  JOBZ    (input) CHARACTER*1
037: *          = 'N':  Compute eigenvalues only;
038: *          = 'V':  Compute eigenvalues and eigenvectors.
039: *
040: *  UPLO    (input) CHARACTER*1
041: *          = 'U':  Upper triangle of A is stored;
042: *          = 'L':  Lower triangle of A is stored.
043: *
044: *  N       (input) INTEGER
045: *          The order of the matrix A.  N >= 0.
046: *
047: *  AP      (input/output) COMPLEX array, dimension (N*(N+1)/2)
048: *          On entry, the upper or lower triangle of the Hermitian matrix
049: *          A, packed columnwise in a linear array.  The j-th column of A
050: *          is stored in the array AP as follows:
051: *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
052: *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
053: *
054: *          On exit, AP is overwritten by values generated during the
055: *          reduction to tridiagonal form.  If UPLO = 'U', the diagonal
056: *          and first superdiagonal of the tridiagonal matrix T overwrite
057: *          the corresponding elements of A, and if UPLO = 'L', the
058: *          diagonal and first subdiagonal of T overwrite the
059: *          corresponding elements of A.
060: *
061: *  W       (output) REAL array, dimension (N)
062: *          If INFO = 0, the eigenvalues in ascending order.
063: *
064: *  Z       (output) COMPLEX array, dimension (LDZ, N)
065: *          If JOBZ = 'V', then if INFO = 0, Z contains the orthonormal
066: *          eigenvectors of the matrix A, with the i-th column of Z
067: *          holding the eigenvector associated with W(i).
068: *          If JOBZ = 'N', then Z is not referenced.
069: *
070: *  LDZ     (input) INTEGER
071: *          The leading dimension of the array Z.  LDZ >= 1, and if
072: *          JOBZ = 'V', LDZ >= max(1,N).
073: *
074: *  WORK    (workspace/output) COMPLEX array, dimension (MAX(1,LWORK))
075: *          On exit, if INFO = 0, WORK(1) returns the required LWORK.
076: *
077: *  LWORK   (input) INTEGER
078: *          The dimension of array WORK.
079: *          If N <= 1,               LWORK must be at least 1.
080: *          If JOBZ = 'N' and N > 1, LWORK must be at least N.
081: *          If JOBZ = 'V' and N > 1, LWORK must be at least 2*N.
082: *
083: *          If LWORK = -1, then a workspace query is assumed; the routine
084: *          only calculates the required sizes of the WORK, RWORK and
085: *          IWORK arrays, returns these values as the first entries of
086: *          the WORK, RWORK and IWORK arrays, and no error message
087: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
088: *
089: *  RWORK   (workspace/output) REAL array, dimension (MAX(1,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:       REAL               ZERO, ONE
130:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
131:       COMPLEX            CONE
132:       PARAMETER          ( CONE = ( 1.0E+0, 0.0E+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:       REAL               ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA,
139:      $                   SMLNUM
140: *     ..
141: *     .. External Functions ..
142:       LOGICAL            LSAME
143:       REAL               CLANHP, SLAMCH
144:       EXTERNAL           LSAME, CLANHP, SLAMCH
145: *     ..
146: *     .. External Subroutines ..
147:       EXTERNAL           CHPTRD, CSSCAL, CSTEDC, CUPMTR, SSCAL, SSTERF,
148:      $                   XERBLA
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( 'CHPEVD', -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 = SLAMCH( 'Safe minimum' )
223:       EPS = SLAMCH( '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 = CLANHP( '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 CSSCAL( ( N*( N+1 ) ) / 2, SIGMA, AP, 1 )
242:       END IF
243: *
244: *     Call CHPTRD 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 CHPTRD( UPLO, N, AP, W, RWORK( INDE ), WORK( INDTAU ),
253:      $             IINFO )
254: *
255: *     For eigenvalues only, call SSTERF.  For eigenvectors, first call
256: *     CUPGTR to generate the orthogonal matrix, then call CSTEDC.
257: *
258:       IF( .NOT.WANTZ ) THEN
259:          CALL SSTERF( N, W, RWORK( INDE ), INFO )
260:       ELSE
261:          CALL CSTEDC( 'I', N, W, RWORK( INDE ), Z, LDZ, WORK( INDWRK ),
262:      $                LLWRK, RWORK( INDRWK ), LLRWK, IWORK, LIWORK,
263:      $                INFO )
264:          CALL CUPMTR( '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 SSCAL( 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 CHPEVD
285: *
286:       END
287: