001:       SUBROUTINE SSPEVX( JOBZ, RANGE, UPLO, N, AP, VL, VU, IL, IU,
002:      $                   ABSTOL, M, W, Z, LDZ, WORK, IWORK, IFAIL,
003:      $                   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, LDZ, M, N
012:       REAL               ABSTOL, VL, VU
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IFAIL( * ), IWORK( * )
016:       REAL               AP( * ), W( * ), WORK( * ), Z( LDZ, * )
017: *     ..
018: *
019: *  Purpose
020: *  =======
021: *
022: *  SSPEVX computes selected eigenvalues and, optionally, eigenvectors
023: *  of a real symmetric matrix A in packed storage.  Eigenvalues/vectors
024: *  can be selected by specifying either a range of values or a range of
025: *  indices 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: *  AP      (input/output) REAL array, dimension (N*(N+1)/2)
048: *          On entry, the upper or lower triangle of the symmetric matrix
049: *          A, packed columnwise in a linear array.  The j-th column of A
050: *          is stored in the array AP as follows:
051: *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
052: *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
053: *
054: *          On exit, AP is overwritten by values generated during the
055: *          reduction to tridiagonal form.  If UPLO = 'U', the diagonal
056: *          and first superdiagonal of the tridiagonal matrix T overwrite
057: *          the corresponding elements of A, and if UPLO = 'L', the
058: *          diagonal and first subdiagonal of T overwrite the
059: *          corresponding elements of A.
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 AP 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: *          If INFO = 0, the selected 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) REAL array, dimension (8*N)
122: *
123: *  IWORK   (workspace) INTEGER array, dimension (5*N)
124: *
125: *  IFAIL   (output) INTEGER array, dimension (N)
126: *          If JOBZ = 'V', then if INFO = 0, the first M elements of
127: *          IFAIL are zero.  If INFO > 0, then IFAIL contains the
128: *          indices of the eigenvectors that failed to converge.
129: *          If JOBZ = 'N', then IFAIL is not referenced.
130: *
131: *  INFO    (output) INTEGER
132: *          = 0:  successful exit
133: *          < 0:  if INFO = -i, the i-th argument had an illegal value
134: *          > 0:  if INFO = i, then i eigenvectors failed to converge.
135: *                Their indices are stored in array IFAIL.
136: *
137: *  =====================================================================
138: *
139: *     .. Parameters ..
140:       REAL               ZERO, ONE
141:       PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0 )
142: *     ..
143: *     .. Local Scalars ..
144:       LOGICAL            ALLEIG, INDEIG, TEST, VALEIG, WANTZ
145:       CHARACTER          ORDER
146:       INTEGER            I, IINFO, IMAX, INDD, INDE, INDEE, INDIBL,
147:      $                   INDISP, INDIWO, INDTAU, INDWRK, ISCALE, ITMP1,
148:      $                   J, JJ, NSPLIT
149:       REAL               ABSTLL, ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN,
150:      $                   SIGMA, SMLNUM, TMP1, VLL, VUU
151: *     ..
152: *     .. External Functions ..
153:       LOGICAL            LSAME
154:       REAL               SLAMCH, SLANSP
155:       EXTERNAL           LSAME, SLAMCH, SLANSP
156: *     ..
157: *     .. External Subroutines ..
158:       EXTERNAL           SCOPY, SOPGTR, SOPMTR, SSCAL, SSPTRD, SSTEBZ,
159:      $                   SSTEIN, SSTEQR, SSTERF, SSWAP, XERBLA
160: *     ..
161: *     .. Intrinsic Functions ..
162:       INTRINSIC          MAX, MIN, SQRT
163: *     ..
164: *     .. Executable Statements ..
165: *
166: *     Test the input parameters.
167: *
168:       WANTZ = LSAME( JOBZ, 'V' )
169:       ALLEIG = LSAME( RANGE, 'A' )
170:       VALEIG = LSAME( RANGE, 'V' )
171:       INDEIG = LSAME( RANGE, 'I' )
172: *
173:       INFO = 0
174:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
175:          INFO = -1
176:       ELSE IF( .NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
177:          INFO = -2
178:       ELSE IF( .NOT.( LSAME( UPLO, 'L' ) .OR. LSAME( UPLO, 'U' ) ) )
179:      $          THEN
180:          INFO = -3
181:       ELSE IF( N.LT.0 ) THEN
182:          INFO = -4
183:       ELSE
184:          IF( VALEIG ) THEN
185:             IF( N.GT.0 .AND. VU.LE.VL )
186:      $         INFO = -7
187:          ELSE IF( INDEIG ) THEN
188:             IF( IL.LT.1 .OR. IL.GT.MAX( 1, N ) ) THEN
189:                INFO = -8
190:             ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
191:                INFO = -9
192:             END IF
193:          END IF
194:       END IF
195:       IF( INFO.EQ.0 ) THEN
196:          IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) )
197:      $      INFO = -14
198:       END IF
199: *
200:       IF( INFO.NE.0 ) THEN
201:          CALL XERBLA( 'SSPEVX', -INFO )
202:          RETURN
203:       END IF
204: *
205: *     Quick return if possible
206: *
207:       M = 0
208:       IF( N.EQ.0 )
209:      $   RETURN
210: *
211:       IF( N.EQ.1 ) THEN
212:          IF( ALLEIG .OR. INDEIG ) THEN
213:             M = 1
214:             W( 1 ) = AP( 1 )
215:          ELSE
216:             IF( VL.LT.AP( 1 ) .AND. VU.GE.AP( 1 ) ) THEN
217:                M = 1
218:                W( 1 ) = AP( 1 )
219:             END IF
220:          END IF
221:          IF( WANTZ )
222:      $      Z( 1, 1 ) = ONE
223:          RETURN
224:       END IF
225: *
226: *     Get machine constants.
227: *
228:       SAFMIN = SLAMCH( 'Safe minimum' )
229:       EPS = SLAMCH( 'Precision' )
230:       SMLNUM = SAFMIN / EPS
231:       BIGNUM = ONE / SMLNUM
232:       RMIN = SQRT( SMLNUM )
233:       RMAX = MIN( SQRT( BIGNUM ), ONE / SQRT( SQRT( SAFMIN ) ) )
234: *
235: *     Scale matrix to allowable range, if necessary.
236: *
237:       ISCALE = 0
238:       ABSTLL = ABSTOL
239:       IF ( VALEIG ) THEN
240:          VLL = VL
241:          VUU = VU
242:       ELSE
243:          VLL = ZERO
244:          VUU = ZERO
245:       ENDIF
246:       ANRM = SLANSP( 'M', UPLO, N, AP, WORK )
247:       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
248:          ISCALE = 1
249:          SIGMA = RMIN / ANRM
250:       ELSE IF( ANRM.GT.RMAX ) THEN
251:          ISCALE = 1
252:          SIGMA = RMAX / ANRM
253:       END IF
254:       IF( ISCALE.EQ.1 ) THEN
255:          CALL SSCAL( ( N*( N+1 ) ) / 2, SIGMA, AP, 1 )
256:          IF( ABSTOL.GT.0 )
257:      $      ABSTLL = ABSTOL*SIGMA
258:          IF( VALEIG ) THEN
259:             VLL = VL*SIGMA
260:             VUU = VU*SIGMA
261:          END IF
262:       END IF
263: *
264: *     Call SSPTRD to reduce symmetric packed matrix to tridiagonal form.
265: *
266:       INDTAU = 1
267:       INDE = INDTAU + N
268:       INDD = INDE + N
269:       INDWRK = INDD + N
270:       CALL SSPTRD( UPLO, N, AP, WORK( INDD ), WORK( INDE ),
271:      $             WORK( INDTAU ), IINFO )
272: *
273: *     If all eigenvalues are desired and ABSTOL is less than or equal
274: *     to zero, then call SSTERF or SOPGTR and SSTEQR.  If this fails
275: *     for some eigenvalue, then try SSTEBZ.
276: *
277:       TEST = .FALSE.
278:       IF (INDEIG) THEN
279:          IF (IL.EQ.1 .AND. IU.EQ.N) THEN
280:             TEST = .TRUE.
281:          END IF
282:       END IF
283:       IF ((ALLEIG .OR. TEST) .AND. (ABSTOL.LE.ZERO)) THEN
284:          CALL SCOPY( N, WORK( INDD ), 1, W, 1 )
285:          INDEE = INDWRK + 2*N
286:          IF( .NOT.WANTZ ) THEN
287:             CALL SCOPY( N-1, WORK( INDE ), 1, WORK( INDEE ), 1 )
288:             CALL SSTERF( N, W, WORK( INDEE ), INFO )
289:          ELSE
290:             CALL SOPGTR( UPLO, N, AP, WORK( INDTAU ), Z, LDZ,
291:      $                   WORK( INDWRK ), IINFO )
292:             CALL SCOPY( N-1, WORK( INDE ), 1, WORK( INDEE ), 1 )
293:             CALL SSTEQR( JOBZ, N, W, WORK( INDEE ), Z, LDZ,
294:      $                   WORK( INDWRK ), INFO )
295:             IF( INFO.EQ.0 ) THEN
296:                DO 10 I = 1, N
297:                   IFAIL( I ) = 0
298:    10          CONTINUE
299:             END IF
300:          END IF
301:          IF( INFO.EQ.0 ) THEN
302:             M = N
303:             GO TO 20
304:          END IF
305:          INFO = 0
306:       END IF
307: *
308: *     Otherwise, call SSTEBZ and, if eigenvectors are desired, SSTEIN.
309: *
310:       IF( WANTZ ) THEN
311:          ORDER = 'B'
312:       ELSE
313:          ORDER = 'E'
314:       END IF
315:       INDIBL = 1
316:       INDISP = INDIBL + N
317:       INDIWO = INDISP + N
318:       CALL SSTEBZ( RANGE, ORDER, N, VLL, VUU, IL, IU, ABSTLL,
319:      $             WORK( INDD ), WORK( INDE ), M, NSPLIT, W,
320:      $             IWORK( INDIBL ), IWORK( INDISP ), WORK( INDWRK ),
321:      $             IWORK( INDIWO ), INFO )
322: *
323:       IF( WANTZ ) THEN
324:          CALL SSTEIN( N, WORK( INDD ), WORK( INDE ), M, W,
325:      $                IWORK( INDIBL ), IWORK( INDISP ), Z, LDZ,
326:      $                WORK( INDWRK ), IWORK( INDIWO ), IFAIL, INFO )
327: *
328: *        Apply orthogonal matrix used in reduction to tridiagonal
329: *        form to eigenvectors returned by SSTEIN.
330: *
331:          CALL SOPMTR( 'L', UPLO, 'N', N, M, AP, WORK( INDTAU ), Z, LDZ,
332:      $                WORK( INDWRK ), IINFO )
333:       END IF
334: *
335: *     If matrix was scaled, then rescale eigenvalues appropriately.
336: *
337:    20 CONTINUE
338:       IF( ISCALE.EQ.1 ) THEN
339:          IF( INFO.EQ.0 ) THEN
340:             IMAX = M
341:          ELSE
342:             IMAX = INFO - 1
343:          END IF
344:          CALL SSCAL( IMAX, ONE / SIGMA, W, 1 )
345:       END IF
346: *
347: *     If eigenvalues are not in order, then sort them, along with
348: *     eigenvectors.
349: *
350:       IF( WANTZ ) THEN
351:          DO 40 J = 1, M - 1
352:             I = 0
353:             TMP1 = W( J )
354:             DO 30 JJ = J + 1, M
355:                IF( W( JJ ).LT.TMP1 ) THEN
356:                   I = JJ
357:                   TMP1 = W( JJ )
358:                END IF
359:    30       CONTINUE
360: *
361:             IF( I.NE.0 ) THEN
362:                ITMP1 = IWORK( INDIBL+I-1 )
363:                W( I ) = W( J )
364:                IWORK( INDIBL+I-1 ) = IWORK( INDIBL+J-1 )
365:                W( J ) = TMP1
366:                IWORK( INDIBL+J-1 ) = ITMP1
367:                CALL SSWAP( N, Z( 1, I ), 1, Z( 1, J ), 1 )
368:                IF( INFO.NE.0 ) THEN
369:                   ITMP1 = IFAIL( I )
370:                   IFAIL( I ) = IFAIL( J )
371:                   IFAIL( J ) = ITMP1
372:                END IF
373:             END IF
374:    40    CONTINUE
375:       END IF
376: *
377:       RETURN
378: *
379: *     End of SSPEVX
380: *
381:       END
382: