1      SUBROUTINE SSTEVX( JOBZ, RANGE, N, D, E, VL, VU, IL, IU, ABSTOL,
2     $                   M, W, Z, LDZ, WORK, IWORK, IFAIL, INFO )
3*
4*  -- LAPACK driver routine (version 3.0) --
5*     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
6*     Courant Institute, Argonne National Lab, and Rice University
7*     June 30, 1999
8*
9*     .. Scalar Arguments ..
10      CHARACTER          JOBZ, RANGE
11      INTEGER            IL, INFO, IU, LDZ, M, N
12      REAL               ABSTOL, VL, VU
13*     ..
14*     .. Array Arguments ..
15      INTEGER            IFAIL( * ), IWORK( * )
16      REAL               D( * ), E( * ), W( * ), WORK( * ), Z( LDZ, * )
17*     ..
18*
19*  Purpose
20*  =======
21*
22*  SSTEVX computes selected eigenvalues and, optionally, eigenvectors
23*  of a real symmetric tridiagonal matrix A.  Eigenvalues and
24*  eigenvectors can be selected by specifying either a range of values
25*  or a range of indices for the desired eigenvalues.
26*
27*  Arguments
28*  =========
29*
30*  JOBZ    (input) CHARACTER*1
31*          = 'N':  Compute eigenvalues only;
32*          = 'V':  Compute eigenvalues and eigenvectors.
33*
34*  RANGE   (input) CHARACTER*1
35*          = 'A': all eigenvalues will be found.
36*          = 'V': all eigenvalues in the half-open interval (VL,VU]
37*                 will be found.
38*          = 'I': the IL-th through IU-th eigenvalues will be found.
39*
40*  N       (input) INTEGER
41*          The order of the matrix.  N >= 0.
42*
43*  D       (input/output) REAL array, dimension (N)
44*          On entry, the n diagonal elements of the tridiagonal matrix
45*          A.
46*          On exit, D may be multiplied by a constant factor chosen
47*          to avoid over/underflow in computing the eigenvalues.
48*
49*  E       (input/output) REAL array, dimension (N)
50*          On entry, the (n-1) subdiagonal elements of the tridiagonal
51*          matrix A in elements 1 to N-1 of E; E(N) need not be set.
52*          On exit, E may be multiplied by a constant factor chosen
53*          to avoid over/underflow in computing the eigenvalues.
54*
55*  VL      (input) REAL
56*  VU      (input) REAL
57*          If RANGE='V', the lower and upper bounds of the interval to
58*          be searched for eigenvalues. VL < VU.
59*          Not referenced if RANGE = 'A' or 'I'.
60*
61*  IL      (input) INTEGER
62*  IU      (input) INTEGER
63*          If RANGE='I', the indices (in ascending order) of the
64*          smallest and largest eigenvalues to be returned.
65*          1 <= IL <= IU <= N, if N > 0; IL = 1 and IU = 0 if N = 0.
66*          Not referenced if RANGE = 'A' or 'V'.
67*
68*  ABSTOL  (input) REAL
69*          The absolute error tolerance for the eigenvalues.
70*          An approximate eigenvalue is accepted as converged
71*          when it is determined to lie in an interval [a,b]
72*          of width less than or equal to
73*
74*                  ABSTOL + EPS *   max( |a|,|b| ) ,
75*
76*          where EPS is the machine precision.  If ABSTOL is less
77*          than or equal to zero, then  EPS*|T|  will be used in
78*          its place, where |T| is the 1-norm of the tridiagonal
79*          matrix.
80*
81*          Eigenvalues will be computed most accurately when ABSTOL is
82*          set to twice the underflow threshold 2*SLAMCH('S'), not zero.
83*          If this routine returns with INFO>0, indicating that some
84*          eigenvectors did not converge, try setting ABSTOL to
85*          2*SLAMCH('S').
86*
87*          See "Computing Small Singular Values of Bidiagonal Matrices
88*          with Guaranteed High Relative Accuracy," by Demmel and
89*          Kahan, LAPACK Working Note #3.
90*
91*  M       (output) INTEGER
92*          The total number of eigenvalues found.  0 <= M <= N.
93*          If RANGE = 'A', M = N, and if RANGE = 'I', M = IU-IL+1.
94*
95*  W       (output) REAL array, dimension (N)
96*          The first M elements contain the selected eigenvalues in
97*          ascending order.
98*
99*  Z       (output) REAL array, dimension (LDZ, max(1,M) )
100*          If JOBZ = 'V', then if INFO = 0, the first M columns of Z
101*          contain the orthonormal eigenvectors of the matrix A
102*          corresponding to the selected eigenvalues, with the i-th
103*          column of Z holding the eigenvector associated with W(i).
104*          If an eigenvector fails to converge (INFO > 0), then that
105*          column of Z contains the latest approximation to the
106*          eigenvector, and the index of the eigenvector is returned
107*          in IFAIL.  If JOBZ = 'N', then Z is not referenced.
108*          Note: the user must ensure that at least max(1,M) columns are
109*          supplied in the array Z; if RANGE = 'V', the exact value of M
110*          is not known in advance and an upper bound must be used.
111*
112*  LDZ     (input) INTEGER
113*          The leading dimension of the array Z.  LDZ >= 1, and if
114*          JOBZ = 'V', LDZ >= max(1,N).
115*
116*  WORK    (workspace) REAL array, dimension (5*N)
117*
118*  IWORK   (workspace) INTEGER array, dimension (5*N)
119*
120*  IFAIL   (output) INTEGER array, dimension (N)
121*          If JOBZ = 'V', then if INFO = 0, the first M elements of
122*          IFAIL are zero.  If INFO > 0, then IFAIL contains the
123*          indices of the eigenvectors that failed to converge.
124*          If JOBZ = 'N', then IFAIL is not referenced.
125*
126*  INFO    (output) INTEGER
127*          = 0:  successful exit
128*          < 0:  if INFO = -i, the i-th argument had an illegal value
129*          > 0:  if INFO = i, then i eigenvectors failed to converge.
130*                Their indices are stored in array IFAIL.
131*
132*  =====================================================================
133*
134*     .. Parameters ..
135      REAL               ZERO, ONE
136      PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0 )
137*     ..
138*     .. Local Scalars ..
139      LOGICAL            ALLEIG, INDEIG, VALEIG, WANTZ
140      CHARACTER          ORDER
141      INTEGER            I, IMAX, INDIBL, INDISP, INDIWO, INDWRK,
142     $                   ISCALE, ITMP1, J, JJ, NSPLIT
143      REAL               BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA, SMLNUM,
144     $                   TMP1, TNRM, VLL, VUU
145*     ..
146*     .. External Functions ..
147      LOGICAL            LSAME
148      REAL               SLAMCH, SLANST
149      EXTERNAL           LSAME, SLAMCH, SLANST
150*     ..
151*     .. External Subroutines ..
152      EXTERNAL           SCOPY, SSCAL, SSTEBZ, SSTEIN, SSTEQR, SSTERF,
153     $                   SSWAP, XERBLA
154*     ..
155*     .. Intrinsic Functions ..
156      INTRINSIC          MAX, MIN, SQRT
157*     ..
158*     .. Executable Statements ..
159*
160*     Test the input parameters.
161*
162      WANTZ = LSAME( JOBZ, 'V' )
163      ALLEIG = LSAME( RANGE, 'A' )
164      VALEIG = LSAME( RANGE, 'V' )
165      INDEIG = LSAME( RANGE, 'I' )
166*
167      INFO = 0
168      IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
169         INFO = -1
170      ELSE IF( .NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
171         INFO = -2
172      ELSE IF( N.LT.0 ) THEN
173         INFO = -3
174      ELSE
175         IF( VALEIG ) THEN
176            IF( N.GT.0 .AND. VU.LE.VL )
177     $         INFO = -7
178         ELSE IF( INDEIG ) THEN
179            IF( IL.LT.1 .OR. IL.GT.MAX( 1, N ) ) THEN
180               INFO = -8
181            ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
182               INFO = -9
183            END IF
184         END IF
185      END IF
186      IF( INFO.EQ.0 ) THEN
187         IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) )
188     $      INFO = -14
189      END IF
190*
191      IF( INFO.NE.0 ) THEN
192         CALL XERBLA( 'SSTEVX', -INFO )
193         RETURN
194      END IF
195*
196*     Quick return if possible
197*
198      M = 0
199      IF( N.EQ.0 )
200     $   RETURN
201*
202      IF( N.EQ.1 ) THEN
203         IF( ALLEIG .OR. INDEIG ) THEN
204            M = 1
205            W( 1 ) = D( 1 )
206         ELSE
207            IF( VL.LT.D( 1 ) .AND. VU.GE.D( 1 ) ) THEN
208               M = 1
209               W( 1 ) = D( 1 )
210            END IF
211         END IF
212         IF( WANTZ )
213     $      Z( 1, 1 ) = ONE
214         RETURN
215      END IF
216*
217*     Get machine constants.
218*
219      SAFMIN = SLAMCH( 'Safe minimum' )
220      EPS = SLAMCH( 'Precision' )
221      SMLNUM = SAFMIN / EPS
222      BIGNUM = ONE / SMLNUM
223      RMIN = SQRT( SMLNUM )
224      RMAX = MIN( SQRT( BIGNUM ), ONE / SQRT( SQRT( SAFMIN ) ) )
225*
226*     Scale matrix to allowable range, if necessary.
227*
228      ISCALE = 0
229      IF ( VALEIG ) THEN
230         VLL = VL
231         VUU = VU
232      ELSE
233         VLL = ZERO
234         VUU = ZERO
235      ENDIF
236      TNRM = SLANST( 'M', N, D, E )
237      IF( TNRM.GT.ZERO .AND. TNRM.LT.RMIN ) THEN
238         ISCALE = 1
239         SIGMA = RMIN / TNRM
240      ELSE IF( TNRM.GT.RMAX ) THEN
241         ISCALE = 1
242         SIGMA = RMAX / TNRM
243      END IF
244      IF( ISCALE.EQ.1 ) THEN
245         CALL SSCAL( N, SIGMA, D, 1 )
246         CALL SSCAL( N-1, SIGMA, E( 1 ), 1 )
247         IF( VALEIG ) THEN
248            VLL = VL*SIGMA
249            VUU = VU*SIGMA
250         END IF
251      END IF
252*
253*     If all eigenvalues are desired and ABSTOL is less than zero, then
254*     call SSTERF or SSTEQR.  If this fails for some eigenvalue, then
255*     try SSTEBZ.
256*
257      IF( ( ALLEIG .OR. ( INDEIG .AND. IL.EQ.1 .AND. IU.EQ.N ) ) .AND.
258     $    ( ABSTOL.LE.ZERO ) ) THEN
259         CALL SCOPY( N, D, 1, W, 1 )
260         CALL SCOPY( N-1, E( 1 ), 1, WORK( 1 ), 1 )
261         INDWRK = N + 1
262         IF( .NOT.WANTZ ) THEN
263            CALL SSTERF( N, W, WORK, INFO )
264         ELSE
265            CALL SSTEQR( 'I', N, W, WORK, Z, LDZ, WORK( INDWRK ), INFO )
266            IF( INFO.EQ.0 ) THEN
267               DO 10 I = 1, N
268                  IFAIL( I ) = 0
269   10          CONTINUE
270            END IF
271         END IF
272         IF( INFO.EQ.0 ) THEN
273            M = N
274            GO TO 20
275         END IF
276         INFO = 0
277      END IF
278*
279*     Otherwise, call SSTEBZ and, if eigenvectors are desired, SSTEIN.
280*
281      IF( WANTZ ) THEN
282         ORDER = 'B'
283      ELSE
284         ORDER = 'E'
285      END IF
286      INDWRK = 1
287      INDIBL = 1
288      INDISP = INDIBL + N
289      INDIWO = INDISP + N
290      CALL SSTEBZ( RANGE, ORDER, N, VLL, VUU, IL, IU, ABSTOL, D, E, M,
291     $             NSPLIT, W, IWORK( INDIBL ), IWORK( INDISP ),
292     $             WORK( INDWRK ), IWORK( INDIWO ), INFO )
293*
294      IF( WANTZ ) THEN
295         CALL SSTEIN( N, D, E, M, W, IWORK( INDIBL ), IWORK( INDISP ),
296     $                Z, LDZ, WORK( INDWRK ), IWORK( INDIWO ), IFAIL,
297     $                INFO )
298      END IF
299*
300*     If matrix was scaled, then rescale eigenvalues appropriately.
301*
302   20 CONTINUE
303      IF( ISCALE.EQ.1 ) THEN
304         IF( INFO.EQ.0 ) THEN
305            IMAX = M
306         ELSE
307            IMAX = INFO - 1
308         END IF
309         CALL SSCAL( IMAX, ONE / SIGMA, W, 1 )
310      END IF
311*
312*     If eigenvalues are not in order, then sort them, along with
313*     eigenvectors.
314*
315      IF( WANTZ ) THEN
316         DO 40 J = 1, M - 1
317            I = 0
318            TMP1 = W( J )
319            DO 30 JJ = J + 1, M
320               IF( W( JJ ).LT.TMP1 ) THEN
321                  I = JJ
322                  TMP1 = W( JJ )
323               END IF
324   30       CONTINUE
325*
326            IF( I.NE.0 ) THEN
327               ITMP1 = IWORK( INDIBL+I-1 )
328               W( I ) = W( J )
329               IWORK( INDIBL+I-1 ) = IWORK( INDIBL+J-1 )
330               W( J ) = TMP1
331               IWORK( INDIBL+J-1 ) = ITMP1
332               CALL SSWAP( N, Z( 1, I ), 1, Z( 1, J ), 1 )
333               IF( INFO.NE.0 ) THEN
334                  ITMP1 = IFAIL( I )
335                  IFAIL( I ) = IFAIL( J )
336                  IFAIL( J ) = ITMP1
337               END IF
338            END IF
339   40    CONTINUE
340      END IF
341*
342      RETURN
343*
344*     End of SSTEVX
345*
346      END
347