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