1       SUBROUTINE ZHBEVX( JOBZ, RANGE, UPLO, N, KD, AB, LDAB, Q, LDQ, VL,
  2      $                   VU, IL, IU, ABSTOL, M, W, Z, LDZ, WORK, RWORK,
  3      $                   IWORK, IFAIL, INFO )
  4 *
  5 *  -- LAPACK driver routine (version 3.2) --
  6 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
  7 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  8 *     November 2006
  9 *
 10 *     .. Scalar Arguments ..
 11       CHARACTER          JOBZ, RANGE, UPLO
 12       INTEGER            IL, INFO, IU, KD, LDAB, LDQ, LDZ, M, N
 13       DOUBLE PRECISION   ABSTOL, VL, VU
 14 *     ..
 15 *     .. Array Arguments ..
 16       INTEGER            IFAIL( * ), IWORK( * )
 17       DOUBLE PRECISION   RWORK( * ), W( * )
 18       COMPLEX*16         AB( LDAB, * ), Q( LDQ, * ), WORK( * ),
 19      $                   Z( LDZ, * )
 20 *     ..
 21 *
 22 *  Purpose
 23 *  =======
 24 *
 25 *  ZHBEVX computes selected eigenvalues and, optionally, eigenvectors
 26 *  of a complex Hermitian band matrix A.  Eigenvalues and eigenvectors
 27 *  can be selected by specifying either a range of values or a range of
 28 *  indices for the desired eigenvalues.
 29 *
 30 *  Arguments
 31 *  =========
 32 *
 33 *  JOBZ    (input) CHARACTER*1
 34 *          = 'N':  Compute eigenvalues only;
 35 *          = 'V':  Compute eigenvalues and eigenvectors.
 36 *
 37 *  RANGE   (input) CHARACTER*1
 38 *          = 'A': all eigenvalues will be found;
 39 *          = 'V': all eigenvalues in the half-open interval (VL,VU]
 40 *                 will be found;
 41 *          = 'I': the IL-th through IU-th eigenvalues will be found.
 42 *
 43 *  UPLO    (input) CHARACTER*1
 44 *          = 'U':  Upper triangle of A is stored;
 45 *          = 'L':  Lower triangle of A is stored.
 46 *
 47 *  N       (input) INTEGER
 48 *          The order of the matrix A.  N >= 0.
 49 *
 50 *  KD      (input) INTEGER
 51 *          The number of superdiagonals of the matrix A if UPLO = 'U',
 52 *          or the number of subdiagonals if UPLO = 'L'.  KD >= 0.
 53 *
 54 *  AB      (input/output) COMPLEX*16 array, dimension (LDAB, N)
 55 *          On entry, the upper or lower triangle of the Hermitian band
 56 *          matrix A, stored in the first KD+1 rows of the array.  The
 57 *          j-th column of A is stored in the j-th column of the array AB
 58 *          as follows:
 59 *          if UPLO = 'U', AB(kd+1+i-j,j) = A(i,j) for max(1,j-kd)<=i<=j;
 60 *          if UPLO = 'L', AB(1+i-j,j)    = A(i,j) for j<=i<=min(n,j+kd).
 61 *
 62 *          On exit, AB is overwritten by values generated during the
 63 *          reduction to tridiagonal form.
 64 *
 65 *  LDAB    (input) INTEGER
 66 *          The leading dimension of the array AB.  LDAB >= KD + 1.
 67 *
 68 *  Q       (output) COMPLEX*16 array, dimension (LDQ, N)
 69 *          If JOBZ = 'V', the N-by-N unitary matrix used in the
 70 *                          reduction to tridiagonal form.
 71 *          If JOBZ = 'N', the array Q is not referenced.
 72 *
 73 *  LDQ     (input) INTEGER
 74 *          The leading dimension of the array Q.  If JOBZ = 'V', then
 75 *          LDQ >= max(1,N).
 76 *
 77 *  VL      (input) DOUBLE PRECISION
 78 *  VU      (input) DOUBLE PRECISION
 79 *          If RANGE='V', the lower and upper bounds of the interval to
 80 *          be searched for eigenvalues. VL < VU.
 81 *          Not referenced if RANGE = 'A' or 'I'.
 82 *
 83 *  IL      (input) INTEGER
 84 *  IU      (input) INTEGER
 85 *          If RANGE='I', the indices (in ascending order) of the
 86 *          smallest and largest eigenvalues to be returned.
 87 *          1 <= IL <= IU <= N, if N > 0; IL = 1 and IU = 0 if N = 0.
 88 *          Not referenced if RANGE = 'A' or 'V'.
 89 *
 90 *  ABSTOL  (input) DOUBLE PRECISION
 91 *          The absolute error tolerance for the eigenvalues.
 92 *          An approximate eigenvalue is accepted as converged
 93 *          when it is determined to lie in an interval [a,b]
 94 *          of width less than or equal to
 95 *
 96 *                  ABSTOL + EPS *   max( |a|,|b| ) ,
 97 *
 98 *          where EPS is the machine precision.  If ABSTOL is less than
 99 *          or equal to zero, then  EPS*|T|  will be used in its place,
100 *          where |T| is the 1-norm of the tridiagonal matrix obtained
101 *          by reducing AB to tridiagonal form.
102 *
103 *          Eigenvalues will be computed most accurately when ABSTOL is
104 *          set to twice the underflow threshold 2*DLAMCH('S'), not zero.
105 *          If this routine returns with INFO>0, indicating that some
106 *          eigenvectors did not converge, try setting ABSTOL to
107 *          2*DLAMCH('S').
108 *
109 *          See "Computing Small Singular Values of Bidiagonal Matrices
110 *          with Guaranteed High Relative Accuracy," by Demmel and
111 *          Kahan, LAPACK Working Note #3.
112 *
113 *  M       (output) INTEGER
114 *          The total number of eigenvalues found.  0 <= M <= N.
115 *          If RANGE = 'A', M = N, and if RANGE = 'I', M = IU-IL+1.
116 *
117 *  W       (output) DOUBLE PRECISION array, dimension (N)
118 *          The first M elements contain the selected eigenvalues in
119 *          ascending order.
120 *
121 *  Z       (output) COMPLEX*16 array, dimension (LDZ, max(1,M))
122 *          If JOBZ = 'V', then if INFO = 0, the first M columns of Z
123 *          contain the orthonormal eigenvectors of the matrix A
124 *          corresponding to the selected eigenvalues, with the i-th
125 *          column of Z holding the eigenvector associated with W(i).
126 *          If an eigenvector fails to converge, then that column of Z
127 *          contains the latest approximation to the eigenvector, and the
128 *          index of the eigenvector is returned in IFAIL.
129 *          If JOBZ = 'N', then Z is not referenced.
130 *          Note: the user must ensure that at least max(1,M) columns are
131 *          supplied in the array Z; if RANGE = 'V', the exact value of M
132 *          is not known in advance and an upper bound must be used.
133 *
134 *  LDZ     (input) INTEGER
135 *          The leading dimension of the array Z.  LDZ >= 1, and if
136 *          JOBZ = 'V', LDZ >= max(1,N).
137 *
138 *  WORK    (workspace) COMPLEX*16 array, dimension (N)
139 *
140 *  RWORK   (workspace) DOUBLE PRECISION array, dimension (7*N)
141 *
142 *  IWORK   (workspace) INTEGER array, dimension (5*N)
143 *
144 *  IFAIL   (output) INTEGER array, dimension (N)
145 *          If JOBZ = 'V', then if INFO = 0, the first M elements of
146 *          IFAIL are zero.  If INFO > 0, then IFAIL contains the
147 *          indices of the eigenvectors that failed to converge.
148 *          If JOBZ = 'N', then IFAIL is not referenced.
149 *
150 *  INFO    (output) INTEGER
151 *          = 0:  successful exit
152 *          < 0:  if INFO = -i, the i-th argument had an illegal value
153 *          > 0:  if INFO = i, then i eigenvectors failed to converge.
154 *                Their indices are stored in array IFAIL.
155 *
156 *  =====================================================================
157 *
158 *     .. Parameters ..
159       DOUBLE PRECISION   ZERO, ONE
160       PARAMETER          ( ZERO = 0.0D0, ONE = 1.0D0 )
161       COMPLEX*16         CZERO, CONE
162       PARAMETER          ( CZERO = ( 0.0D00.0D0 ),
163      $                   CONE = ( 1.0D00.0D0 ) )
164 *     ..
165 *     .. Local Scalars ..
166       LOGICAL            ALLEIG, INDEIG, LOWER, TEST, VALEIG, WANTZ
167       CHARACTER          ORDER
168       INTEGER            I, IINFO, IMAX, INDD, INDE, INDEE, INDIBL,
169      $                   INDISP, INDIWK, INDRWK, INDWRK, ISCALE, ITMP1,
170      $                   J, JJ, NSPLIT
171       DOUBLE PRECISION   ABSTLL, ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN,
172      $                   SIGMA, SMLNUM, TMP1, VLL, VUU
173       COMPLEX*16         CTMP1
174 *     ..
175 *     .. External Functions ..
176       LOGICAL            LSAME
177       DOUBLE PRECISION   DLAMCH, ZLANHB
178       EXTERNAL           LSAME, DLAMCH, ZLANHB
179 *     ..
180 *     .. External Subroutines ..
181       EXTERNAL           DCOPY, DSCAL, DSTEBZ, DSTERF, XERBLA, ZCOPY,
182      $                   ZGEMV, ZHBTRD, ZLACPY, ZLASCL, ZSTEIN, ZSTEQR,
183      $                   ZSWAP
184 *     ..
185 *     .. Intrinsic Functions ..
186       INTRINSIC          DBLEMAXMINSQRT
187 *     ..
188 *     .. Executable Statements ..
189 *
190 *     Test the input parameters.
191 *
192       WANTZ = LSAME( JOBZ, 'V' )
193       ALLEIG = LSAME( RANGE'A' )
194       VALEIG = LSAME( RANGE'V' )
195       INDEIG = LSAME( RANGE'I' )
196       LOWER = LSAME( UPLO, 'L' )
197 *
198       INFO = 0
199       IF.NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
200          INFO = -1
201       ELSE IF.NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
202          INFO = -2
203       ELSE IF.NOT.( LOWER .OR. LSAME( UPLO, 'U' ) ) ) THEN
204          INFO = -3
205       ELSE IF( N.LT.0 ) THEN
206          INFO = -4
207       ELSE IF( KD.LT.0 ) THEN
208          INFO = -5
209       ELSE IF( LDAB.LT.KD+1 ) THEN
210          INFO = -7
211       ELSE IF( WANTZ .AND. LDQ.LT.MAX1, N ) ) THEN
212          INFO = -9
213       ELSE
214          IF( VALEIG ) THEN
215             IF( N.GT.0 .AND. VU.LE.VL )
216      $         INFO = -11
217          ELSE IF( INDEIG ) THEN
218             IF( IL.LT.1 .OR. IL.GT.MAX1, N ) ) THEN
219                INFO = -12
220             ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
221                INFO = -13
222             END IF
223          END IF
224       END IF
225       IF( INFO.EQ.0 ) THEN
226          IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) )
227      $      INFO = -18
228       END IF
229 *
230       IF( INFO.NE.0 ) THEN
231          CALL XERBLA( 'ZHBEVX'-INFO )
232          RETURN
233       END IF
234 *
235 *     Quick return if possible
236 *
237       M = 0
238       IF( N.EQ.0 )
239      $   RETURN
240 *
241       IF( N.EQ.1 ) THEN
242          M = 1
243          IF( LOWER ) THEN
244             CTMP1 = AB( 11 )
245          ELSE
246             CTMP1 = AB( KD+11 )
247          END IF
248          TMP1 = DBLE( CTMP1 )
249          IF( VALEIG ) THEN
250             IF.NOT.( VL.LT.TMP1 .AND. VU.GE.TMP1 ) )
251      $         M = 0
252          END IF
253          IF( M.EQ.1 ) THEN
254             W( 1 ) = CTMP1
255             IF( WANTZ )
256      $         Z( 11 ) = CONE
257          END IF
258          RETURN
259       END IF
260 *
261 *     Get machine constants.
262 *
263       SAFMIN = DLAMCH( 'Safe minimum' )
264       EPS = DLAMCH( 'Precision' )
265       SMLNUM = SAFMIN / EPS
266       BIGNUM = ONE / SMLNUM
267       RMIN = SQRT( SMLNUM )
268       RMAX = MINSQRT( BIGNUM ), ONE / SQRTSQRT( SAFMIN ) ) )
269 *
270 *     Scale matrix to allowable range, if necessary.
271 *
272       ISCALE = 0
273       ABSTLL = ABSTOL
274       IF( VALEIG ) THEN
275          VLL = VL
276          VUU = VU
277       ELSE
278          VLL = ZERO
279          VUU = ZERO
280       END IF
281       ANRM = ZLANHB( 'M', UPLO, N, KD, AB, LDAB, RWORK )
282       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
283          ISCALE = 1
284          SIGMA = RMIN / ANRM
285       ELSE IF( ANRM.GT.RMAX ) THEN
286          ISCALE = 1
287          SIGMA = RMAX / ANRM
288       END IF
289       IF( ISCALE.EQ.1 ) THEN
290          IF( LOWER ) THEN
291             CALL ZLASCL( 'B', KD, KD, ONE, SIGMA, N, N, AB, LDAB, INFO )
292          ELSE
293             CALL ZLASCL( 'Q', KD, KD, ONE, SIGMA, N, N, AB, LDAB, INFO )
294          END IF
295          IF( ABSTOL.GT.0 )
296      $      ABSTLL = ABSTOL*SIGMA
297          IF( VALEIG ) THEN
298             VLL = VL*SIGMA
299             VUU = VU*SIGMA
300          END IF
301       END IF
302 *
303 *     Call ZHBTRD to reduce Hermitian band matrix to tridiagonal form.
304 *
305       INDD = 1
306       INDE = INDD + N
307       INDRWK = INDE + N
308       INDWRK = 1
309       CALL ZHBTRD( JOBZ, UPLO, N, KD, AB, LDAB, RWORK( INDD ),
310      $             RWORK( INDE ), Q, LDQ, WORK( INDWRK ), IINFO )
311 *
312 *     If all eigenvalues are desired and ABSTOL is less than or equal
313 *     to zero, then call DSTERF or ZSTEQR.  If this fails for some
314 *     eigenvalue, then try DSTEBZ.
315 *
316       TEST = .FALSE.
317       IF (INDEIG) THEN
318          IF (IL.EQ.1 .AND. IU.EQ.N) THEN
319             TEST = .TRUE.
320          END IF
321       END IF
322       IF ((ALLEIG .OR. TEST) .AND. (ABSTOL.LE.ZERO)) THEN
323          CALL DCOPY( N, RWORK( INDD ), 1, W, 1 )
324          INDEE = INDRWK + 2*N
325          IF.NOT.WANTZ ) THEN
326             CALL DCOPY( N-1, RWORK( INDE ), 1, RWORK( INDEE ), 1 )
327             CALL DSTERF( N, W, RWORK( INDEE ), INFO )
328          ELSE
329             CALL ZLACPY( 'A', N, N, Q, LDQ, Z, LDZ )
330             CALL DCOPY( N-1, RWORK( INDE ), 1, RWORK( INDEE ), 1 )
331             CALL ZSTEQR( JOBZ, N, W, RWORK( INDEE ), Z, LDZ,
332      $                   RWORK( INDRWK ), INFO )
333             IF( INFO.EQ.0 ) THEN
334                DO 10 I = 1, N
335                   IFAIL( I ) = 0
336    10          CONTINUE
337             END IF
338          END IF
339          IF( INFO.EQ.0 ) THEN
340             M = N
341             GO TO 30
342          END IF
343          INFO = 0
344       END IF
345 *
346 *     Otherwise, call DSTEBZ and, if eigenvectors are desired, ZSTEIN.
347 *
348       IF( WANTZ ) THEN
349          ORDER = 'B'
350       ELSE
351          ORDER = 'E'
352       END IF
353       INDIBL = 1
354       INDISP = INDIBL + N
355       INDIWK = INDISP + N
356       CALL DSTEBZ( RANGE, ORDER, N, VLL, VUU, IL, IU, ABSTLL,
357      $             RWORK( INDD ), RWORK( INDE ), M, NSPLIT, W,
358      $             IWORK( INDIBL ), IWORK( INDISP ), RWORK( INDRWK ),
359      $             IWORK( INDIWK ), INFO )
360 *
361       IF( WANTZ ) THEN
362          CALL ZSTEIN( N, RWORK( INDD ), RWORK( INDE ), M, W,
363      $                IWORK( INDIBL ), IWORK( INDISP ), Z, LDZ,
364      $                RWORK( INDRWK ), IWORK( INDIWK ), IFAIL, INFO )
365 *
366 *        Apply unitary matrix used in reduction to tridiagonal
367 *        form to eigenvectors returned by ZSTEIN.
368 *
369          DO 20 J = 1, M
370             CALL ZCOPY( N, Z( 1, J ), 1, WORK( 1 ), 1 )
371             CALL ZGEMV( 'N', N, N, CONE, Q, LDQ, WORK, 1, CZERO,
372      $                  Z( 1, J ), 1 )
373    20    CONTINUE
374       END IF
375 *
376 *     If matrix was scaled, then rescale eigenvalues appropriately.
377 *
378    30 CONTINUE
379       IF( ISCALE.EQ.1 ) THEN
380          IF( INFO.EQ.0 ) THEN
381             IMAX = M
382          ELSE
383             IMAX = INFO - 1
384          END IF
385          CALL DSCAL( IMAX, ONE / SIGMA, W, 1 )
386       END IF
387 *
388 *     If eigenvalues are not in order, then sort them, along with
389 *     eigenvectors.
390 *
391       IF( WANTZ ) THEN
392          DO 50 J = 1, M - 1
393             I = 0
394             TMP1 = W( J )
395             DO 40 JJ = J + 1, M
396                IF( W( JJ ).LT.TMP1 ) THEN
397                   I = JJ
398                   TMP1 = W( JJ )
399                END IF
400    40       CONTINUE
401 *
402             IF( I.NE.0 ) THEN
403                ITMP1 = IWORK( INDIBL+I-1 )
404                W( I ) = W( J )
405                IWORK( INDIBL+I-1 ) = IWORK( INDIBL+J-1 )
406                W( J ) = TMP1
407                IWORK( INDIBL+J-1 ) = ITMP1
408                CALL ZSWAP( N, Z( 1, I ), 1, Z( 1, J ), 1 )
409                IF( INFO.NE.0 ) THEN
410                   ITMP1 = IFAIL( I )
411                   IFAIL( I ) = IFAIL( J )
412                   IFAIL( J ) = ITMP1
413                END IF
414             END IF
415    50    CONTINUE
416       END IF
417 *
418       RETURN
419 *
420 *     End of ZHBEVX
421 *
422       END