001:       SUBROUTINE SSBEVX( JOBZ, RANGE, UPLO, N, KD, AB, LDAB, Q, LDQ, VL,
002:      $                   VU, IL, IU, ABSTOL, M, W, Z, LDZ, WORK, IWORK,
003:      $                   IFAIL, INFO )
004: *
005: *  -- LAPACK driver routine (version 3.2) --
006: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       CHARACTER          JOBZ, RANGE, UPLO
011:       INTEGER            IL, INFO, IU, KD, LDAB, LDQ, LDZ, M, N
012:       REAL               ABSTOL, VL, VU
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IFAIL( * ), IWORK( * )
016:       REAL               AB( LDAB, * ), Q( LDQ, * ), W( * ), WORK( * ),
017:      $                   Z( LDZ, * )
018: *     ..
019: *
020: *  Purpose
021: *  =======
022: *
023: *  SSBEVX computes selected eigenvalues and, optionally, eigenvectors
024: *  of a real symmetric band matrix A.  Eigenvalues and eigenvectors can
025: *  be selected by specifying either a range of values or a range of
026: *  indices for the desired eigenvalues.
027: *
028: *  Arguments
029: *  =========
030: *
031: *  JOBZ    (input) CHARACTER*1
032: *          = 'N':  Compute eigenvalues only;
033: *          = 'V':  Compute eigenvalues and eigenvectors.
034: *
035: *  RANGE   (input) CHARACTER*1
036: *          = 'A': all eigenvalues will be found;
037: *          = 'V': all eigenvalues in the half-open interval (VL,VU]
038: *                 will be found;
039: *          = 'I': the IL-th through IU-th eigenvalues will be found.
040: *
041: *  UPLO    (input) CHARACTER*1
042: *          = 'U':  Upper triangle of A is stored;
043: *          = 'L':  Lower triangle of A is stored.
044: *
045: *  N       (input) INTEGER
046: *          The order of the matrix A.  N >= 0.
047: *
048: *  KD      (input) INTEGER
049: *          The number of superdiagonals of the matrix A if UPLO = 'U',
050: *          or the number of subdiagonals if UPLO = 'L'.  KD >= 0.
051: *
052: *  AB      (input/output) REAL array, dimension (LDAB, N)
053: *          On entry, the upper or lower triangle of the symmetric band
054: *          matrix A, stored in the first KD+1 rows of the array.  The
055: *          j-th column of A is stored in the j-th column of the array AB
056: *          as follows:
057: *          if UPLO = 'U', AB(kd+1+i-j,j) = A(i,j) for max(1,j-kd)<=i<=j;
058: *          if UPLO = 'L', AB(1+i-j,j)    = A(i,j) for j<=i<=min(n,j+kd).
059: *
060: *          On exit, AB is overwritten by values generated during the
061: *          reduction to tridiagonal form.  If UPLO = 'U', the first
062: *          superdiagonal and the diagonal of the tridiagonal matrix T
063: *          are returned in rows KD and KD+1 of AB, and if UPLO = 'L',
064: *          the diagonal and first subdiagonal of T are returned in the
065: *          first two rows of AB.
066: *
067: *  LDAB    (input) INTEGER
068: *          The leading dimension of the array AB.  LDAB >= KD + 1.
069: *
070: *  Q       (output) REAL array, dimension (LDQ, N)
071: *          If JOBZ = 'V', the N-by-N orthogonal matrix used in the
072: *                         reduction to tridiagonal form.
073: *          If JOBZ = 'N', the array Q is not referenced.
074: *
075: *  LDQ     (input) INTEGER
076: *          The leading dimension of the array Q.  If JOBZ = 'V', then
077: *          LDQ >= max(1,N).
078: *
079: *  VL      (input) REAL
080: *  VU      (input) REAL
081: *          If RANGE='V', the lower and upper bounds of the interval to
082: *          be searched for eigenvalues. VL < VU.
083: *          Not referenced if RANGE = 'A' or 'I'.
084: *
085: *  IL      (input) INTEGER
086: *  IU      (input) INTEGER
087: *          If RANGE='I', the indices (in ascending order) of the
088: *          smallest and largest eigenvalues to be returned.
089: *          1 <= IL <= IU <= N, if N > 0; IL = 1 and IU = 0 if N = 0.
090: *          Not referenced if RANGE = 'A' or 'V'.
091: *
092: *  ABSTOL  (input) REAL
093: *          The absolute error tolerance for the eigenvalues.
094: *          An approximate eigenvalue is accepted as converged
095: *          when it is determined to lie in an interval [a,b]
096: *          of width less than or equal to
097: *
098: *                  ABSTOL + EPS *   max( |a|,|b| ) ,
099: *
100: *          where EPS is the machine precision.  If ABSTOL is less than
101: *          or equal to zero, then  EPS*|T|  will be used in its place,
102: *          where |T| is the 1-norm of the tridiagonal matrix obtained
103: *          by reducing AB to tridiagonal form.
104: *
105: *          Eigenvalues will be computed most accurately when ABSTOL is
106: *          set to twice the underflow threshold 2*SLAMCH('S'), not zero.
107: *          If this routine returns with INFO>0, indicating that some
108: *          eigenvectors did not converge, try setting ABSTOL to
109: *          2*SLAMCH('S').
110: *
111: *          See "Computing Small Singular Values of Bidiagonal Matrices
112: *          with Guaranteed High Relative Accuracy," by Demmel and
113: *          Kahan, LAPACK Working Note #3.
114: *
115: *  M       (output) INTEGER
116: *          The total number of eigenvalues found.  0 <= M <= N.
117: *          If RANGE = 'A', M = N, and if RANGE = 'I', M = IU-IL+1.
118: *
119: *  W       (output) REAL array, dimension (N)
120: *          The first M elements contain the selected eigenvalues in
121: *          ascending order.
122: *
123: *  Z       (output) REAL array, dimension (LDZ, max(1,M))
124: *          If JOBZ = 'V', then if INFO = 0, the first M columns of Z
125: *          contain the orthonormal eigenvectors of the matrix A
126: *          corresponding to the selected eigenvalues, with the i-th
127: *          column of Z holding the eigenvector associated with W(i).
128: *          If an eigenvector fails to converge, then that column of Z
129: *          contains the latest approximation to the eigenvector, and the
130: *          index of the eigenvector is returned in IFAIL.
131: *          If JOBZ = 'N', then Z is not referenced.
132: *          Note: the user must ensure that at least max(1,M) columns are
133: *          supplied in the array Z; if RANGE = 'V', the exact value of M
134: *          is not known in advance and an upper bound must be used.
135: *
136: *  LDZ     (input) INTEGER
137: *          The leading dimension of the array Z.  LDZ >= 1, and if
138: *          JOBZ = 'V', LDZ >= max(1,N).
139: *
140: *  WORK    (workspace) REAL 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:       REAL               ZERO, ONE
160:       PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0 )
161: *     ..
162: *     .. Local Scalars ..
163:       LOGICAL            ALLEIG, INDEIG, LOWER, TEST, VALEIG, WANTZ
164:       CHARACTER          ORDER
165:       INTEGER            I, IINFO, IMAX, INDD, INDE, INDEE, INDIBL,
166:      $                   INDISP, INDIWO, INDWRK, ISCALE, ITMP1, J, JJ,
167:      $                   NSPLIT
168:       REAL               ABSTLL, ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN,
169:      $                   SIGMA, SMLNUM, TMP1, VLL, VUU
170: *     ..
171: *     .. External Functions ..
172:       LOGICAL            LSAME
173:       REAL               SLAMCH, SLANSB
174:       EXTERNAL           LSAME, SLAMCH, SLANSB
175: *     ..
176: *     .. External Subroutines ..
177:       EXTERNAL           SCOPY, SGEMV, SLACPY, SLASCL, SSBTRD, SSCAL,
178:      $                   SSTEBZ, SSTEIN, SSTEQR, SSTERF, SSWAP, XERBLA
179: *     ..
180: *     .. Intrinsic Functions ..
181:       INTRINSIC          MAX, MIN, SQRT
182: *     ..
183: *     .. Executable Statements ..
184: *
185: *     Test the input parameters.
186: *
187:       WANTZ = LSAME( JOBZ, 'V' )
188:       ALLEIG = LSAME( RANGE, 'A' )
189:       VALEIG = LSAME( RANGE, 'V' )
190:       INDEIG = LSAME( RANGE, 'I' )
191:       LOWER = LSAME( UPLO, 'L' )
192: *
193:       INFO = 0
194:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
195:          INFO = -1
196:       ELSE IF( .NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
197:          INFO = -2
198:       ELSE IF( .NOT.( LOWER .OR. LSAME( UPLO, 'U' ) ) ) THEN
199:          INFO = -3
200:       ELSE IF( N.LT.0 ) THEN
201:          INFO = -4
202:       ELSE IF( KD.LT.0 ) THEN
203:          INFO = -5
204:       ELSE IF( LDAB.LT.KD+1 ) THEN
205:          INFO = -7
206:       ELSE IF( WANTZ .AND. LDQ.LT.MAX( 1, N ) ) THEN
207:          INFO = -9
208:       ELSE
209:          IF( VALEIG ) THEN
210:             IF( N.GT.0 .AND. VU.LE.VL )
211:      $         INFO = -11
212:          ELSE IF( INDEIG ) THEN
213:             IF( IL.LT.1 .OR. IL.GT.MAX( 1, N ) ) THEN
214:                INFO = -12
215:             ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
216:                INFO = -13
217:             END IF
218:          END IF
219:       END IF
220:       IF( INFO.EQ.0 ) THEN
221:          IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) )
222:      $     INFO = -18
223:       END IF
224: *
225:       IF( INFO.NE.0 ) THEN
226:          CALL XERBLA( 'SSBEVX', -INFO )
227:          RETURN
228:       END IF
229: *
230: *     Quick return if possible
231: *
232:       M = 0
233:       IF( N.EQ.0 )
234:      $   RETURN
235: *
236:       IF( N.EQ.1 ) THEN
237:          M = 1
238:          IF( LOWER ) THEN
239:             TMP1 = AB( 1, 1 )
240:          ELSE
241:             TMP1 = AB( KD+1, 1 )
242:          END IF
243:          IF( VALEIG ) THEN
244:             IF( .NOT.( VL.LT.TMP1 .AND. VU.GE.TMP1 ) )
245:      $         M = 0
246:          END IF
247:          IF( M.EQ.1 ) THEN
248:             W( 1 ) = TMP1
249:             IF( WANTZ )
250:      $         Z( 1, 1 ) = ONE
251:          END IF
252:          RETURN
253:       END IF
254: *
255: *     Get machine constants.
256: *
257:       SAFMIN = SLAMCH( 'Safe minimum' )
258:       EPS = SLAMCH( 'Precision' )
259:       SMLNUM = SAFMIN / EPS
260:       BIGNUM = ONE / SMLNUM
261:       RMIN = SQRT( SMLNUM )
262:       RMAX = MIN( SQRT( BIGNUM ), ONE / SQRT( SQRT( SAFMIN ) ) )
263: *
264: *     Scale matrix to allowable range, if necessary.
265: *
266:       ISCALE = 0
267:       ABSTLL = ABSTOL
268:       IF ( VALEIG ) THEN
269:          VLL = VL
270:          VUU = VU
271:       ELSE
272:          VLL = ZERO
273:          VUU = ZERO
274:       ENDIF
275:       ANRM = SLANSB( 'M', UPLO, N, KD, AB, LDAB, WORK )
276:       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
277:          ISCALE = 1
278:          SIGMA = RMIN / ANRM
279:       ELSE IF( ANRM.GT.RMAX ) THEN
280:          ISCALE = 1
281:          SIGMA = RMAX / ANRM
282:       END IF
283:       IF( ISCALE.EQ.1 ) THEN
284:          IF( LOWER ) THEN
285:             CALL SLASCL( 'B', KD, KD, ONE, SIGMA, N, N, AB, LDAB, INFO )
286:          ELSE
287:             CALL SLASCL( 'Q', KD, KD, ONE, SIGMA, N, N, AB, LDAB, INFO )
288:          END IF
289:          IF( ABSTOL.GT.0 )
290:      $      ABSTLL = ABSTOL*SIGMA
291:          IF( VALEIG ) THEN
292:             VLL = VL*SIGMA
293:             VUU = VU*SIGMA
294:          END IF
295:       END IF
296: *
297: *     Call SSBTRD to reduce symmetric band matrix to tridiagonal form.
298: *
299:       INDD = 1
300:       INDE = INDD + N
301:       INDWRK = INDE + N
302:       CALL SSBTRD( JOBZ, UPLO, N, KD, AB, LDAB, WORK( INDD ),
303:      $             WORK( INDE ), Q, LDQ, WORK( INDWRK ), IINFO )
304: *
305: *     If all eigenvalues are desired and ABSTOL is less than or equal
306: *     to zero, then call SSTERF or SSTEQR.  If this fails for some
307: *     eigenvalue, then try SSTEBZ.
308: *
309:       TEST = .FALSE.
310:       IF (INDEIG) THEN
311:          IF (IL.EQ.1 .AND. IU.EQ.N) THEN
312:             TEST = .TRUE.
313:          END IF
314:       END IF
315:       IF ((ALLEIG .OR. TEST) .AND. (ABSTOL.LE.ZERO)) THEN
316:          CALL SCOPY( N, WORK( INDD ), 1, W, 1 )
317:          INDEE = INDWRK + 2*N
318:          IF( .NOT.WANTZ ) THEN
319:             CALL SCOPY( N-1, WORK( INDE ), 1, WORK( INDEE ), 1 )
320:             CALL SSTERF( N, W, WORK( INDEE ), INFO )
321:          ELSE
322:             CALL SLACPY( 'A', N, N, Q, LDQ, Z, LDZ )
323:             CALL SCOPY( N-1, WORK( INDE ), 1, WORK( INDEE ), 1 )
324:             CALL SSTEQR( JOBZ, N, W, WORK( INDEE ), Z, LDZ,
325:      $                   WORK( INDWRK ), INFO )
326:             IF( INFO.EQ.0 ) THEN
327:                DO 10 I = 1, N
328:                   IFAIL( I ) = 0
329:    10          CONTINUE
330:             END IF
331:          END IF
332:          IF( INFO.EQ.0 ) THEN
333:             M = N
334:             GO TO 30
335:          END IF
336:          INFO = 0
337:       END IF
338: *
339: *     Otherwise, call SSTEBZ and, if eigenvectors are desired, SSTEIN.
340: *
341:       IF( WANTZ ) THEN
342:          ORDER = 'B'
343:       ELSE
344:          ORDER = 'E'
345:       END IF
346:       INDIBL = 1
347:       INDISP = INDIBL + N
348:       INDIWO = INDISP + N
349:       CALL SSTEBZ( RANGE, ORDER, N, VLL, VUU, IL, IU, ABSTLL,
350:      $             WORK( INDD ), WORK( INDE ), M, NSPLIT, W,
351:      $             IWORK( INDIBL ), IWORK( INDISP ), WORK( INDWRK ),
352:      $             IWORK( INDIWO ), INFO )
353: *
354:       IF( WANTZ ) THEN
355:          CALL SSTEIN( N, WORK( INDD ), WORK( INDE ), M, W,
356:      $                IWORK( INDIBL ), IWORK( INDISP ), Z, LDZ,
357:      $                WORK( INDWRK ), IWORK( INDIWO ), IFAIL, INFO )
358: *
359: *        Apply orthogonal matrix used in reduction to tridiagonal
360: *        form to eigenvectors returned by SSTEIN.
361: *
362:          DO 20 J = 1, M
363:             CALL SCOPY( N, Z( 1, J ), 1, WORK( 1 ), 1 )
364:             CALL SGEMV( 'N', N, N, ONE, Q, LDQ, WORK, 1, ZERO,
365:      $                  Z( 1, J ), 1 )
366:    20    CONTINUE
367:       END IF
368: *
369: *     If matrix was scaled, then rescale eigenvalues appropriately.
370: *
371:    30 CONTINUE
372:       IF( ISCALE.EQ.1 ) THEN
373:          IF( INFO.EQ.0 ) THEN
374:             IMAX = M
375:          ELSE
376:             IMAX = INFO - 1
377:          END IF
378:          CALL SSCAL( IMAX, ONE / SIGMA, W, 1 )
379:       END IF
380: *
381: *     If eigenvalues are not in order, then sort them, along with
382: *     eigenvectors.
383: *
384:       IF( WANTZ ) THEN
385:          DO 50 J = 1, M - 1
386:             I = 0
387:             TMP1 = W( J )
388:             DO 40 JJ = J + 1, M
389:                IF( W( JJ ).LT.TMP1 ) THEN
390:                   I = JJ
391:                   TMP1 = W( JJ )
392:                END IF
393:    40       CONTINUE
394: *
395:             IF( I.NE.0 ) THEN
396:                ITMP1 = IWORK( INDIBL+I-1 )
397:                W( I ) = W( J )
398:                IWORK( INDIBL+I-1 ) = IWORK( INDIBL+J-1 )
399:                W( J ) = TMP1
400:                IWORK( INDIBL+J-1 ) = ITMP1
401:                CALL SSWAP( N, Z( 1, I ), 1, Z( 1, J ), 1 )
402:                IF( INFO.NE.0 ) THEN
403:                   ITMP1 = IFAIL( I )
404:                   IFAIL( I ) = IFAIL( J )
405:                   IFAIL( J ) = ITMP1
406:                END IF
407:             END IF
408:    50    CONTINUE
409:       END IF
410: *
411:       RETURN
412: *
413: *     End of SSBEVX
414: *
415:       END
416: