001:       SUBROUTINE SSYEVD( JOBZ, UPLO, N, A, LDA, W, WORK, LWORK, IWORK,
002:      $                   LIWORK, 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, UPLO
010:       INTEGER            INFO, LDA, LIWORK, LWORK, N
011: *     ..
012: *     .. Array Arguments ..
013:       INTEGER            IWORK( * )
014:       REAL               A( LDA, * ), W( * ), WORK( * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  SSYEVD computes all eigenvalues and, optionally, eigenvectors of a
021: *  real symmetric matrix A. If eigenvectors are desired, it uses a
022: *  divide and conquer algorithm.
023: *
024: *  The divide and conquer algorithm makes very mild assumptions about
025: *  floating point arithmetic. It will work on machines with a guard
026: *  digit in add/subtract, or on those binary machines without guard
027: *  digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or
028: *  Cray-2. It could conceivably fail on hexadecimal or decimal machines
029: *  without guard digits, but we know of none.
030: *
031: *  Because of large use of BLAS of level 3, SSYEVD needs N**2 more
032: *  workspace than SSYEVX.
033: *
034: *  Arguments
035: *  =========
036: *
037: *  JOBZ    (input) CHARACTER*1
038: *          = 'N':  Compute eigenvalues only;
039: *          = 'V':  Compute eigenvalues and eigenvectors.
040: *
041: *  UPLO    (input) CHARACTER*1
042: *          = 'U':  Upper triangle of A is stored;
043: *          = 'L':  Lower triangle of A is stored.
044: *
045: *  N       (input) INTEGER
046: *          The order of the matrix A.  N >= 0.
047: *
048: *  A       (input/output) REAL array, dimension (LDA, N)
049: *          On entry, the symmetric matrix A.  If UPLO = 'U', the
050: *          leading N-by-N upper triangular part of A contains the
051: *          upper triangular part of the matrix A.  If UPLO = 'L',
052: *          the leading N-by-N lower triangular part of A contains
053: *          the lower triangular part of the matrix A.
054: *          On exit, if JOBZ = 'V', then if INFO = 0, A contains the
055: *          orthonormal eigenvectors of the matrix A.
056: *          If JOBZ = 'N', then on exit the lower triangle (if UPLO='L')
057: *          or the upper triangle (if UPLO='U') of A, including the
058: *          diagonal, is destroyed.
059: *
060: *  LDA     (input) INTEGER
061: *          The leading dimension of the array A.  LDA >= max(1,N).
062: *
063: *  W       (output) REAL array, dimension (N)
064: *          If INFO = 0, the eigenvalues in ascending order.
065: *
066: *  WORK    (workspace/output) REAL array,
067: *                                         dimension (LWORK)
068: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
069: *
070: *  LWORK   (input) INTEGER
071: *          The dimension of the array WORK.
072: *          If N <= 1,               LWORK must be at least 1.
073: *          If JOBZ = 'N' and N > 1, LWORK must be at least 2*N+1.
074: *          If JOBZ = 'V' and N > 1, LWORK must be at least 
075: *                                                1 + 6*N + 2*N**2.
076: *
077: *          If LWORK = -1, then a workspace query is assumed; the routine
078: *          only calculates the optimal sizes of the WORK and IWORK
079: *          arrays, returns these values as the first entries of the WORK
080: *          and IWORK arrays, and no error message related to LWORK or
081: *          LIWORK is issued by XERBLA.
082: *
083: *  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK))
084: *          On exit, if INFO = 0, IWORK(1) returns the optimal LIWORK.
085: *
086: *  LIWORK  (input) INTEGER
087: *          The dimension of the array IWORK.
088: *          If N <= 1,                LIWORK must be at least 1.
089: *          If JOBZ  = 'N' and N > 1, LIWORK must be at least 1.
090: *          If JOBZ  = 'V' and N > 1, LIWORK must be at least 3 + 5*N.
091: *
092: *          If LIWORK = -1, then a workspace query is assumed; the
093: *          routine only calculates the optimal sizes of the WORK and
094: *          IWORK arrays, returns these values as the first entries of
095: *          the WORK and IWORK arrays, and no error message related to
096: *          LWORK or LIWORK is issued by XERBLA.
097: *
098: *  INFO    (output) INTEGER
099: *          = 0:  successful exit
100: *          < 0:  if INFO = -i, the i-th argument had an illegal value
101: *          > 0:  if INFO = i and JOBZ = 'N', then the algorithm failed
102: *                to converge; i off-diagonal elements of an intermediate
103: *                tridiagonal form did not converge to zero;
104: *                if INFO = i and JOBZ = 'V', then the algorithm failed
105: *                to compute an eigenvalue while working on the submatrix
106: *                lying in rows and columns INFO/(N+1) through
107: *                mod(INFO,N+1).
108: *
109: *  Further Details
110: *  ===============
111: *
112: *  Based on contributions by
113: *     Jeff Rutter, Computer Science Division, University of California
114: *     at Berkeley, USA
115: *  Modified by Francoise Tisseur, University of Tennessee.
116: *
117: *  Modified description of INFO. Sven, 16 Feb 05.
118: *  =====================================================================
119: *
120: *     .. Parameters ..
121:       REAL               ZERO, ONE
122:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
123: *     ..
124: *     .. Local Scalars ..
125: *
126:       LOGICAL            LOWER, LQUERY, WANTZ
127:       INTEGER            IINFO, INDE, INDTAU, INDWK2, INDWRK, ISCALE,
128:      $                   LIOPT, LIWMIN, LLWORK, LLWRK2, LOPT, LWMIN
129:       REAL               ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA,
130:      $                   SMLNUM
131: *     ..
132: *     .. External Functions ..
133:       LOGICAL            LSAME
134:       INTEGER            ILAENV
135:       REAL               SLAMCH, SLANSY
136:       EXTERNAL           ILAENV, LSAME, SLAMCH, SLANSY
137: *     ..
138: *     .. External Subroutines ..
139:       EXTERNAL           SLACPY, SLASCL, SORMTR, SSCAL, SSTEDC, SSTERF,
140:      $                   SSYTRD, XERBLA
141: *     ..
142: *     .. Intrinsic Functions ..
143:       INTRINSIC          MAX, SQRT
144: *     ..
145: *     .. Executable Statements ..
146: *
147: *     Test the input parameters.
148: *
149:       WANTZ = LSAME( JOBZ, 'V' )
150:       LOWER = LSAME( UPLO, 'L' )
151:       LQUERY = ( LWORK.EQ.-1 .OR. LIWORK.EQ.-1 )
152: *
153:       INFO = 0
154:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
155:          INFO = -1
156:       ELSE IF( .NOT.( LOWER .OR. LSAME( UPLO, 'U' ) ) ) THEN
157:          INFO = -2
158:       ELSE IF( N.LT.0 ) THEN
159:          INFO = -3
160:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
161:          INFO = -5
162:       END IF
163: *
164:       IF( INFO.EQ.0 ) THEN
165:          IF( N.LE.1 ) THEN
166:             LIWMIN = 1
167:             LWMIN = 1
168:             LOPT = LWMIN
169:             LIOPT = LIWMIN
170:          ELSE
171:             IF( WANTZ ) THEN
172:                LIWMIN = 3 + 5*N
173:                LWMIN = 1 + 6*N + 2*N**2
174:             ELSE
175:                LIWMIN = 1
176:                LWMIN = 2*N + 1
177:             END IF
178:             LOPT = MAX( LWMIN, 2*N +
179:      $                  ILAENV( 1, 'SSYTRD', UPLO, N, -1, -1, -1 ) )
180:             LIOPT = LIWMIN
181:          END IF
182:          WORK( 1 ) = LOPT
183:          IWORK( 1 ) = LIOPT
184: *
185:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
186:             INFO = -8
187:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
188:             INFO = -10
189:          END IF
190:       END IF
191: *
192:       IF( INFO.NE.0 ) THEN
193:          CALL XERBLA( 'SSYEVD', -INFO )
194:          RETURN
195:       ELSE IF( LQUERY ) THEN
196:          RETURN 
197:       END IF
198: *
199: *     Quick return if possible
200: *
201:       IF( N.EQ.0 )
202:      $   RETURN
203: *
204:       IF( N.EQ.1 ) THEN
205:          W( 1 ) = A( 1, 1 )
206:          IF( WANTZ )
207:      $      A( 1, 1 ) = ONE
208:          RETURN 
209:       END IF
210: *
211: *     Get machine constants.
212: *
213:       SAFMIN = SLAMCH( 'Safe minimum' )
214:       EPS = SLAMCH( 'Precision' )
215:       SMLNUM = SAFMIN / EPS
216:       BIGNUM = ONE / SMLNUM
217:       RMIN = SQRT( SMLNUM )
218:       RMAX = SQRT( BIGNUM )
219: *
220: *     Scale matrix to allowable range, if necessary.
221: *
222:       ANRM = SLANSY( 'M', UPLO, N, A, LDA, WORK )
223:       ISCALE = 0
224:       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
225:          ISCALE = 1
226:          SIGMA = RMIN / ANRM
227:       ELSE IF( ANRM.GT.RMAX ) THEN
228:          ISCALE = 1
229:          SIGMA = RMAX / ANRM
230:       END IF
231:       IF( ISCALE.EQ.1 )
232:      $   CALL SLASCL( UPLO, 0, 0, ONE, SIGMA, N, N, A, LDA, INFO )
233: *
234: *     Call SSYTRD to reduce symmetric matrix to tridiagonal form.
235: *
236:       INDE = 1
237:       INDTAU = INDE + N
238:       INDWRK = INDTAU + N
239:       LLWORK = LWORK - INDWRK + 1
240:       INDWK2 = INDWRK + N*N
241:       LLWRK2 = LWORK - INDWK2 + 1
242: *
243:       CALL SSYTRD( UPLO, N, A, LDA, W, WORK( INDE ), WORK( INDTAU ),
244:      $             WORK( INDWRK ), LLWORK, IINFO )
245: *
246: *     For eigenvalues only, call SSTERF.  For eigenvectors, first call
247: *     SSTEDC to generate the eigenvector matrix, WORK(INDWRK), of the
248: *     tridiagonal matrix, then call SORMTR to multiply it by the
249: *     Householder transformations stored in A.
250: *
251:       IF( .NOT.WANTZ ) THEN
252:          CALL SSTERF( N, W, WORK( INDE ), INFO )
253:       ELSE
254:          CALL SSTEDC( 'I', N, W, WORK( INDE ), WORK( INDWRK ), N,
255:      $                WORK( INDWK2 ), LLWRK2, IWORK, LIWORK, INFO )
256:          CALL SORMTR( 'L', UPLO, 'N', N, N, A, LDA, WORK( INDTAU ),
257:      $                WORK( INDWRK ), N, WORK( INDWK2 ), LLWRK2, IINFO )
258:          CALL SLACPY( 'A', N, N, WORK( INDWRK ), N, A, LDA )
259:       END IF
260: *
261: *     If matrix was scaled, then rescale eigenvalues appropriately.
262: *
263:       IF( ISCALE.EQ.1 )
264:      $   CALL SSCAL( N, ONE / SIGMA, W, 1 )
265: *
266:       WORK( 1 ) = LOPT
267:       IWORK( 1 ) = LIOPT
268: *
269:       RETURN
270: *
271: *     End of SSYEVD
272: *
273:       END
274: