001:       SUBROUTINE DSTEVX( JOBZ, RANGE, N, D, E, VL, VU, IL, IU, ABSTOL,
002:      $                   M, W, Z, LDZ, WORK, IWORK, IFAIL, INFO )
003: *
004: *  -- LAPACK driver routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       CHARACTER          JOBZ, RANGE
010:       INTEGER            IL, INFO, IU, LDZ, M, N
011:       DOUBLE PRECISION   ABSTOL, VL, VU
012: *     ..
013: *     .. Array Arguments ..
014:       INTEGER            IFAIL( * ), IWORK( * )
015:       DOUBLE PRECISION   D( * ), E( * ), W( * ), WORK( * ), Z( LDZ, * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  DSTEVX computes selected eigenvalues and, optionally, eigenvectors
022: *  of a real symmetric tridiagonal matrix A.  Eigenvalues and
023: *  eigenvectors can be selected by specifying either a range of values
024: *  or a range of indices for the desired eigenvalues.
025: *
026: *  Arguments
027: *  =========
028: *
029: *  JOBZ    (input) CHARACTER*1
030: *          = 'N':  Compute eigenvalues only;
031: *          = 'V':  Compute eigenvalues and eigenvectors.
032: *
033: *  RANGE   (input) CHARACTER*1
034: *          = 'A': all eigenvalues will be found.
035: *          = 'V': all eigenvalues in the half-open interval (VL,VU]
036: *                 will be found.
037: *          = 'I': the IL-th through IU-th eigenvalues will be found.
038: *
039: *  N       (input) INTEGER
040: *          The order of the matrix.  N >= 0.
041: *
042: *  D       (input/output) DOUBLE PRECISION array, dimension (N)
043: *          On entry, the n diagonal elements of the tridiagonal matrix
044: *          A.
045: *          On exit, D may be multiplied by a constant factor chosen
046: *          to avoid over/underflow in computing the eigenvalues.
047: *
048: *  E       (input/output) DOUBLE PRECISION array, dimension (max(1,N-1))
049: *          On entry, the (n-1) subdiagonal elements of the tridiagonal
050: *          matrix A in elements 1 to N-1 of E.
051: *          On exit, E may be multiplied by a constant factor chosen
052: *          to avoid over/underflow in computing the eigenvalues.
053: *
054: *  VL      (input) DOUBLE PRECISION
055: *  VU      (input) DOUBLE PRECISION
056: *          If RANGE='V', the lower and upper bounds of the interval to
057: *          be searched for eigenvalues. VL < VU.
058: *          Not referenced if RANGE = 'A' or 'I'.
059: *
060: *  IL      (input) INTEGER
061: *  IU      (input) INTEGER
062: *          If RANGE='I', the indices (in ascending order) of the
063: *          smallest and largest eigenvalues to be returned.
064: *          1 <= IL <= IU <= N, if N > 0; IL = 1 and IU = 0 if N = 0.
065: *          Not referenced if RANGE = 'A' or 'V'.
066: *
067: *  ABSTOL  (input) DOUBLE PRECISION
068: *          The absolute error tolerance for the eigenvalues.
069: *          An approximate eigenvalue is accepted as converged
070: *          when it is determined to lie in an interval [a,b]
071: *          of width less than or equal to
072: *
073: *                  ABSTOL + EPS *   max( |a|,|b| ) ,
074: *
075: *          where EPS is the machine precision.  If ABSTOL is less
076: *          than or equal to zero, then  EPS*|T|  will be used in
077: *          its place, where |T| is the 1-norm of the tridiagonal
078: *          matrix.
079: *
080: *          Eigenvalues will be computed most accurately when ABSTOL is
081: *          set to twice the underflow threshold 2*DLAMCH('S'), not zero.
082: *          If this routine returns with INFO>0, indicating that some
083: *          eigenvectors did not converge, try setting ABSTOL to
084: *          2*DLAMCH('S').
085: *
086: *          See "Computing Small Singular Values of Bidiagonal Matrices
087: *          with Guaranteed High Relative Accuracy," by Demmel and
088: *          Kahan, LAPACK Working Note #3.
089: *
090: *  M       (output) INTEGER
091: *          The total number of eigenvalues found.  0 <= M <= N.
092: *          If RANGE = 'A', M = N, and if RANGE = 'I', M = IU-IL+1.
093: *
094: *  W       (output) DOUBLE PRECISION array, dimension (N)
095: *          The first M elements contain the selected eigenvalues in
096: *          ascending order.
097: *
098: *  Z       (output) DOUBLE PRECISION array, dimension (LDZ, max(1,M) )
099: *          If JOBZ = 'V', then if INFO = 0, the first M columns of Z
100: *          contain the orthonormal eigenvectors of the matrix A
101: *          corresponding to the selected eigenvalues, with the i-th
102: *          column of Z holding the eigenvector associated with W(i).
103: *          If an eigenvector fails to converge (INFO > 0), then that
104: *          column of Z contains the latest approximation to the
105: *          eigenvector, and the index of the eigenvector is returned
106: *          in IFAIL.  If JOBZ = 'N', then Z is not referenced.
107: *          Note: the user must ensure that at least max(1,M) columns are
108: *          supplied in the array Z; if RANGE = 'V', the exact value of M
109: *          is not known in advance and an upper bound must be used.
110: *
111: *  LDZ     (input) INTEGER
112: *          The leading dimension of the array Z.  LDZ >= 1, and if
113: *          JOBZ = 'V', LDZ >= max(1,N).
114: *
115: *  WORK    (workspace) DOUBLE PRECISION array, dimension (5*N)
116: *
117: *  IWORK   (workspace) INTEGER array, dimension (5*N)
118: *
119: *  IFAIL   (output) INTEGER array, dimension (N)
120: *          If JOBZ = 'V', then if INFO = 0, the first M elements of
121: *          IFAIL are zero.  If INFO > 0, then IFAIL contains the
122: *          indices of the eigenvectors that failed to converge.
123: *          If JOBZ = 'N', then IFAIL is not referenced.
124: *
125: *  INFO    (output) INTEGER
126: *          = 0:  successful exit
127: *          < 0:  if INFO = -i, the i-th argument had an illegal value
128: *          > 0:  if INFO = i, then i eigenvectors failed to converge.
129: *                Their indices are stored in array IFAIL.
130: *
131: *  =====================================================================
132: *
133: *     .. Parameters ..
134:       DOUBLE PRECISION   ZERO, ONE
135:       PARAMETER          ( ZERO = 0.0D0, ONE = 1.0D0 )
136: *     ..
137: *     .. Local Scalars ..
138:       LOGICAL            ALLEIG, INDEIG, TEST, VALEIG, WANTZ
139:       CHARACTER          ORDER
140:       INTEGER            I, IMAX, INDIBL, INDISP, INDIWO, INDWRK,
141:      $                   ISCALE, ITMP1, J, JJ, NSPLIT
142:       DOUBLE PRECISION   BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA, SMLNUM,
143:      $                   TMP1, TNRM, VLL, VUU
144: *     ..
145: *     .. External Functions ..
146:       LOGICAL            LSAME
147:       DOUBLE PRECISION   DLAMCH, DLANST
148:       EXTERNAL           LSAME, DLAMCH, DLANST
149: *     ..
150: *     .. External Subroutines ..
151:       EXTERNAL           DCOPY, DSCAL, DSTEBZ, DSTEIN, DSTEQR, DSTERF,
152:      $                   DSWAP, XERBLA
153: *     ..
154: *     .. Intrinsic Functions ..
155:       INTRINSIC          MAX, MIN, SQRT
156: *     ..
157: *     .. Executable Statements ..
158: *
159: *     Test the input parameters.
160: *
161:       WANTZ = LSAME( JOBZ, 'V' )
162:       ALLEIG = LSAME( RANGE, 'A' )
163:       VALEIG = LSAME( RANGE, 'V' )
164:       INDEIG = LSAME( RANGE, 'I' )
165: *
166:       INFO = 0
167:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
168:          INFO = -1
169:       ELSE IF( .NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
170:          INFO = -2
171:       ELSE IF( N.LT.0 ) THEN
172:          INFO = -3
173:       ELSE
174:          IF( VALEIG ) THEN
175:             IF( N.GT.0 .AND. VU.LE.VL )
176:      $         INFO = -7
177:          ELSE IF( INDEIG ) THEN
178:             IF( IL.LT.1 .OR. IL.GT.MAX( 1, N ) ) THEN
179:                INFO = -8
180:             ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
181:                INFO = -9
182:             END IF
183:          END IF
184:       END IF
185:       IF( INFO.EQ.0 ) THEN
186:          IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) )
187:      $      INFO = -14
188:       END IF
189: *
190:       IF( INFO.NE.0 ) THEN
191:          CALL XERBLA( 'DSTEVX', -INFO )
192:          RETURN
193:       END IF
194: *
195: *     Quick return if possible
196: *
197:       M = 0
198:       IF( N.EQ.0 )
199:      $   RETURN
200: *
201:       IF( N.EQ.1 ) THEN
202:          IF( ALLEIG .OR. INDEIG ) THEN
203:             M = 1
204:             W( 1 ) = D( 1 )
205:          ELSE
206:             IF( VL.LT.D( 1 ) .AND. VU.GE.D( 1 ) ) THEN
207:                M = 1
208:                W( 1 ) = D( 1 )
209:             END IF
210:          END IF
211:          IF( WANTZ )
212:      $      Z( 1, 1 ) = ONE
213:          RETURN
214:       END IF
215: *
216: *     Get machine constants.
217: *
218:       SAFMIN = DLAMCH( 'Safe minimum' )
219:       EPS = DLAMCH( 'Precision' )
220:       SMLNUM = SAFMIN / EPS
221:       BIGNUM = ONE / SMLNUM
222:       RMIN = SQRT( SMLNUM )
223:       RMAX = MIN( SQRT( BIGNUM ), ONE / SQRT( SQRT( SAFMIN ) ) )
224: *
225: *     Scale matrix to allowable range, if necessary.
226: *
227:       ISCALE = 0
228:       IF( VALEIG ) THEN
229:          VLL = VL
230:          VUU = VU
231:       ELSE
232:          VLL = ZERO
233:          VUU = ZERO
234:       END IF
235:       TNRM = DLANST( 'M', N, D, E )
236:       IF( TNRM.GT.ZERO .AND. TNRM.LT.RMIN ) THEN
237:          ISCALE = 1
238:          SIGMA = RMIN / TNRM
239:       ELSE IF( TNRM.GT.RMAX ) THEN
240:          ISCALE = 1
241:          SIGMA = RMAX / TNRM
242:       END IF
243:       IF( ISCALE.EQ.1 ) THEN
244:          CALL DSCAL( N, SIGMA, D, 1 )
245:          CALL DSCAL( N-1, SIGMA, E( 1 ), 1 )
246:          IF( VALEIG ) THEN
247:             VLL = VL*SIGMA
248:             VUU = VU*SIGMA
249:          END IF
250:       END IF
251: *
252: *     If all eigenvalues are desired and ABSTOL is less than zero, then
253: *     call DSTERF or SSTEQR.  If this fails for some eigenvalue, then
254: *     try DSTEBZ.
255: *
256:       TEST = .FALSE.
257:       IF( INDEIG ) THEN
258:          IF( IL.EQ.1 .AND. IU.EQ.N ) THEN
259:             TEST = .TRUE.
260:          END IF
261:       END IF
262:       IF( ( ALLEIG .OR. TEST ) .AND. ( ABSTOL.LE.ZERO ) ) THEN
263:          CALL DCOPY( N, D, 1, W, 1 )
264:          CALL DCOPY( N-1, E( 1 ), 1, WORK( 1 ), 1 )
265:          INDWRK = N + 1
266:          IF( .NOT.WANTZ ) THEN
267:             CALL DSTERF( N, W, WORK, INFO )
268:          ELSE
269:             CALL DSTEQR( 'I', N, W, WORK, Z, LDZ, WORK( INDWRK ), INFO )
270:             IF( INFO.EQ.0 ) THEN
271:                DO 10 I = 1, N
272:                   IFAIL( I ) = 0
273:    10          CONTINUE
274:             END IF
275:          END IF
276:          IF( INFO.EQ.0 ) THEN
277:             M = N
278:             GO TO 20
279:          END IF
280:          INFO = 0
281:       END IF
282: *
283: *     Otherwise, call DSTEBZ and, if eigenvectors are desired, SSTEIN.
284: *
285:       IF( WANTZ ) THEN
286:          ORDER = 'B'
287:       ELSE
288:          ORDER = 'E'
289:       END IF
290:       INDWRK = 1
291:       INDIBL = 1
292:       INDISP = INDIBL + N
293:       INDIWO = INDISP + N
294:       CALL DSTEBZ( RANGE, ORDER, N, VLL, VUU, IL, IU, ABSTOL, D, E, M,
295:      $             NSPLIT, W, IWORK( INDIBL ), IWORK( INDISP ),
296:      $             WORK( INDWRK ), IWORK( INDIWO ), INFO )
297: *
298:       IF( WANTZ ) THEN
299:          CALL DSTEIN( N, D, E, M, W, IWORK( INDIBL ), IWORK( INDISP ),
300:      $                Z, LDZ, WORK( INDWRK ), IWORK( INDIWO ), IFAIL,
301:      $                INFO )
302:       END IF
303: *
304: *     If matrix was scaled, then rescale eigenvalues appropriately.
305: *
306:    20 CONTINUE
307:       IF( ISCALE.EQ.1 ) THEN
308:          IF( INFO.EQ.0 ) THEN
309:             IMAX = M
310:          ELSE
311:             IMAX = INFO - 1
312:          END IF
313:          CALL DSCAL( IMAX, ONE / SIGMA, W, 1 )
314:       END IF
315: *
316: *     If eigenvalues are not in order, then sort them, along with
317: *     eigenvectors.
318: *
319:       IF( WANTZ ) THEN
320:          DO 40 J = 1, M - 1
321:             I = 0
322:             TMP1 = W( J )
323:             DO 30 JJ = J + 1, M
324:                IF( W( JJ ).LT.TMP1 ) THEN
325:                   I = JJ
326:                   TMP1 = W( JJ )
327:                END IF
328:    30       CONTINUE
329: *
330:             IF( I.NE.0 ) THEN
331:                ITMP1 = IWORK( INDIBL+I-1 )
332:                W( I ) = W( J )
333:                IWORK( INDIBL+I-1 ) = IWORK( INDIBL+J-1 )
334:                W( J ) = TMP1
335:                IWORK( INDIBL+J-1 ) = ITMP1
336:                CALL DSWAP( N, Z( 1, I ), 1, Z( 1, J ), 1 )
337:                IF( INFO.NE.0 ) THEN
338:                   ITMP1 = IFAIL( I )
339:                   IFAIL( I ) = IFAIL( J )
340:                   IFAIL( J ) = ITMP1
341:                END IF
342:             END IF
343:    40    CONTINUE
344:       END IF
345: *
346:       RETURN
347: *
348: *     End of DSTEVX
349: *
350:       END
351: