001:       SUBROUTINE SSPEVD( JOBZ, UPLO, N, AP, W, Z, LDZ, WORK, LWORK,
002:      $                   IWORK, LIWORK, INFO )
003: *
004: *  -- LAPACK driver routine (version 3.2) --
005: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
006: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       CHARACTER          JOBZ, UPLO
011:       INTEGER            INFO, LDZ, LIWORK, LWORK, N
012: *     ..
013: *     .. Array Arguments ..
014:       INTEGER            IWORK( * )
015:       REAL               AP( * ), W( * ), WORK( * ), Z( LDZ, * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  SSPEVD computes all the eigenvalues and, optionally, eigenvectors
022: *  of a real symmetric matrix A in packed storage. If eigenvectors are
023: *  desired, it uses a divide and conquer algorithm.
024: *
025: *  The divide and conquer algorithm makes very mild assumptions about
026: *  floating point arithmetic. It will work on machines with a guard
027: *  digit in add/subtract, or on those binary machines without guard
028: *  digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or
029: *  Cray-2. It could conceivably fail on hexadecimal or decimal machines
030: *  without guard digits, but we know of none.
031: *
032: *  Arguments
033: *  =========
034: *
035: *  JOBZ    (input) CHARACTER*1
036: *          = 'N':  Compute eigenvalues only;
037: *          = 'V':  Compute eigenvalues and eigenvectors.
038: *
039: *  UPLO    (input) CHARACTER*1
040: *          = 'U':  Upper triangle of A is stored;
041: *          = 'L':  Lower triangle of A is stored.
042: *
043: *  N       (input) INTEGER
044: *          The order of the matrix A.  N >= 0.
045: *
046: *  AP      (input/output) REAL array, dimension (N*(N+1)/2)
047: *          On entry, the upper or lower triangle of the symmetric matrix
048: *          A, packed columnwise in a linear array.  The j-th column of A
049: *          is stored in the array AP as follows:
050: *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
051: *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
052: *
053: *          On exit, AP is overwritten by values generated during the
054: *          reduction to tridiagonal form.  If UPLO = 'U', the diagonal
055: *          and first superdiagonal of the tridiagonal matrix T overwrite
056: *          the corresponding elements of A, and if UPLO = 'L', the
057: *          diagonal and first subdiagonal of T overwrite the
058: *          corresponding elements of A.
059: *
060: *  W       (output) REAL array, dimension (N)
061: *          If INFO = 0, the eigenvalues in ascending order.
062: *
063: *  Z       (output) REAL array, dimension (LDZ, N)
064: *          If JOBZ = 'V', then if INFO = 0, Z contains the orthonormal
065: *          eigenvectors of the matrix A, with the i-th column of Z
066: *          holding the eigenvector associated with W(i).
067: *          If JOBZ = 'N', then Z is not referenced.
068: *
069: *  LDZ     (input) INTEGER
070: *          The leading dimension of the array Z.  LDZ >= 1, and if
071: *          JOBZ = 'V', LDZ >= max(1,N).
072: *
073: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
074: *          On exit, if INFO = 0, WORK(1) returns the required LWORK.
075: *
076: *  LWORK   (input) INTEGER
077: *          The dimension of the array WORK.
078: *          If N <= 1,               LWORK must be at least 1.
079: *          If JOBZ = 'N' and N > 1, LWORK must be at least 2*N.
080: *          If JOBZ = 'V' and N > 1, LWORK must be at least
081: *                                                 1 + 6*N + N**2.
082: *
083: *          If LWORK = -1, then a workspace query is assumed; the routine
084: *          only calculates the required sizes of the WORK and IWORK
085: *          arrays, returns these values as the first entries of the WORK
086: *          and IWORK arrays, and no error message related to LWORK or
087: *          LIWORK is issued by XERBLA.
088: *
089: *  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK))
090: *          On exit, if INFO = 0, IWORK(1) returns the required LIWORK.
091: *
092: *  LIWORK  (input) INTEGER
093: *          The dimension of the array IWORK.
094: *          If JOBZ  = 'N' or N <= 1, LIWORK must be at least 1.
095: *          If JOBZ  = 'V' and N > 1, LIWORK must be at least 3 + 5*N.
096: *
097: *          If LIWORK = -1, then a workspace query is assumed; the
098: *          routine only calculates the required sizes of the WORK and
099: *          IWORK arrays, returns these values as the first entries of
100: *          the WORK and IWORK arrays, and no error message related to
101: *          LWORK or LIWORK is issued by XERBLA.
102: *
103: *  INFO    (output) INTEGER
104: *          = 0:  successful exit
105: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
106: *          > 0:  if INFO = i, the algorithm failed to converge; i
107: *                off-diagonal elements of an intermediate tridiagonal
108: *                form did not converge to zero.
109: *
110: *  =====================================================================
111: *
112: *     .. Parameters ..
113:       REAL               ZERO, ONE
114:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
115: *     ..
116: *     .. Local Scalars ..
117:       LOGICAL            LQUERY, WANTZ
118:       INTEGER            IINFO, INDE, INDTAU, INDWRK, ISCALE, LIWMIN,
119:      $                   LLWORK, LWMIN
120:       REAL               ANRM, BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA,
121:      $                   SMLNUM
122: *     ..
123: *     .. External Functions ..
124:       LOGICAL            LSAME
125:       REAL               SLAMCH, SLANSP
126:       EXTERNAL           LSAME, SLAMCH, SLANSP
127: *     ..
128: *     .. External Subroutines ..
129:       EXTERNAL           SOPMTR, SSCAL, SSPTRD, SSTEDC, SSTERF, XERBLA
130: *     ..
131: *     .. Intrinsic Functions ..
132:       INTRINSIC          SQRT
133: *     ..
134: *     .. Executable Statements ..
135: *
136: *     Test the input parameters.
137: *
138:       WANTZ = LSAME( JOBZ, 'V' )
139:       LQUERY = ( LWORK.EQ.-1 .OR. LIWORK.EQ.-1 )
140: *
141:       INFO = 0
142:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
143:          INFO = -1
144:       ELSE IF( .NOT.( LSAME( UPLO, 'U' ) .OR. LSAME( UPLO, 'L' ) ) )
145:      $          THEN
146:          INFO = -2
147:       ELSE IF( N.LT.0 ) THEN
148:          INFO = -3
149:       ELSE IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
150:          INFO = -7
151:       END IF
152: *
153:       IF( INFO.EQ.0 ) THEN
154:          IF( N.LE.1 ) THEN
155:             LIWMIN = 1
156:             LWMIN = 1
157:          ELSE
158:             IF( WANTZ ) THEN
159:                LIWMIN = 3 + 5*N
160:                LWMIN = 1 + 6*N + N**2
161:             ELSE
162:                LIWMIN = 1
163:                LWMIN = 2*N
164:             END IF
165:          END IF
166:          IWORK( 1 ) = LIWMIN
167:          WORK( 1 ) = LWMIN
168: *
169:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
170:             INFO = -9
171:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
172:             INFO = -11
173:          END IF
174:       END IF
175: *
176:       IF( INFO.NE.0 ) THEN
177:          CALL XERBLA( 'SSPEVD', -INFO )
178:          RETURN
179:       ELSE IF( LQUERY ) THEN
180:          RETURN 
181:       END IF
182: *
183: *     Quick return if possible
184: *
185:       IF( N.EQ.0 )
186:      $   RETURN 
187: *
188:       IF( N.EQ.1 ) THEN
189:          W( 1 ) = AP( 1 )
190:          IF( WANTZ )
191:      $      Z( 1, 1 ) = ONE
192:          RETURN 
193:       END IF
194: *
195: *     Get machine constants.
196: *
197:       SAFMIN = SLAMCH( 'Safe minimum' )
198:       EPS = SLAMCH( 'Precision' )
199:       SMLNUM = SAFMIN / EPS
200:       BIGNUM = ONE / SMLNUM
201:       RMIN = SQRT( SMLNUM )
202:       RMAX = SQRT( BIGNUM )
203: *
204: *     Scale matrix to allowable range, if necessary.
205: *
206:       ANRM = SLANSP( 'M', UPLO, N, AP, WORK )
207:       ISCALE = 0
208:       IF( ANRM.GT.ZERO .AND. ANRM.LT.RMIN ) THEN
209:          ISCALE = 1
210:          SIGMA = RMIN / ANRM
211:       ELSE IF( ANRM.GT.RMAX ) THEN
212:          ISCALE = 1
213:          SIGMA = RMAX / ANRM
214:       END IF
215:       IF( ISCALE.EQ.1 ) THEN
216:          CALL SSCAL( ( N*( N+1 ) ) / 2, SIGMA, AP, 1 )
217:       END IF
218: *
219: *     Call SSPTRD to reduce symmetric packed matrix to tridiagonal form.
220: *
221:       INDE = 1
222:       INDTAU = INDE + N
223:       CALL SSPTRD( UPLO, N, AP, W, WORK( INDE ), WORK( INDTAU ), IINFO )
224: *
225: *     For eigenvalues only, call SSTERF.  For eigenvectors, first call
226: *     SSTEDC to generate the eigenvector matrix, WORK(INDWRK), of the
227: *     tridiagonal matrix, then call SOPMTR to multiply it by the
228: *     Householder transformations represented in AP.
229: *
230:       IF( .NOT.WANTZ ) THEN
231:          CALL SSTERF( N, W, WORK( INDE ), INFO )
232:       ELSE
233:          INDWRK = INDTAU + N
234:          LLWORK = LWORK - INDWRK + 1
235:          CALL SSTEDC( 'I', N, W, WORK( INDE ), Z, LDZ, WORK( INDWRK ),
236:      $                LLWORK, IWORK, LIWORK, INFO )
237:          CALL SOPMTR( 'L', UPLO, 'N', N, N, AP, WORK( INDTAU ), Z, LDZ,
238:      $                WORK( INDWRK ), IINFO )
239:       END IF
240: *
241: *     If matrix was scaled, then rescale eigenvalues appropriately.
242: *
243:       IF( ISCALE.EQ.1 )
244:      $   CALL SSCAL( N, ONE / SIGMA, W, 1 )
245: *
246:       WORK( 1 ) = LWMIN
247:       IWORK( 1 ) = LIWMIN
248:       RETURN
249: *
250: *     End of SSPEVD
251: *
252:       END
253: