001:       SUBROUTINE SSTEVD( JOBZ, N, D, E, Z, LDZ, 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
010:       INTEGER            INFO, LDZ, LIWORK, LWORK, N
011: *     ..
012: *     .. Array Arguments ..
013:       INTEGER            IWORK( * )
014:       REAL               D( * ), E( * ), WORK( * ), Z( LDZ, * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  SSTEVD computes all eigenvalues and, optionally, eigenvectors of a
021: *  real symmetric tridiagonal matrix. If eigenvectors are desired, it
022: *  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: *  N       (input) INTEGER
039: *          The order of the matrix.  N >= 0.
040: *
041: *  D       (input/output) REAL array, dimension (N)
042: *          On entry, the n diagonal elements of the tridiagonal matrix
043: *          A.
044: *          On exit, if INFO = 0, the eigenvalues in ascending order.
045: *
046: *  E       (input/output) REAL array, dimension (N-1)
047: *          On entry, the (n-1) subdiagonal elements of the tridiagonal
048: *          matrix A, stored in elements 1 to N-1 of E.
049: *          On exit, the contents of E are destroyed.
050: *
051: *  Z       (output) REAL array, dimension (LDZ, N)
052: *          If JOBZ = 'V', then if INFO = 0, Z contains the orthonormal
053: *          eigenvectors of the matrix A, with the i-th column of Z
054: *          holding the eigenvector associated with D(i).
055: *          If JOBZ = 'N', then Z is not referenced.
056: *
057: *  LDZ     (input) INTEGER
058: *          The leading dimension of the array Z.  LDZ >= 1, and if
059: *          JOBZ = 'V', LDZ >= max(1,N).
060: *
061: *  WORK    (workspace/output) REAL array,
062: *                                         dimension (LWORK)
063: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
064: *
065: *  LWORK   (input) INTEGER
066: *          The dimension of the array WORK.
067: *          If JOBZ  = 'N' or N <= 1 then LWORK must be at least 1.
068: *          If JOBZ  = 'V' and N > 1 then LWORK must be at least
069: *                         ( 1 + 4*N + N**2 ).
070: *
071: *          If LWORK = -1, then a workspace query is assumed; the routine
072: *          only calculates the optimal sizes of the WORK and IWORK
073: *          arrays, returns these values as the first entries of the WORK
074: *          and IWORK arrays, and no error message related to LWORK or
075: *          LIWORK is issued by XERBLA.
076: *
077: *  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK))
078: *          On exit, if INFO = 0, IWORK(1) returns the optimal LIWORK.
079: *
080: *  LIWORK  (input) INTEGER
081: *          The dimension of the array IWORK.
082: *          If JOBZ  = 'N' or N <= 1 then LIWORK must be at least 1.
083: *          If JOBZ  = 'V' and N > 1 then LIWORK must be at least 3+5*N.
084: *
085: *          If LIWORK = -1, then a workspace query is assumed; the
086: *          routine only calculates the optimal sizes of the WORK and
087: *          IWORK arrays, returns these values as the first entries of
088: *          the WORK and IWORK arrays, and no error message related to
089: *          LWORK or LIWORK is issued by XERBLA.
090: *
091: *  INFO    (output) INTEGER
092: *          = 0:  successful exit
093: *          < 0:  if INFO = -i, the i-th argument had an illegal value
094: *          > 0:  if INFO = i, the algorithm failed to converge; i
095: *                off-diagonal elements of E did not converge to zero.
096: *
097: *  =====================================================================
098: *
099: *     .. Parameters ..
100:       REAL               ZERO, ONE
101:       PARAMETER          ( ZERO = 0.0E0, ONE = 1.0E0 )
102: *     ..
103: *     .. Local Scalars ..
104:       LOGICAL            LQUERY, WANTZ
105:       INTEGER            ISCALE, LIWMIN, LWMIN
106:       REAL               BIGNUM, EPS, RMAX, RMIN, SAFMIN, SIGMA, SMLNUM,
107:      $                   TNRM
108: *     ..
109: *     .. External Functions ..
110:       LOGICAL            LSAME
111:       REAL               SLAMCH, SLANST
112:       EXTERNAL           LSAME, SLAMCH, SLANST
113: *     ..
114: *     .. External Subroutines ..
115:       EXTERNAL           SSCAL, SSTEDC, SSTERF, XERBLA
116: *     ..
117: *     .. Intrinsic Functions ..
118:       INTRINSIC          SQRT
119: *     ..
120: *     .. Executable Statements ..
121: *
122: *     Test the input parameters.
123: *
124:       WANTZ = LSAME( JOBZ, 'V' )
125:       LQUERY = ( LWORK.EQ.-1 .OR. LIWORK.EQ.-1 )
126: *
127:       INFO = 0
128:       LIWMIN = 1
129:       LWMIN = 1
130:       IF( N.GT.1 .AND. WANTZ ) THEN
131:          LWMIN = 1 + 4*N + N**2
132:          LIWMIN = 3 + 5*N
133:       END IF
134: *
135:       IF( .NOT.( WANTZ .OR. LSAME( JOBZ, 'N' ) ) ) THEN
136:          INFO = -1
137:       ELSE IF( N.LT.0 ) THEN
138:          INFO = -2
139:       ELSE IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
140:          INFO = -6
141:       END IF
142: *
143:       IF( INFO.EQ.0 ) THEN
144:          WORK( 1 ) = LWMIN
145:          IWORK( 1 ) = LIWMIN
146: *
147:          IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
148:             INFO = -8
149:          ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
150:             INFO = -10
151:          END IF
152:       END IF
153: *
154:       IF( INFO.NE.0 ) THEN
155:          CALL XERBLA( 'SSTEVD', -INFO )
156:          RETURN 
157:       ELSE IF( LQUERY ) THEN
158:          RETURN
159:       END IF
160: *
161: *     Quick return if possible
162: *
163:       IF( N.EQ.0 )
164:      $   RETURN 
165: *
166:       IF( N.EQ.1 ) THEN
167:          IF( WANTZ )
168:      $      Z( 1, 1 ) = ONE
169:          RETURN 
170:       END IF
171: *
172: *     Get machine constants.
173: *
174:       SAFMIN = SLAMCH( 'Safe minimum' )
175:       EPS = SLAMCH( 'Precision' )
176:       SMLNUM = SAFMIN / EPS
177:       BIGNUM = ONE / SMLNUM
178:       RMIN = SQRT( SMLNUM )
179:       RMAX = SQRT( BIGNUM )
180: *
181: *     Scale matrix to allowable range, if necessary.
182: *
183:       ISCALE = 0
184:       TNRM = SLANST( 'M', N, D, E )
185:       IF( TNRM.GT.ZERO .AND. TNRM.LT.RMIN ) THEN
186:          ISCALE = 1
187:          SIGMA = RMIN / TNRM
188:       ELSE IF( TNRM.GT.RMAX ) THEN
189:          ISCALE = 1
190:          SIGMA = RMAX / TNRM
191:       END IF
192:       IF( ISCALE.EQ.1 ) THEN
193:          CALL SSCAL( N, SIGMA, D, 1 )
194:          CALL SSCAL( N-1, SIGMA, E( 1 ), 1 )
195:       END IF
196: *
197: *     For eigenvalues only, call SSTERF.  For eigenvalues and
198: *     eigenvectors, call SSTEDC.
199: *
200:       IF( .NOT.WANTZ ) THEN
201:          CALL SSTERF( N, D, E, INFO )
202:       ELSE
203:          CALL SSTEDC( 'I', N, D, E, Z, LDZ, WORK, LWORK, IWORK, LIWORK,
204:      $                INFO )
205:       END IF
206: *
207: *     If matrix was scaled, then rescale eigenvalues appropriately.
208: *
209:       IF( ISCALE.EQ.1 )
210:      $   CALL SSCAL( N, ONE / SIGMA, D, 1 )
211: *
212:       WORK( 1 ) = LWMIN
213:       IWORK( 1 ) = LIWMIN
214: *
215:       RETURN
216: *
217: *     End of SSTEVD
218: *
219:       END
220: