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