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