001:       SUBROUTINE SSTEVR( JOBZ, RANGE, N, D, E, VL, VU, IL, IU, ABSTOL,
002:      $                   M, W, Z, LDZ, ISUPPZ, WORK, LWORK, IWORK,
003:      $                   LIWORK, 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
011:       INTEGER            IL, INFO, IU, LDZ, LIWORK, LWORK, M, N
012:       REAL               ABSTOL, VL, VU
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            ISUPPZ( * ), IWORK( * )
016:       REAL               D( * ), E( * ), W( * ), WORK( * ), Z( LDZ, * )
017: *     ..
018: *
019: *  Purpose
020: *  =======
021: *
022: *  SSTEVR computes selected eigenvalues and, optionally, eigenvectors
023: *  of a real symmetric tridiagonal matrix T.  Eigenvalues and
024: *  eigenvectors can be selected by specifying either a range of values
025: *  or a range of indices for the desired eigenvalues.
026: *
027: *  Whenever possible, SSTEVR calls SSTEMR to compute the
028: *  eigenspectrum using Relatively Robust Representations.  SSTEMR
029: *  computes eigenvalues by the dqds algorithm, while orthogonal
030: *  eigenvectors are computed from various "good" L D L^T representations
031: *  (also known as Relatively Robust Representations). Gram-Schmidt
032: *  orthogonalization is avoided as far as possible. More specifically,
033: *  the various steps of the algorithm are as follows. For the i-th
034: *  unreduced block of T,
035: *     (a) Compute T - sigma_i = L_i D_i L_i^T, such that L_i D_i L_i^T
036: *          is a relatively robust representation,
037: *     (b) Compute the eigenvalues, lambda_j, of L_i D_i L_i^T to high
038: *         relative accuracy by the dqds algorithm,
039: *     (c) If there is a cluster of close eigenvalues, "choose" sigma_i
040: *         close to the cluster, and go to step (a),
041: *     (d) Given the approximate eigenvalue lambda_j of L_i D_i L_i^T,
042: *         compute the corresponding eigenvector by forming a
043: *         rank-revealing twisted factorization.
044: *  The desired accuracy of the output can be specified by the input
045: *  parameter ABSTOL.
046: *
047: *  For more details, see "A new O(n^2) algorithm for the symmetric
048: *  tridiagonal eigenvalue/eigenvector problem", by Inderjit Dhillon,
049: *  Computer Science Division Technical Report No. UCB//CSD-97-971,
050: *  UC Berkeley, May 1997.
051: *
052: *
053: *  Note 1 : SSTEVR calls SSTEMR when the full spectrum is requested
054: *  on machines which conform to the ieee-754 floating point standard.
055: *  SSTEVR calls SSTEBZ and SSTEIN on non-ieee machines and
056: *  when partial spectrum requests are made.
057: *
058: *  Normal execution of SSTEMR may create NaNs and infinities and
059: *  hence may abort due to a floating point exception in environments
060: *  which do not handle NaNs and infinities in the ieee standard default
061: *  manner.
062: *
063: *  Arguments
064: *  =========
065: *
066: *  JOBZ    (input) CHARACTER*1
067: *          = 'N':  Compute eigenvalues only;
068: *          = 'V':  Compute eigenvalues and eigenvectors.
069: *
070: *  RANGE   (input) CHARACTER*1
071: *          = 'A': all eigenvalues will be found.
072: *          = 'V': all eigenvalues in the half-open interval (VL,VU]
073: *                 will be found.
074: *          = 'I': the IL-th through IU-th eigenvalues will be found.
075: ********** For RANGE = 'V' or 'I' and IU - IL < N - 1, SSTEBZ and
076: ********** SSTEIN are called
077: *
078: *  N       (input) INTEGER
079: *          The order of the matrix.  N >= 0.
080: *
081: *  D       (input/output) REAL array, dimension (N)
082: *          On entry, the n diagonal elements of the tridiagonal matrix
083: *          A.
084: *          On exit, D may be multiplied by a constant factor chosen
085: *          to avoid over/underflow in computing the eigenvalues.
086: *
087: *  E       (input/output) REAL array, dimension (max(1,N-1))
088: *          On entry, the (n-1) subdiagonal elements of the tridiagonal
089: *          matrix A in elements 1 to N-1 of E.
090: *          On exit, E may be multiplied by a constant factor chosen
091: *          to avoid over/underflow in computing the eigenvalues.
092: *
093: *  VL      (input) REAL
094: *  VU      (input) REAL
095: *          If RANGE='V', the lower and upper bounds of the interval to
096: *          be searched for eigenvalues. VL < VU.
097: *          Not referenced if RANGE = 'A' or 'I'.
098: *
099: *  IL      (input) INTEGER
100: *  IU      (input) INTEGER
101: *          If RANGE='I', the indices (in ascending order) of the
102: *          smallest and largest eigenvalues to be returned.
103: *          1 <= IL <= IU <= N, if N > 0; IL = 1 and IU = 0 if N = 0.
104: *          Not referenced if RANGE = 'A' or 'V'.
105: *
106: *  ABSTOL  (input) REAL
107: *          The absolute error tolerance for the eigenvalues.
108: *          An approximate eigenvalue is accepted as converged
109: *          when it is determined to lie in an interval [a,b]
110: *          of width less than or equal to
111: *
112: *                  ABSTOL + EPS *   max( |a|,|b| ) ,
113: *
114: *          where EPS is the machine precision.  If ABSTOL is less than
115: *          or equal to zero, then  EPS*|T|  will be used in its place,
116: *          where |T| is the 1-norm of the tridiagonal matrix obtained
117: *          by reducing A to tridiagonal form.
118: *
119: *          See "Computing Small Singular Values of Bidiagonal Matrices
120: *          with Guaranteed High Relative Accuracy," by Demmel and
121: *          Kahan, LAPACK Working Note #3.
122: *
123: *          If high relative accuracy is important, set ABSTOL to
124: *          SLAMCH( 'Safe minimum' ).  Doing so will guarantee that
125: *          eigenvalues are computed to high relative accuracy when
126: *          possible in future releases.  The current code does not
127: *          make any guarantees about high relative accuracy, but
128: *          future releases will. See J. Barlow and J. Demmel,
129: *          "Computing Accurate Eigensystems of Scaled Diagonally
130: *          Dominant Matrices", LAPACK Working Note #7, for a discussion
131: *          of which matrices define their eigenvalues to high relative
132: *          accuracy.
133: *
134: *  M       (output) INTEGER
135: *          The total number of eigenvalues found.  0 <= M <= N.
136: *          If RANGE = 'A', M = N, and if RANGE = 'I', M = IU-IL+1.
137: *
138: *  W       (output) REAL array, dimension (N)
139: *          The first M elements contain the selected eigenvalues in
140: *          ascending order.
141: *
142: *  Z       (output) REAL array, dimension (LDZ, max(1,M) )
143: *          If JOBZ = 'V', then if INFO = 0, the first M columns of Z
144: *          contain the orthonormal eigenvectors of the matrix A
145: *          corresponding to the selected eigenvalues, with the i-th
146: *          column of Z holding the eigenvector associated with W(i).
147: *          Note: the user must ensure that at least max(1,M) columns are
148: *          supplied in the array Z; if RANGE = 'V', the exact value of M
149: *          is not known in advance and an upper bound must be used.
150: *
151: *  LDZ     (input) INTEGER
152: *          The leading dimension of the array Z.  LDZ >= 1, and if
153: *          JOBZ = 'V', LDZ >= max(1,N).
154: *
155: *  ISUPPZ  (output) INTEGER array, dimension ( 2*max(1,M) )
156: *          The support of the eigenvectors in Z, i.e., the indices
157: *          indicating the nonzero elements in Z. The i-th eigenvector
158: *          is nonzero only in elements ISUPPZ( 2*i-1 ) through
159: *          ISUPPZ( 2*i ).
160: ********** Implemented only for RANGE = 'A' or 'I' and IU - IL = N - 1
161: *
162: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
163: *          On exit, if INFO = 0, WORK(1) returns the optimal (and
164: *          minimal) LWORK.
165: *
166: *  LWORK   (input) INTEGER
167: *          The dimension of the array WORK.  LWORK >= 20*N.
168: *
169: *          If LWORK = -1, then a workspace query is assumed; the routine
170: *          only calculates the optimal sizes of the WORK and IWORK
171: *          arrays, returns these values as the first entries of the WORK
172: *          and IWORK arrays, and no error message related to LWORK or
173: *          LIWORK is issued by XERBLA.
174: *
175: *  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK))
176: *          On exit, if INFO = 0, IWORK(1) returns the optimal (and
177: *          minimal) LIWORK.
178: *
179: *  LIWORK  (input) INTEGER
180: *          The dimension of the array IWORK.  LIWORK >= 10*N.
181: *
182: *          If LIWORK = -1, then a workspace query is assumed; the
183: *          routine only calculates the optimal sizes of the WORK and
184: *          IWORK arrays, returns these values as the first entries of
185: *          the WORK and IWORK arrays, and no error message related to
186: *          LWORK or LIWORK is issued by XERBLA.
187: *
188: *  INFO    (output) INTEGER
189: *          = 0:  successful exit
190: *          < 0:  if INFO = -i, the i-th argument had an illegal value
191: *          > 0:  Internal error
192: *
193: *  Further Details
194: *  ===============
195: *
196: *  Based on contributions by
197: *     Inderjit Dhillon, IBM Almaden, USA
198: *     Osni Marques, LBNL/NERSC, USA
199: *     Ken Stanley, Computer Science Division, University of
200: *       California at Berkeley, USA
201: *     Jason Riedy, Computer Science Division, University of
202: *       California at Berkeley, USA
203: *
204: *  =====================================================================
205: *
206: *     .. Parameters ..
207:       REAL               ZERO, ONE, TWO
208:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0, TWO = 2.0E+0 )
209: *     ..
210: *     .. Local Scalars ..
211:       LOGICAL            ALLEIG, INDEIG, TEST, LQUERY, VALEIG, WANTZ,
212:      $                   TRYRAC
213:       CHARACTER          ORDER
214:       INTEGER            I, IEEEOK, IMAX, INDIBL, INDIFL, INDISP,
215:      $                   INDIWO, ISCALE, J, JJ, LIWMIN, LWMIN, NSPLIT
216:       REAL               BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA, SMLNUM,
217:      $                   TMP1, TNRM, VLL, VUU
218: *     ..
219: *     .. External Functions ..
220:       LOGICAL            LSAME
221:       INTEGER            ILAENV
222:       REAL               SLAMCH, SLANST
223:       EXTERNAL           LSAME, ILAENV, SLAMCH, SLANST
224: *     ..
225: *     .. External Subroutines ..
226:       EXTERNAL           SCOPY, SSCAL, SSTEBZ, SSTEMR, SSTEIN, SSTERF,
227:      $                   SSWAP, XERBLA
228: *     ..
229: *     .. Intrinsic Functions ..
230:       INTRINSIC          MAX, MIN, SQRT
231: *     ..
232: *     .. Executable Statements ..
233: *
234: *
235: *     Test the input parameters.
236: *
237:       IEEEOK = ILAENV( 10, 'SSTEVR', 'N', 1, 2, 3, 4 )
238: *
239:       WANTZ = LSAME( JOBZ, 'V' )
240:       ALLEIG = LSAME( RANGE, 'A' )
241:       VALEIG = LSAME( RANGE, 'V' )
242:       INDEIG = LSAME( RANGE, 'I' )
243: *
244:       LQUERY = ( ( LWORK.EQ.-1 ) .OR. ( LIWORK.EQ.-1 ) )
245:       LWMIN = MAX( 1, 20*N )
246:       LIWMIN = MAX(1, 10*N )
247: *
248: *
249:       INFO = 0
250:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
251:          INFO = -1
252:       ELSE IF( .NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
253:          INFO = -2
254:       ELSE IF( N.LT.0 ) THEN
255:          INFO = -3
256:       ELSE
257:          IF( VALEIG ) THEN
258:             IF( N.GT.0 .AND. VU.LE.VL )
259:      $         INFO = -7
260:          ELSE IF( INDEIG ) THEN
261:             IF( IL.LT.1 .OR. IL.GT.MAX( 1, N ) ) THEN
262:                INFO = -8
263:             ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
264:                INFO = -9
265:             END IF
266:          END IF
267:       END IF
268:       IF( INFO.EQ.0 ) THEN
269:          IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
270:             INFO = -14
271:          END IF
272:       END IF
273: *
274:       IF( INFO.EQ.0 ) THEN
275:          WORK( 1 ) = LWMIN
276:          IWORK( 1 ) = LIWMIN
277: *
278:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
279:             INFO = -17
280:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
281:             INFO = -19
282:          END IF
283:       END IF
284: *
285:       IF( INFO.NE.0 ) THEN
286:          CALL XERBLA( 'SSTEVR', -INFO )
287:          RETURN
288:       ELSE IF( LQUERY ) THEN
289:          RETURN
290:       END IF
291: *
292: *     Quick return if possible
293: *
294:       M = 0
295:       IF( N.EQ.0 )
296:      $   RETURN
297: *
298:       IF( N.EQ.1 ) THEN
299:          IF( ALLEIG .OR. INDEIG ) THEN
300:             M = 1
301:             W( 1 ) = D( 1 )
302:          ELSE
303:             IF( VL.LT.D( 1 ) .AND. VU.GE.D( 1 ) ) THEN
304:                M = 1
305:                W( 1 ) = D( 1 )
306:             END IF
307:          END IF
308:          IF( WANTZ )
309:      $      Z( 1, 1 ) = ONE
310:          RETURN
311:       END IF
312: *
313: *     Get machine constants.
314: *
315:       SAFMIN = SLAMCH( 'Safe minimum' )
316:       EPS = SLAMCH( 'Precision' )
317:       SMLNUM = SAFMIN / EPS
318:       BIGNUM = ONE / SMLNUM
319:       RMIN = SQRT( SMLNUM )
320:       RMAX = MIN( SQRT( BIGNUM ), ONE / SQRT( SQRT( SAFMIN ) ) )
321: *
322: *
323: *     Scale matrix to allowable range, if necessary.
324: *
325:       ISCALE = 0
326:       VLL = VL
327:       VUU = VU
328: *
329:       TNRM = SLANST( 'M', N, D, E )
330:       IF( TNRM.GT.ZERO .AND. TNRM.LT.RMIN ) THEN
331:          ISCALE = 1
332:          SIGMA = RMIN / TNRM
333:       ELSE IF( TNRM.GT.RMAX ) THEN
334:          ISCALE = 1
335:          SIGMA = RMAX / TNRM
336:       END IF
337:       IF( ISCALE.EQ.1 ) THEN
338:          CALL SSCAL( N, SIGMA, D, 1 )
339:          CALL SSCAL( N-1, SIGMA, E( 1 ), 1 )
340:          IF( VALEIG ) THEN
341:             VLL = VL*SIGMA
342:             VUU = VU*SIGMA
343:          END IF
344:       END IF
345: 
346: *     Initialize indices into workspaces.  Note: These indices are used only
347: *     if SSTERF or SSTEMR fail.
348: 
349: *     IWORK(INDIBL:INDIBL+M-1) corresponds to IBLOCK in SSTEBZ and
350: *     stores the block indices of each of the M<=N eigenvalues.
351:       INDIBL = 1
352: *     IWORK(INDISP:INDISP+NSPLIT-1) corresponds to ISPLIT in SSTEBZ and
353: *     stores the starting and finishing indices of each block.
354:       INDISP = INDIBL + N
355: *     IWORK(INDIFL:INDIFL+N-1) stores the indices of eigenvectors
356: *     that corresponding to eigenvectors that fail to converge in
357: *     SSTEIN.  This information is discarded; if any fail, the driver
358: *     returns INFO > 0.
359:       INDIFL = INDISP + N
360: *     INDIWO is the offset of the remaining integer workspace.
361:       INDIWO = INDISP + N
362: *
363: *     If all eigenvalues are desired, then
364: *     call SSTERF or SSTEMR.  If this fails for some eigenvalue, then
365: *     try SSTEBZ.
366: *
367: *
368:       TEST = .FALSE.
369:       IF( INDEIG ) THEN
370:          IF( IL.EQ.1 .AND. IU.EQ.N ) THEN
371:             TEST = .TRUE.
372:          END IF
373:       END IF
374:       IF( ( ALLEIG .OR. TEST ) .AND. IEEEOK.EQ.1 ) THEN
375:          CALL SCOPY( N-1, E( 1 ), 1, WORK( 1 ), 1 )
376:          IF( .NOT.WANTZ ) THEN
377:             CALL SCOPY( N, D, 1, W, 1 )
378:             CALL SSTERF( N, W, WORK, INFO )
379:          ELSE
380:             CALL SCOPY( N, D, 1, WORK( N+1 ), 1 )
381:             IF (ABSTOL .LE. TWO*N*EPS) THEN
382:                TRYRAC = .TRUE.
383:             ELSE
384:                TRYRAC = .FALSE.
385:             END IF
386:             CALL SSTEMR( JOBZ, 'A', N, WORK( N+1 ), WORK, VL, VU, IL,
387:      $                   IU, M, W, Z, LDZ, N, ISUPPZ, TRYRAC,
388:      $                   WORK( 2*N+1 ), LWORK-2*N, IWORK, LIWORK, INFO )
389: *
390:          END IF
391:          IF( INFO.EQ.0 ) THEN
392:             M = N
393:             GO TO 10
394:          END IF
395:          INFO = 0
396:       END IF
397: *
398: *     Otherwise, call SSTEBZ and, if eigenvectors are desired, SSTEIN.
399: *
400:       IF( WANTZ ) THEN
401:          ORDER = 'B'
402:       ELSE
403:          ORDER = 'E'
404:       END IF
405: 
406:       CALL SSTEBZ( RANGE, ORDER, N, VLL, VUU, IL, IU, ABSTOL, D, E, M,
407:      $             NSPLIT, W, IWORK( INDIBL ), IWORK( INDISP ), WORK,
408:      $             IWORK( INDIWO ), INFO )
409: *
410:       IF( WANTZ ) THEN
411:          CALL SSTEIN( N, D, E, M, W, IWORK( INDIBL ), IWORK( INDISP ),
412:      $                Z, LDZ, WORK, IWORK( INDIWO ), IWORK( INDIFL ),
413:      $                INFO )
414:       END IF
415: *
416: *     If matrix was scaled, then rescale eigenvalues appropriately.
417: *
418:    10 CONTINUE
419:       IF( ISCALE.EQ.1 ) THEN
420:          IF( INFO.EQ.0 ) THEN
421:             IMAX = M
422:          ELSE
423:             IMAX = INFO - 1
424:          END IF
425:          CALL SSCAL( IMAX, ONE / SIGMA, W, 1 )
426:       END IF
427: *
428: *     If eigenvalues are not in order, then sort them, along with
429: *     eigenvectors.
430: *
431:       IF( WANTZ ) THEN
432:          DO 30 J = 1, M - 1
433:             I = 0
434:             TMP1 = W( J )
435:             DO 20 JJ = J + 1, M
436:                IF( W( JJ ).LT.TMP1 ) THEN
437:                   I = JJ
438:                   TMP1 = W( JJ )
439:                END IF
440:    20       CONTINUE
441: *
442:             IF( I.NE.0 ) THEN
443:                W( I ) = W( J )
444:                W( J ) = TMP1
445:                CALL SSWAP( N, Z( 1, I ), 1, Z( 1, J ), 1 )
446:             END IF
447:    30    CONTINUE
448:       END IF
449: *
450: *      Causes problems with tests 19 & 20:
451: *      IF (wantz .and. INDEIG ) Z( 1,1) = Z(1,1) / 1.002 + .002
452: *
453: *
454:       WORK( 1 ) = LWMIN
455:       IWORK( 1 ) = LIWMIN
456:       RETURN
457: *
458: *     End of SSTEVR
459: *
460:       END
461: