```001:       SUBROUTINE SSYEV( JOBZ, UPLO, N, A, LDA, W, WORK, LWORK, INFO )
002: *
003: *  -- LAPACK driver routine (version 3.2) --
004: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
005: *     November 2006
006: *
007: *     .. Scalar Arguments ..
008:       CHARACTER          JOBZ, UPLO
009:       INTEGER            INFO, LDA, LWORK, N
010: *     ..
011: *     .. Array Arguments ..
012:       REAL               A( LDA, * ), W( * ), WORK( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  SSYEV computes all eigenvalues and, optionally, eigenvectors of a
019: *  real symmetric matrix A.
020: *
021: *  Arguments
022: *  =========
023: *
024: *  JOBZ    (input) CHARACTER*1
025: *          = 'N':  Compute eigenvalues only;
026: *          = 'V':  Compute eigenvalues and eigenvectors.
027: *
028: *  UPLO    (input) CHARACTER*1
029: *          = 'U':  Upper triangle of A is stored;
030: *          = 'L':  Lower triangle of A is stored.
031: *
032: *  N       (input) INTEGER
033: *          The order of the matrix A.  N >= 0.
034: *
035: *  A       (input/output) REAL array, dimension (LDA, N)
036: *          On entry, the symmetric matrix A.  If UPLO = 'U', the
037: *          leading N-by-N upper triangular part of A contains the
038: *          upper triangular part of the matrix A.  If UPLO = 'L',
039: *          the leading N-by-N lower triangular part of A contains
040: *          the lower triangular part of the matrix A.
041: *          On exit, if JOBZ = 'V', then if INFO = 0, A contains the
042: *          orthonormal eigenvectors of the matrix A.
043: *          If JOBZ = 'N', then on exit the lower triangle (if UPLO='L')
044: *          or the upper triangle (if UPLO='U') of A, including the
045: *          diagonal, is destroyed.
046: *
047: *  LDA     (input) INTEGER
048: *          The leading dimension of the array A.  LDA >= max(1,N).
049: *
050: *  W       (output) REAL array, dimension (N)
051: *          If INFO = 0, the eigenvalues in ascending order.
052: *
053: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
054: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
055: *
056: *  LWORK   (input) INTEGER
057: *          The length of the array WORK.  LWORK >= max(1,3*N-1).
058: *          For optimal efficiency, LWORK >= (NB+2)*N,
059: *          where NB is the blocksize for SSYTRD returned by ILAENV.
060: *
061: *          If LWORK = -1, then a workspace query is assumed; the routine
062: *          only calculates the optimal size of the WORK array, returns
063: *          this value as the first entry of the WORK array, and no error
064: *          message related to LWORK is issued by XERBLA.
065: *
066: *  INFO    (output) INTEGER
067: *          = 0:  successful exit
068: *          < 0:  if INFO = -i, the i-th argument had an illegal value
069: *          > 0:  if INFO = i, the algorithm failed to converge; i
070: *                off-diagonal elements of an intermediate tridiagonal
071: *                form did not converge to zero.
072: *
073: *  =====================================================================
074: *
075: *     .. Parameters ..
076:       REAL               ZERO, ONE
077:       PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0 )
078: *     ..
079: *     .. Local Scalars ..
080:       LOGICAL            LOWER, LQUERY, WANTZ
081:       INTEGER            IINFO, IMAX, INDE, INDTAU, INDWRK, ISCALE,
082:      \$                   LLWORK, LWKOPT, NB
083:       REAL               ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA,
084:      \$                   SMLNUM
085: *     ..
086: *     .. External Functions ..
087:       LOGICAL            LSAME
088:       INTEGER            ILAENV
089:       REAL               SLAMCH, SLANSY
090:       EXTERNAL           ILAENV, LSAME, SLAMCH, SLANSY
091: *     ..
092: *     .. External Subroutines ..
093:       EXTERNAL           SLASCL, SORGTR, SSCAL, SSTEQR, SSTERF, SSYTRD,
094:      \$                   XERBLA
095: *     ..
096: *     .. Intrinsic Functions ..
097:       INTRINSIC          MAX, SQRT
098: *     ..
099: *     .. Executable Statements ..
100: *
101: *     Test the input parameters.
102: *
103:       WANTZ = LSAME( JOBZ, 'V' )
104:       LOWER = LSAME( UPLO, 'L' )
105:       LQUERY = ( LWORK.EQ.-1 )
106: *
107:       INFO = 0
108:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
109:          INFO = -1
110:       ELSE IF( .NOT.( LOWER .OR. LSAME( UPLO, 'U' ) ) ) THEN
111:          INFO = -2
112:       ELSE IF( N.LT.0 ) THEN
113:          INFO = -3
114:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
115:          INFO = -5
116:       END IF
117: *
118:       IF( INFO.EQ.0 ) THEN
119:          NB = ILAENV( 1, 'SSYTRD', UPLO, N, -1, -1, -1 )
120:          LWKOPT = MAX( 1, ( NB+2 )*N )
121:          WORK( 1 ) = LWKOPT
122: *
123:          IF( LWORK.LT.MAX( 1, 3*N-1 ) .AND. .NOT.LQUERY )
124:      \$      INFO = -8
125:       END IF
126: *
127:       IF( INFO.NE.0 ) THEN
128:          CALL XERBLA( 'SSYEV ', -INFO )
129:          RETURN
130:       ELSE IF( LQUERY ) THEN
131:          RETURN
132:       END IF
133: *
134: *     Quick return if possible
135: *
136:       IF( N.EQ.0 ) THEN
137:          RETURN
138:       END IF
139: *
140:       IF( N.EQ.1 ) THEN
141:          W( 1 ) = A( 1, 1 )
142:          WORK( 1 ) = 2
143:          IF( WANTZ )
144:      \$      A( 1, 1 ) = ONE
145:          RETURN
146:       END IF
147: *
148: *     Get machine constants.
149: *
150:       SAFMIN = SLAMCH( 'Safe minimum' )
151:       EPS = SLAMCH( 'Precision' )
152:       SMLNUM = SAFMIN / EPS
153:       BIGNUM = ONE / SMLNUM
154:       RMIN = SQRT( SMLNUM )
155:       RMAX = SQRT( BIGNUM )
156: *
157: *     Scale matrix to allowable range, if necessary.
158: *
159:       ANRM = SLANSY( 'M', UPLO, N, A, LDA, WORK )
160:       ISCALE = 0
161:       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
162:          ISCALE = 1
163:          SIGMA = RMIN / ANRM
164:       ELSE IF( ANRM.GT.RMAX ) THEN
165:          ISCALE = 1
166:          SIGMA = RMAX / ANRM
167:       END IF
168:       IF( ISCALE.EQ.1 )
169:      \$   CALL SLASCL( UPLO, 0, 0, ONE, SIGMA, N, N, A, LDA, INFO )
170: *
171: *     Call SSYTRD to reduce symmetric matrix to tridiagonal form.
172: *
173:       INDE = 1
174:       INDTAU = INDE + N
175:       INDWRK = INDTAU + N
176:       LLWORK = LWORK - INDWRK + 1
177:       CALL SSYTRD( UPLO, N, A, LDA, W, WORK( INDE ), WORK( INDTAU ),
178:      \$             WORK( INDWRK ), LLWORK, IINFO )
179: *
180: *     For eigenvalues only, call SSTERF.  For eigenvectors, first call
181: *     SORGTR to generate the orthogonal matrix, then call SSTEQR.
182: *
183:       IF( .NOT.WANTZ ) THEN
184:          CALL SSTERF( N, W, WORK( INDE ), INFO )
185:       ELSE
186:          CALL SORGTR( UPLO, N, A, LDA, WORK( INDTAU ), WORK( INDWRK ),
187:      \$                LLWORK, IINFO )
188:          CALL SSTEQR( JOBZ, N, W, WORK( INDE ), A, LDA, WORK( INDTAU ),
189:      \$                INFO )
190:       END IF
191: *
192: *     If matrix was scaled, then rescale eigenvalues appropriately.
193: *
194:       IF( ISCALE.EQ.1 ) THEN
195:          IF( INFO.EQ.0 ) THEN
196:             IMAX = N
197:          ELSE
198:             IMAX = INFO - 1
199:          END IF
200:          CALL SSCAL( IMAX, ONE / SIGMA, W, 1 )
201:       END IF
202: *
203: *     Set WORK(1) to optimal workspace size.
204: *
205:       WORK( 1 ) = LWKOPT
206: *
207:       RETURN
208: *
209: *     End of SSYEV
210: *
211:       END
212: ```