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