001:       SUBROUTINE CHBEVD( JOBZ, UPLO, N, KD, AB, LDAB, W, Z, LDZ, WORK,
002:      $                   LWORK, 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, KD, LDAB, LDZ, LIWORK, LRWORK, LWORK, N
011: *     ..
012: *     .. Array Arguments ..
013:       INTEGER            IWORK( * )
014:       REAL               RWORK( * ), W( * )
015:       COMPLEX            AB( LDAB, * ), WORK( * ), Z( LDZ, * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  CHBEVD computes all the eigenvalues and, optionally, eigenvectors of
022: *  a complex Hermitian band matrix A.  If eigenvectors are desired, it
023: *  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: *  KD      (input) INTEGER
047: *          The number of superdiagonals of the matrix A if UPLO = 'U',
048: *          or the number of subdiagonals if UPLO = 'L'.  KD >= 0.
049: *
050: *  AB      (input/output) COMPLEX array, dimension (LDAB, N)
051: *          On entry, the upper or lower triangle of the Hermitian band
052: *          matrix A, stored in the first KD+1 rows of the array.  The
053: *          j-th column of A is stored in the j-th column of the array AB
054: *          as follows:
055: *          if UPLO = 'U', AB(kd+1+i-j,j) = A(i,j) for max(1,j-kd)<=i<=j;
056: *          if UPLO = 'L', AB(1+i-j,j)    = A(i,j) for j<=i<=min(n,j+kd).
057: *
058: *          On exit, AB is overwritten by values generated during the
059: *          reduction to tridiagonal form.  If UPLO = 'U', the first
060: *          superdiagonal and the diagonal of the tridiagonal matrix T
061: *          are returned in rows KD and KD+1 of AB, and if UPLO = 'L',
062: *          the diagonal and first subdiagonal of T are returned in the
063: *          first two rows of AB.
064: *
065: *  LDAB    (input) INTEGER
066: *          The leading dimension of the array AB.  LDAB >= KD + 1.
067: *
068: *  W       (output) REAL array, dimension (N)
069: *          If INFO = 0, the eigenvalues in ascending order.
070: *
071: *  Z       (output) COMPLEX array, dimension (LDZ, N)
072: *          If JOBZ = 'V', then if INFO = 0, Z contains the orthonormal
073: *          eigenvectors of the matrix A, with the i-th column of Z
074: *          holding the eigenvector associated with W(i).
075: *          If JOBZ = 'N', then Z is not referenced.
076: *
077: *  LDZ     (input) INTEGER
078: *          The leading dimension of the array Z.  LDZ >= 1, and if
079: *          JOBZ = 'V', LDZ >= max(1,N).
080: *
081: *  WORK    (workspace/output) COMPLEX array, dimension (MAX(1,LWORK))
082: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
083: *
084: *  LWORK   (input) INTEGER
085: *          The dimension of the array WORK.
086: *          If N <= 1,               LWORK must be at least 1.
087: *          If JOBZ = 'N' and N > 1, LWORK must be at least N.
088: *          If JOBZ = 'V' and N > 1, LWORK must be at least 2*N**2.
089: *
090: *          If LWORK = -1, then a workspace query is assumed; the routine
091: *          only calculates the optimal sizes of the WORK, RWORK and
092: *          IWORK arrays, returns these values as the first entries of
093: *          the WORK, RWORK and IWORK arrays, and no error message
094: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
095: *
096: *  RWORK   (workspace/output) REAL array,
097: *                                         dimension (LRWORK)
098: *          On exit, if INFO = 0, RWORK(1) returns the optimal LRWORK.
099: *
100: *  LRWORK  (input) INTEGER
101: *          The dimension of array RWORK.
102: *          If N <= 1,               LRWORK must be at least 1.
103: *          If JOBZ = 'N' and N > 1, LRWORK must be at least N.
104: *          If JOBZ = 'V' and N > 1, LRWORK must be at least
105: *                        1 + 5*N + 2*N**2.
106: *
107: *          If LRWORK = -1, then a workspace query is assumed; the
108: *          routine only calculates the optimal sizes of the WORK, RWORK
109: *          and IWORK arrays, returns these values as the first entries
110: *          of the WORK, RWORK and IWORK arrays, and no error message
111: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
112: *
113: *  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK))
114: *          On exit, if INFO = 0, IWORK(1) returns the optimal LIWORK.
115: *
116: *  LIWORK  (input) INTEGER
117: *          The dimension of array IWORK.
118: *          If JOBZ = 'N' or N <= 1, LIWORK must be at least 1.
119: *          If JOBZ = 'V' and N > 1, LIWORK must be at least 3 + 5*N .
120: *
121: *          If LIWORK = -1, then a workspace query is assumed; the
122: *          routine only calculates the optimal sizes of the WORK, RWORK
123: *          and IWORK arrays, returns these values as the first entries
124: *          of the WORK, RWORK and IWORK arrays, and no error message
125: *          related to LWORK or LRWORK or LIWORK is issued by XERBLA.
126: *
127: *  INFO    (output) INTEGER
128: *          = 0:  successful exit.
129: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
130: *          > 0:  if INFO = i, the algorithm failed to converge; i
131: *                off-diagonal elements of an intermediate tridiagonal
132: *                form did not converge to zero.
133: *
134: *  =====================================================================
135: *
136: *     .. Parameters ..
137:       REAL               ZERO, ONE
138:       PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0 )
139:       COMPLEX            CZERO, CONE
140:       PARAMETER          ( CZERO = ( 0.0E0, 0.0E0 ),
141:      $                   CONE = ( 1.0E0, 0.0E0 ) )
142: *     ..
143: *     .. Local Scalars ..
144:       LOGICAL            LOWER, LQUERY, WANTZ
145:       INTEGER            IINFO, IMAX, INDE, INDWK2, INDWRK, ISCALE,
146:      $                   LIWMIN, LLRWK, LLWK2, LRWMIN, LWMIN
147:       REAL               ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA,
148:      $                   SMLNUM
149: *     ..
150: *     .. External Functions ..
151:       LOGICAL            LSAME
152:       REAL               CLANHB, SLAMCH
153:       EXTERNAL           LSAME, CLANHB, SLAMCH
154: *     ..
155: *     .. External Subroutines ..
156:       EXTERNAL           CGEMM, CHBTRD, CLACPY, CLASCL, CSTEDC, SSCAL,
157:      $                   SSTERF, XERBLA
158: *     ..
159: *     .. Intrinsic Functions ..
160:       INTRINSIC          SQRT
161: *     ..
162: *     .. Executable Statements ..
163: *
164: *     Test the input parameters.
165: *
166:       WANTZ = LSAME( JOBZ, 'V' )
167:       LOWER = LSAME( UPLO, 'L' )
168:       LQUERY = ( LWORK.EQ.-1 .OR. LIWORK.EQ.-1 .OR. LRWORK.EQ.-1 )
169: *
170:       INFO = 0
171:       IF( N.LE.1 ) THEN
172:          LWMIN = 1
173:          LRWMIN = 1
174:          LIWMIN = 1
175:       ELSE
176:          IF( WANTZ ) THEN
177:             LWMIN = 2*N**2
178:             LRWMIN = 1 + 5*N + 2*N**2
179:             LIWMIN = 3 + 5*N
180:          ELSE
181:             LWMIN = N
182:             LRWMIN = N
183:             LIWMIN = 1
184:          END IF
185:       END IF
186:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
187:          INFO = -1
188:       ELSE IF( .NOT.( LOWER .OR. LSAME( UPLO, 'U' ) ) ) THEN
189:          INFO = -2
190:       ELSE IF( N.LT.0 ) THEN
191:          INFO = -3
192:       ELSE IF( KD.LT.0 ) THEN
193:          INFO = -4
194:       ELSE IF( LDAB.LT.KD+1 ) THEN
195:          INFO = -6
196:       ELSE IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
197:          INFO = -9
198:       END IF
199: *
200:       IF( INFO.EQ.0 ) THEN
201:          WORK( 1 ) = LWMIN
202:          RWORK( 1 ) = LRWMIN
203:          IWORK( 1 ) = LIWMIN
204: *
205:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
206:             INFO = -11
207:          ELSE IF( LRWORK.LT.LRWMIN .AND. .NOT.LQUERY ) THEN
208:             INFO = -13
209:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
210:             INFO = -15
211:          END IF
212:       END IF
213: *
214:       IF( INFO.NE.0 ) THEN
215:          CALL XERBLA( 'CHBEVD', -INFO )
216:          RETURN
217:       ELSE IF( LQUERY ) THEN
218:          RETURN
219:       END IF
220: *
221: *     Quick return if possible
222: *
223:       IF( N.EQ.0 )
224:      $   RETURN 
225: *
226:       IF( N.EQ.1 ) THEN
227:          W( 1 ) = AB( 1, 1 )
228:          IF( WANTZ )
229:      $      Z( 1, 1 ) = CONE
230:          RETURN 
231:       END IF
232: *
233: *     Get machine constants.
234: *
235:       SAFMIN = SLAMCH( 'Safe minimum' )
236:       EPS = SLAMCH( 'Precision' )
237:       SMLNUM = SAFMIN / EPS
238:       BIGNUM = ONE / SMLNUM
239:       RMIN = SQRT( SMLNUM )
240:       RMAX = SQRT( BIGNUM )
241: *
242: *     Scale matrix to allowable range, if necessary.
243: *
244:       ANRM = CLANHB( 'M', UPLO, N, KD, AB, LDAB, RWORK )
245:       ISCALE = 0
246:       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
247:          ISCALE = 1
248:          SIGMA = RMIN / ANRM
249:       ELSE IF( ANRM.GT.RMAX ) THEN
250:          ISCALE = 1
251:          SIGMA = RMAX / ANRM
252:       END IF
253:       IF( ISCALE.EQ.1 ) THEN
254:          IF( LOWER ) THEN
255:             CALL CLASCL( 'B', KD, KD, ONE, SIGMA, N, N, AB, LDAB, INFO )
256:          ELSE
257:             CALL CLASCL( 'Q', KD, KD, ONE, SIGMA, N, N, AB, LDAB, INFO )
258:          END IF
259:       END IF
260: *
261: *     Call CHBTRD to reduce Hermitian band matrix to tridiagonal form.
262: *
263:       INDE = 1
264:       INDWRK = INDE + N
265:       INDWK2 = 1 + N*N
266:       LLWK2 = LWORK - INDWK2 + 1
267:       LLRWK = LRWORK - INDWRK + 1
268:       CALL CHBTRD( JOBZ, UPLO, N, KD, AB, LDAB, W, RWORK( INDE ), Z,
269:      $             LDZ, WORK, IINFO )
270: *
271: *     For eigenvalues only, call SSTERF.  For eigenvectors, call CSTEDC.
272: *
273:       IF( .NOT.WANTZ ) THEN
274:          CALL SSTERF( N, W, RWORK( INDE ), INFO )
275:       ELSE
276:          CALL CSTEDC( 'I', N, W, RWORK( INDE ), WORK, N, WORK( INDWK2 ),
277:      $                LLWK2, RWORK( INDWRK ), LLRWK, IWORK, LIWORK,
278:      $                INFO )
279:          CALL CGEMM( 'N', 'N', N, N, N, CONE, Z, LDZ, WORK, N, CZERO,
280:      $               WORK( INDWK2 ), N )
281:          CALL CLACPY( 'A', N, N, WORK( INDWK2 ), N, Z, LDZ )
282:       END IF
283: *
284: *     If matrix was scaled, then rescale eigenvalues appropriately.
285: *
286:       IF( ISCALE.EQ.1 ) THEN
287:          IF( INFO.EQ.0 ) THEN
288:             IMAX = N
289:          ELSE
290:             IMAX = INFO - 1
291:          END IF
292:          CALL SSCAL( IMAX, ONE / SIGMA, W, 1 )
293:       END IF
294: *
295:       WORK( 1 ) = LWMIN
296:       RWORK( 1 ) = LRWMIN
297:       IWORK( 1 ) = LIWMIN
298:       RETURN
299: *
300: *     End of CHBEVD
301: *
302:       END
303: