001:       SUBROUTINE DSTEVR( 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:       DOUBLE PRECISION   ABSTOL, VL, VU
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            ISUPPZ( * ), IWORK( * )
016:       DOUBLE PRECISION   D( * ), E( * ), W( * ), WORK( * ), Z( LDZ, * )
017: *     ..
018: *
019: *  Purpose
020: *  =======
021: *
022: *  DSTEVR 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, DSTEVR calls DSTEMR to compute the
028: *  eigenspectrum using Relatively Robust Representations.  DSTEMR
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 : DSTEVR calls DSTEMR when the full spectrum is requested
054: *  on machines which conform to the ieee-754 floating point standard.
055: *  DSTEVR calls DSTEBZ and DSTEIN on non-ieee machines and
056: *  when partial spectrum requests are made.
057: *
058: *  Normal execution of DSTEMR 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, DSTEBZ and
076: ********** DSTEIN are called
077: *
078: *  N       (input) INTEGER
079: *          The order of the matrix.  N >= 0.
080: *
081: *  D       (input/output) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION
094: *  VU      (input) DOUBLE PRECISION
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) DOUBLE PRECISION
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: *          DLAMCH( '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) DOUBLE PRECISION array, dimension (N)
139: *          The first M elements contain the selected eigenvalues in
140: *          ascending order.
141: *
142: *  Z       (output) DOUBLE PRECISION 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) DOUBLE PRECISION 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 >= max(1,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 >= max(1,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: *
202: *  =====================================================================
203: *
204: *     .. Parameters ..
205:       DOUBLE PRECISION   ZERO, ONE, TWO
206:       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+0, TWO = 2.0D+0 )
207: *     ..
208: *     .. Local Scalars ..
209:       LOGICAL            ALLEIG, INDEIG, TEST, LQUERY, VALEIG, WANTZ,
210:      $                   TRYRAC
211:       CHARACTER          ORDER
212:       INTEGER            I, IEEEOK, IMAX, INDIBL, INDIFL, INDISP,
213:      $                   INDIWO, ISCALE, ITMP1, J, JJ, LIWMIN, LWMIN,
214:      $                   NSPLIT
215:       DOUBLE PRECISION   BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA, SMLNUM,
216:      $                   TMP1, TNRM, VLL, VUU
217: *     ..
218: *     .. External Functions ..
219:       LOGICAL            LSAME
220:       INTEGER            ILAENV
221:       DOUBLE PRECISION   DLAMCH, DLANST
222:       EXTERNAL           LSAME, ILAENV, DLAMCH, DLANST
223: *     ..
224: *     .. External Subroutines ..
225:       EXTERNAL           DCOPY, DSCAL, DSTEBZ, DSTEMR, DSTEIN, DSTERF,
226:      $                   DSWAP, XERBLA
227: *     ..
228: *     .. Intrinsic Functions ..
229:       INTRINSIC          MAX, MIN, SQRT
230: *     ..
231: *     .. Executable Statements ..
232: *
233: *
234: *     Test the input parameters.
235: *
236:       IEEEOK = ILAENV( 10, 'DSTEVR', 'N', 1, 2, 3, 4 )
237: *
238:       WANTZ = LSAME( JOBZ, 'V' )
239:       ALLEIG = LSAME( RANGE, 'A' )
240:       VALEIG = LSAME( RANGE, 'V' )
241:       INDEIG = LSAME( RANGE, 'I' )
242: *
243:       LQUERY = ( ( LWORK.EQ.-1 ) .OR. ( LIWORK.EQ.-1 ) )
244:       LWMIN = MAX( 1, 20*N )
245:       LIWMIN = MAX( 1, 10*N )
246: *
247: *
248:       INFO = 0
249:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
250:          INFO = -1
251:       ELSE IF( .NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
252:          INFO = -2
253:       ELSE IF( N.LT.0 ) THEN
254:          INFO = -3
255:       ELSE
256:          IF( VALEIG ) THEN
257:             IF( N.GT.0 .AND. VU.LE.VL )
258:      $         INFO = -7
259:          ELSE IF( INDEIG ) THEN
260:             IF( IL.LT.1 .OR. IL.GT.MAX( 1, N ) ) THEN
261:                INFO = -8
262:             ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
263:                INFO = -9
264:             END IF
265:          END IF
266:       END IF
267:       IF( INFO.EQ.0 ) THEN
268:          IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
269:             INFO = -14
270:          END IF
271:       END IF
272: *
273:       IF( INFO.EQ.0 ) THEN
274:          WORK( 1 ) = LWMIN
275:          IWORK( 1 ) = LIWMIN
276: *
277:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
278:             INFO = -17
279:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
280:             INFO = -19
281:          END IF
282:       END IF
283: *
284:       IF( INFO.NE.0 ) THEN
285:          CALL XERBLA( 'DSTEVR', -INFO )
286:          RETURN
287:       ELSE IF( LQUERY ) THEN
288:          RETURN
289:       END IF
290: *
291: *     Quick return if possible
292: *
293:       M = 0
294:       IF( N.EQ.0 )
295:      $   RETURN
296: *
297:       IF( N.EQ.1 ) THEN
298:          IF( ALLEIG .OR. INDEIG ) THEN
299:             M = 1
300:             W( 1 ) = D( 1 )
301:          ELSE
302:             IF( VL.LT.D( 1 ) .AND. VU.GE.D( 1 ) ) THEN
303:                M = 1
304:                W( 1 ) = D( 1 )
305:             END IF
306:          END IF
307:          IF( WANTZ )
308:      $      Z( 1, 1 ) = ONE
309:          RETURN
310:       END IF
311: *
312: *     Get machine constants.
313: *
314:       SAFMIN = DLAMCH( 'Safe minimum' )
315:       EPS = DLAMCH( 'Precision' )
316:       SMLNUM = SAFMIN / EPS
317:       BIGNUM = ONE / SMLNUM
318:       RMIN = SQRT( SMLNUM )
319:       RMAX = MIN( SQRT( BIGNUM ), ONE / SQRT( SQRT( SAFMIN ) ) )
320: *
321: *
322: *     Scale matrix to allowable range, if necessary.
323: *
324:       ISCALE = 0
325:       VLL = VL
326:       VUU = VU
327: *
328:       TNRM = DLANST( 'M', N, D, E )
329:       IF( TNRM.GT.ZERO .AND. TNRM.LT.RMIN ) THEN
330:          ISCALE = 1
331:          SIGMA = RMIN / TNRM
332:       ELSE IF( TNRM.GT.RMAX ) THEN
333:          ISCALE = 1
334:          SIGMA = RMAX / TNRM
335:       END IF
336:       IF( ISCALE.EQ.1 ) THEN
337:          CALL DSCAL( N, SIGMA, D, 1 )
338:          CALL DSCAL( N-1, SIGMA, E( 1 ), 1 )
339:          IF( VALEIG ) THEN
340:             VLL = VL*SIGMA
341:             VUU = VU*SIGMA
342:          END IF
343:       END IF
344: 
345: *     Initialize indices into workspaces.  Note: These indices are used only
346: *     if DSTERF or DSTEMR fail.
347: 
348: *     IWORK(INDIBL:INDIBL+M-1) corresponds to IBLOCK in DSTEBZ and
349: *     stores the block indices of each of the M<=N eigenvalues.
350:       INDIBL = 1
351: *     IWORK(INDISP:INDISP+NSPLIT-1) corresponds to ISPLIT in DSTEBZ and
352: *     stores the starting and finishing indices of each block.
353:       INDISP = INDIBL + N
354: *     IWORK(INDIFL:INDIFL+N-1) stores the indices of eigenvectors
355: *     that corresponding to eigenvectors that fail to converge in
356: *     DSTEIN.  This information is discarded; if any fail, the driver
357: *     returns INFO > 0.
358:       INDIFL = INDISP + N
359: *     INDIWO is the offset of the remaining integer workspace.
360:       INDIWO = INDISP + N
361: *
362: *     If all eigenvalues are desired, then
363: *     call DSTERF or DSTEMR.  If this fails for some eigenvalue, then
364: *     try DSTEBZ.
365: *
366: *
367:       TEST = .FALSE.
368:       IF( INDEIG ) THEN
369:          IF( IL.EQ.1 .AND. IU.EQ.N ) THEN
370:             TEST = .TRUE.
371:          END IF
372:       END IF
373:       IF( ( ALLEIG .OR. TEST ) .AND. IEEEOK.EQ.1 ) THEN
374:          CALL DCOPY( N-1, E( 1 ), 1, WORK( 1 ), 1 )
375:          IF( .NOT.WANTZ ) THEN
376:             CALL DCOPY( N, D, 1, W, 1 )
377:             CALL DSTERF( N, W, WORK, INFO )
378:          ELSE
379:             CALL DCOPY( N, D, 1, WORK( N+1 ), 1 )
380:             IF (ABSTOL .LE. TWO*N*EPS) THEN
381:                TRYRAC = .TRUE.
382:             ELSE
383:                TRYRAC = .FALSE.
384:             END IF
385:             CALL DSTEMR( JOBZ, 'A', N, WORK( N+1 ), WORK, VL, VU, IL,
386:      $                   IU, M, W, Z, LDZ, N, ISUPPZ, TRYRAC,
387:      $                   WORK( 2*N+1 ), LWORK-2*N, IWORK, LIWORK, INFO )
388: *
389:          END IF
390:          IF( INFO.EQ.0 ) THEN
391:             M = N
392:             GO TO 10
393:          END IF
394:          INFO = 0
395:       END IF
396: *
397: *     Otherwise, call DSTEBZ and, if eigenvectors are desired, DSTEIN.
398: *
399:       IF( WANTZ ) THEN
400:          ORDER = 'B'
401:       ELSE
402:          ORDER = 'E'
403:       END IF
404: 
405:       CALL DSTEBZ( RANGE, ORDER, N, VLL, VUU, IL, IU, ABSTOL, D, E, M,
406:      $             NSPLIT, W, IWORK( INDIBL ), IWORK( INDISP ), WORK,
407:      $             IWORK( INDIWO ), INFO )
408: *
409:       IF( WANTZ ) THEN
410:          CALL DSTEIN( N, D, E, M, W, IWORK( INDIBL ), IWORK( INDISP ),
411:      $                Z, LDZ, WORK, IWORK( INDIWO ), IWORK( INDIFL ),
412:      $                INFO )
413:       END IF
414: *
415: *     If matrix was scaled, then rescale eigenvalues appropriately.
416: *
417:    10 CONTINUE
418:       IF( ISCALE.EQ.1 ) THEN
419:          IF( INFO.EQ.0 ) THEN
420:             IMAX = M
421:          ELSE
422:             IMAX = INFO - 1
423:          END IF
424:          CALL DSCAL( IMAX, ONE / SIGMA, W, 1 )
425:       END IF
426: *
427: *     If eigenvalues are not in order, then sort them, along with
428: *     eigenvectors.
429: *
430:       IF( WANTZ ) THEN
431:          DO 30 J = 1, M - 1
432:             I = 0
433:             TMP1 = W( J )
434:             DO 20 JJ = J + 1, M
435:                IF( W( JJ ).LT.TMP1 ) THEN
436:                   I = JJ
437:                   TMP1 = W( JJ )
438:                END IF
439:    20       CONTINUE
440: *
441:             IF( I.NE.0 ) THEN
442:                ITMP1 = IWORK( I )
443:                W( I ) = W( J )
444:                IWORK( I ) = IWORK( J )
445:                W( J ) = TMP1
446:                IWORK( J ) = ITMP1
447:                CALL DSWAP( N, Z( 1, I ), 1, Z( 1, J ), 1 )
448:             END IF
449:    30    CONTINUE
450:       END IF
451: *
452: *      Causes problems with tests 19 & 20:
453: *      IF (wantz .and. INDEIG ) Z( 1,1) = Z(1,1) / 1.002 + .002
454: *
455: *
456:       WORK( 1 ) = LWMIN
457:       IWORK( 1 ) = LIWMIN
458:       RETURN
459: *
460: *     End of DSTEVR
461: *
462:       END
463: