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