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