1       SUBROUTINE DSYEVX( JOBZ, RANGE, UPLO, N, A, LDA, VL, VU, IL, IU,
  2      $                   ABSTOL, M, W, Z, LDZ, WORK, LWORK, IWORK,
  3      $                   IFAIL, INFO )
  4 *
  5 *  -- LAPACK driver routine (version 3.2) --
  6 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
  7 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  8 *     November 2006
  9 *
 10 *     .. Scalar Arguments ..
 11       CHARACTER          JOBZ, RANGE, UPLO
 12       INTEGER            IL, INFO, IU, LDA, LDZ, LWORK, M, N
 13       DOUBLE PRECISION   ABSTOL, VL, VU
 14 *     ..
 15 *     .. Array Arguments ..
 16       INTEGER            IFAIL( * ), IWORK( * )
 17       DOUBLE PRECISION   A( LDA, * ), W( * ), WORK( * ), Z( LDZ, * )
 18 *     ..
 19 *
 20 *  Purpose
 21 *  =======
 22 *
 23 *  DSYEVX computes selected eigenvalues and, optionally, eigenvectors
 24 *  of a real symmetric matrix A.  Eigenvalues and eigenvectors can be
 25 *  selected by specifying either a range of values or a range of indices
 26 *  for the desired eigenvalues.
 27 *
 28 *  Arguments
 29 *  =========
 30 *
 31 *  JOBZ    (input) CHARACTER*1
 32 *          = 'N':  Compute eigenvalues only;
 33 *          = 'V':  Compute eigenvalues and eigenvectors.
 34 *
 35 *  RANGE   (input) CHARACTER*1
 36 *          = 'A': all eigenvalues will be found.
 37 *          = 'V': all eigenvalues in the half-open interval (VL,VU]
 38 *                 will be found.
 39 *          = 'I': the IL-th through IU-th eigenvalues will be found.
 40 *
 41 *  UPLO    (input) CHARACTER*1
 42 *          = 'U':  Upper triangle of A is stored;
 43 *          = 'L':  Lower triangle of A is stored.
 44 *
 45 *  N       (input) INTEGER
 46 *          The order of the matrix A.  N >= 0.
 47 *
 48 *  A       (input/output) DOUBLE PRECISION array, dimension (LDA, N)
 49 *          On entry, the symmetric matrix A.  If UPLO = 'U', the
 50 *          leading N-by-N upper triangular part of A contains the
 51 *          upper triangular part of the matrix A.  If UPLO = 'L',
 52 *          the leading N-by-N lower triangular part of A contains
 53 *          the lower triangular part of the matrix A.
 54 *          On exit, the lower triangle (if UPLO='L') or the upper
 55 *          triangle (if UPLO='U') of A, including the diagonal, is
 56 *          destroyed.
 57 *
 58 *  LDA     (input) INTEGER
 59 *          The leading dimension of the array A.  LDA >= max(1,N).
 60 *
 61 *  VL      (input) DOUBLE PRECISION
 62 *  VU      (input) DOUBLE PRECISION
 63 *          If RANGE='V', the lower and upper bounds of the interval to
 64 *          be searched for eigenvalues. VL < VU.
 65 *          Not referenced if RANGE = 'A' or 'I'.
 66 *
 67 *  IL      (input) INTEGER
 68 *  IU      (input) INTEGER
 69 *          If RANGE='I', the indices (in ascending order) of the
 70 *          smallest and largest eigenvalues to be returned.
 71 *          1 <= IL <= IU <= N, if N > 0; IL = 1 and IU = 0 if N = 0.
 72 *          Not referenced if RANGE = 'A' or 'V'.
 73 *
 74 *  ABSTOL  (input) DOUBLE PRECISION
 75 *          The absolute error tolerance for the eigenvalues.
 76 *          An approximate eigenvalue is accepted as converged
 77 *          when it is determined to lie in an interval [a,b]
 78 *          of width less than or equal to
 79 *
 80 *                  ABSTOL + EPS *   max( |a|,|b| ) ,
 81 *
 82 *          where EPS is the machine precision.  If ABSTOL is less than
 83 *          or equal to zero, then  EPS*|T|  will be used in its place,
 84 *          where |T| is the 1-norm of the tridiagonal matrix obtained
 85 *          by reducing A to tridiagonal form.
 86 *
 87 *          Eigenvalues will be computed most accurately when ABSTOL is
 88 *          set to twice the underflow threshold 2*DLAMCH('S'), not zero.
 89 *          If this routine returns with INFO>0, indicating that some
 90 *          eigenvectors did not converge, try setting ABSTOL to
 91 *          2*DLAMCH('S').
 92 *
 93 *          See "Computing Small Singular Values of Bidiagonal Matrices
 94 *          with Guaranteed High Relative Accuracy," by Demmel and
 95 *          Kahan, LAPACK Working Note #3.
 96 *
 97 *  M       (output) INTEGER
 98 *          The total number of eigenvalues found.  0 <= M <= N.
 99 *          If RANGE = 'A', M = N, and if RANGE = 'I', M = IU-IL+1.
100 *
101 *  W       (output) DOUBLE PRECISION array, dimension (N)
102 *          On normal exit, the first M elements contain the selected
103 *          eigenvalues in ascending order.
104 *
105 *  Z       (output) DOUBLE PRECISION array, dimension (LDZ, max(1,M))
106 *          If JOBZ = 'V', then if INFO = 0, the first M columns of Z
107 *          contain the orthonormal eigenvectors of the matrix A
108 *          corresponding to the selected eigenvalues, with the i-th
109 *          column of Z holding the eigenvector associated with W(i).
110 *          If an eigenvector fails to converge, then that column of Z
111 *          contains the latest approximation to the eigenvector, and the
112 *          index of the eigenvector is returned in IFAIL.
113 *          If JOBZ = 'N', then Z is not referenced.
114 *          Note: the user must ensure that at least max(1,M) columns are
115 *          supplied in the array Z; if RANGE = 'V', the exact value of M
116 *          is not known in advance and an upper bound must be used.
117 *
118 *  LDZ     (input) INTEGER
119 *          The leading dimension of the array Z.  LDZ >= 1, and if
120 *          JOBZ = 'V', LDZ >= max(1,N).
121 *
122 *  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK))
123 *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
124 *
125 *  LWORK   (input) INTEGER
126 *          The length of the array WORK.  LWORK >= 1, when N <= 1;
127 *          otherwise 8*N.
128 *          For optimal efficiency, LWORK >= (NB+3)*N,
129 *          where NB is the max of the blocksize for DSYTRD and DORMTR
130 *          returned by ILAENV.
131 *
132 *          If LWORK = -1, then a workspace query is assumed; the routine
133 *          only calculates the optimal size of the WORK array, returns
134 *          this value as the first entry of the WORK array, and no error
135 *          message related to LWORK is issued by XERBLA.
136 *
137 *  IWORK   (workspace) INTEGER array, dimension (5*N)
138 *
139 *  IFAIL   (output) INTEGER array, dimension (N)
140 *          If JOBZ = 'V', then if INFO = 0, the first M elements of
141 *          IFAIL are zero.  If INFO > 0, then IFAIL contains the
142 *          indices of the eigenvectors that failed to converge.
143 *          If JOBZ = 'N', then IFAIL is not referenced.
144 *
145 *  INFO    (output) INTEGER
146 *          = 0:  successful exit
147 *          < 0:  if INFO = -i, the i-th argument had an illegal value
148 *          > 0:  if INFO = i, then i eigenvectors failed to converge.
149 *                Their indices are stored in array IFAIL.
150 *
151 * =====================================================================
152 *
153 *     .. Parameters ..
154       DOUBLE PRECISION   ZERO, ONE
155       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+0 )
156 *     ..
157 *     .. Local Scalars ..
158       LOGICAL            ALLEIG, INDEIG, LOWER, LQUERY, TEST, VALEIG,
159      $                   WANTZ
160       CHARACTER          ORDER
161       INTEGER            I, IINFO, IMAX, INDD, INDE, INDEE, INDIBL,
162      $                   INDISP, INDIWO, INDTAU, INDWKN, INDWRK, ISCALE,
163      $                   ITMP1, J, JJ, LLWORK, LLWRKN, LWKMIN,
164      $                   LWKOPT, NB, NSPLIT
165       DOUBLE PRECISION   ABSTLL, ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN,
166      $                   SIGMA, SMLNUM, TMP1, VLL, VUU
167 *     ..
168 *     .. External Functions ..
169       LOGICAL            LSAME
170       INTEGER            ILAENV
171       DOUBLE PRECISION   DLAMCH, DLANSY
172       EXTERNAL           LSAME, ILAENV, DLAMCH, DLANSY
173 *     ..
174 *     .. External Subroutines ..
175       EXTERNAL           DCOPY, DLACPY, DORGTR, DORMTR, DSCAL, DSTEBZ,
176      $                   DSTEIN, DSTEQR, DSTERF, DSWAP, DSYTRD, XERBLA
177 *     ..
178 *     .. Intrinsic Functions ..
179       INTRINSIC          MAXMINSQRT
180 *     ..
181 *     .. Executable Statements ..
182 *
183 *     Test the input parameters.
184 *
185       LOWER = LSAME( UPLO, 'L' )
186       WANTZ = LSAME( JOBZ, 'V' )
187       ALLEIG = LSAME( RANGE'A' )
188       VALEIG = LSAME( RANGE'V' )
189       INDEIG = LSAME( RANGE'I' )
190       LQUERY = ( LWORK.EQ.-1 )
191 *
192       INFO = 0
193       IF.NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
194          INFO = -1
195       ELSE IF.NOT.( ALLEIG .OR. VALEIG .OR. INDEIG ) ) THEN
196          INFO = -2
197       ELSE IF.NOT.( LOWER .OR. LSAME( UPLO, 'U' ) ) ) THEN
198          INFO = -3
199       ELSE IF( N.LT.0 ) THEN
200          INFO = -4
201       ELSE IF( LDA.LT.MAX1, N ) ) THEN
202          INFO = -6
203       ELSE
204          IF( VALEIG ) THEN
205             IF( N.GT.0 .AND. VU.LE.VL )
206      $         INFO = -8
207          ELSE IF( INDEIG ) THEN
208             IF( IL.LT.1 .OR. IL.GT.MAX1, N ) ) THEN
209                INFO = -9
210             ELSE IF( IU.LT.MIN( N, IL ) .OR. IU.GT.N ) THEN
211                INFO = -10
212             END IF
213          END IF
214       END IF
215       IF( INFO.EQ.0 ) THEN
216          IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
217             INFO = -15
218          END IF
219       END IF
220 *
221       IF( INFO.EQ.0 ) THEN
222          IF( N.LE.1 ) THEN
223             LWKMIN = 1
224             WORK( 1 ) = LWKMIN
225          ELSE
226             LWKMIN = 8*N
227             NB = ILAENV( 1'DSYTRD', UPLO, N, -1-1-1 )
228             NB = MAX( NB, ILAENV( 1'DORMTR', UPLO, N, -1-1-1 ) )
229             LWKOPT = MAX( LWKMIN, ( NB + 3 )*N )
230             WORK( 1 ) = LWKOPT
231          END IF
232 *
233          IF( LWORK.LT.LWKMIN .AND. .NOT.LQUERY )
234      $      INFO = -17
235       END IF
236 *
237       IF( INFO.NE.0 ) THEN
238          CALL XERBLA( 'DSYEVX'-INFO )
239          RETURN
240       ELSE IF( LQUERY ) THEN
241          RETURN
242       END IF
243 *
244 *     Quick return if possible
245 *
246       M = 0
247       IF( N.EQ.0 ) THEN
248          RETURN
249       END IF
250 *
251       IF( N.EQ.1 ) THEN
252          IF( ALLEIG .OR. INDEIG ) THEN
253             M = 1
254             W( 1 ) = A( 11 )
255          ELSE
256             IF( VL.LT.A( 11 ) .AND. VU.GE.A( 11 ) ) THEN
257                M = 1
258                W( 1 ) = A( 11 )
259             END IF
260          END IF
261          IF( WANTZ )
262      $      Z( 11 ) = ONE
263          RETURN
264       END IF
265 *
266 *     Get machine constants.
267 *
268       SAFMIN = DLAMCH( 'Safe minimum' )
269       EPS = DLAMCH( 'Precision' )
270       SMLNUM = SAFMIN / EPS
271       BIGNUM = ONE / SMLNUM
272       RMIN = SQRT( SMLNUM )
273       RMAX = MINSQRT( BIGNUM ), ONE / SQRTSQRT( SAFMIN ) ) )
274 *
275 *     Scale matrix to allowable range, if necessary.
276 *
277       ISCALE = 0
278       ABSTLL = ABSTOL
279       IF( VALEIG ) THEN
280          VLL = VL
281          VUU = VU
282       END IF
283       ANRM = DLANSY( 'M', UPLO, N, A, LDA, WORK )
284       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
285          ISCALE = 1
286          SIGMA = RMIN / ANRM
287       ELSE IF( ANRM.GT.RMAX ) THEN
288          ISCALE = 1
289          SIGMA = RMAX / ANRM
290       END IF
291       IF( ISCALE.EQ.1 ) THEN
292          IF( LOWER ) THEN
293             DO 10 J = 1, N
294                CALL DSCAL( N-J+1, SIGMA, A( J, J ), 1 )
295    10       CONTINUE
296          ELSE
297             DO 20 J = 1, N
298                CALL DSCAL( J, SIGMA, A( 1, J ), 1 )
299    20       CONTINUE
300          END IF
301          IF( ABSTOL.GT.0 )
302      $      ABSTLL = ABSTOL*SIGMA
303          IF( VALEIG ) THEN
304             VLL = VL*SIGMA
305             VUU = VU*SIGMA
306          END IF
307       END IF
308 *
309 *     Call DSYTRD to reduce symmetric matrix to tridiagonal form.
310 *
311       INDTAU = 1
312       INDE = INDTAU + N
313       INDD = INDE + N
314       INDWRK = INDD + N
315       LLWORK = LWORK - INDWRK + 1
316       CALL DSYTRD( UPLO, N, A, LDA, WORK( INDD ), WORK( INDE ),
317      $             WORK( INDTAU ), WORK( INDWRK ), LLWORK, IINFO )
318 *
319 *     If all eigenvalues are desired and ABSTOL is less than or equal to
320 *     zero, then call DSTERF or DORGTR and SSTEQR.  If this fails for
321 *     some eigenvalue, then try DSTEBZ.
322 *
323       TEST = .FALSE.
324       IF( INDEIG ) THEN
325          IF( IL.EQ.1 .AND. IU.EQ.N ) THEN
326             TEST = .TRUE.
327          END IF
328       END IF
329       IF( ( ALLEIG .OR. TEST ) .AND. ( ABSTOL.LE.ZERO ) ) THEN
330          CALL DCOPY( N, WORK( INDD ), 1, W, 1 )
331          INDEE = INDWRK + 2*N
332          IF.NOT.WANTZ ) THEN
333             CALL DCOPY( N-1, WORK( INDE ), 1, WORK( INDEE ), 1 )
334             CALL DSTERF( N, W, WORK( INDEE ), INFO )
335          ELSE
336             CALL DLACPY( 'A', N, N, A, LDA, Z, LDZ )
337             CALL DORGTR( UPLO, N, Z, LDZ, WORK( INDTAU ),
338      $                   WORK( INDWRK ), LLWORK, IINFO )
339             CALL DCOPY( N-1, WORK( INDE ), 1, WORK( INDEE ), 1 )
340             CALL DSTEQR( JOBZ, N, W, WORK( INDEE ), Z, LDZ,
341      $                   WORK( INDWRK ), INFO )
342             IF( INFO.EQ.0 ) THEN
343                DO 30 I = 1, N
344                   IFAIL( I ) = 0
345    30          CONTINUE
346             END IF
347          END IF
348          IF( INFO.EQ.0 ) THEN
349             M = N
350             GO TO 40
351          END IF
352          INFO = 0
353       END IF
354 *
355 *     Otherwise, call DSTEBZ and, if eigenvectors are desired, SSTEIN.
356 *
357       IF( WANTZ ) THEN
358          ORDER = 'B'
359       ELSE
360          ORDER = 'E'
361       END IF
362       INDIBL = 1
363       INDISP = INDIBL + N
364       INDIWO = INDISP + N
365       CALL DSTEBZ( RANGE, ORDER, N, VLL, VUU, IL, IU, ABSTLL,
366      $             WORK( INDD ), WORK( INDE ), M, NSPLIT, W,
367      $             IWORK( INDIBL ), IWORK( INDISP ), WORK( INDWRK ),
368      $             IWORK( INDIWO ), INFO )
369 *
370       IF( WANTZ ) THEN
371          CALL DSTEIN( N, WORK( INDD ), WORK( INDE ), M, W,
372      $                IWORK( INDIBL ), IWORK( INDISP ), Z, LDZ,
373      $                WORK( INDWRK ), IWORK( INDIWO ), IFAIL, INFO )
374 *
375 *        Apply orthogonal matrix used in reduction to tridiagonal
376 *        form to eigenvectors returned by DSTEIN.
377 *
378          INDWKN = INDE
379          LLWRKN = LWORK - INDWKN + 1
380          CALL DORMTR( 'L', UPLO, 'N', N, M, A, LDA, WORK( INDTAU ), Z,
381      $                LDZ, WORK( INDWKN ), LLWRKN, IINFO )
382       END IF
383 *
384 *     If matrix was scaled, then rescale eigenvalues appropriately.
385 *
386    40 CONTINUE
387       IF( ISCALE.EQ.1 ) THEN
388          IF( INFO.EQ.0 ) THEN
389             IMAX = M
390          ELSE
391             IMAX = INFO - 1
392          END IF
393          CALL DSCAL( IMAX, ONE / SIGMA, W, 1 )
394       END IF
395 *
396 *     If eigenvalues are not in order, then sort them, along with
397 *     eigenvectors.
398 *
399       IF( WANTZ ) THEN
400          DO 60 J = 1, M - 1
401             I = 0
402             TMP1 = W( J )
403             DO 50 JJ = J + 1, M
404                IF( W( JJ ).LT.TMP1 ) THEN
405                   I = JJ
406                   TMP1 = W( JJ )
407                END IF
408    50       CONTINUE
409 *
410             IF( I.NE.0 ) THEN
411                ITMP1 = IWORK( INDIBL+I-1 )
412                W( I ) = W( J )
413                IWORK( INDIBL+I-1 ) = IWORK( INDIBL+J-1 )
414                W( J ) = TMP1
415                IWORK( INDIBL+J-1 ) = ITMP1
416                CALL DSWAP( N, Z( 1, I ), 1, Z( 1, J ), 1 )
417                IF( INFO.NE.0 ) THEN
418                   ITMP1 = IFAIL( I )
419                   IFAIL( I ) = IFAIL( J )
420                   IFAIL( J ) = ITMP1
421                END IF
422             END IF
423    60    CONTINUE
424       END IF
425 *
426 *     Set WORK(1) to optimal workspace size.
427 *
428       WORK( 1 ) = LWKOPT
429 *
430       RETURN
431 *
432 *     End of DSYEVX
433 *
434       END