001:       SUBROUTINE CLAED8( K, N, QSIZ, Q, LDQ, D, RHO, CUTPNT, Z, DLAMDA,
002:      $                   Q2, LDQ2, W, INDXP, INDX, INDXQ, PERM, GIVPTR,
003:      $                   GIVCOL, GIVNUM, INFO )
004: *
005: *  -- LAPACK routine (version 3.2) --
006: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       INTEGER            CUTPNT, GIVPTR, INFO, K, LDQ, LDQ2, N, QSIZ
011:       REAL               RHO
012: *     ..
013: *     .. Array Arguments ..
014:       INTEGER            GIVCOL( 2, * ), INDX( * ), INDXP( * ),
015:      $                   INDXQ( * ), PERM( * )
016:       REAL               D( * ), DLAMDA( * ), GIVNUM( 2, * ), W( * ),
017:      $                   Z( * )
018:       COMPLEX            Q( LDQ, * ), Q2( LDQ2, * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  CLAED8 merges the two sets of eigenvalues together into a single
025: *  sorted set.  Then it tries to deflate the size of the problem.
026: *  There are two ways in which deflation can occur:  when two or more
027: *  eigenvalues are close together or if there is a tiny element in the
028: *  Z vector.  For each such occurrence the order of the related secular
029: *  equation problem is reduced by one.
030: *
031: *  Arguments
032: *  =========
033: *
034: *  K      (output) INTEGER
035: *         Contains the number of non-deflated eigenvalues.
036: *         This is the order of the related secular equation.
037: *
038: *  N      (input) INTEGER
039: *         The dimension of the symmetric tridiagonal matrix.  N >= 0.
040: *
041: *  QSIZ   (input) INTEGER
042: *         The dimension of the unitary matrix used to reduce
043: *         the dense or band matrix to tridiagonal form.
044: *         QSIZ >= N if ICOMPQ = 1.
045: *
046: *  Q      (input/output) COMPLEX array, dimension (LDQ,N)
047: *         On entry, Q contains the eigenvectors of the partially solved
048: *         system which has been previously updated in matrix
049: *         multiplies with other partially solved eigensystems.
050: *         On exit, Q contains the trailing (N-K) updated eigenvectors
051: *         (those which were deflated) in its last N-K columns.
052: *
053: *  LDQ    (input) INTEGER
054: *         The leading dimension of the array Q.  LDQ >= max( 1, N ).
055: *
056: *  D      (input/output) REAL array, dimension (N)
057: *         On entry, D contains the eigenvalues of the two submatrices to
058: *         be combined.  On exit, D contains the trailing (N-K) updated
059: *         eigenvalues (those which were deflated) sorted into increasing
060: *         order.
061: *
062: *  RHO    (input/output) REAL
063: *         Contains the off diagonal element associated with the rank-1
064: *         cut which originally split the two submatrices which are now
065: *         being recombined. RHO is modified during the computation to
066: *         the value required by SLAED3.
067: *
068: *  CUTPNT (input) INTEGER
069: *         Contains the location of the last eigenvalue in the leading
070: *         sub-matrix.  MIN(1,N) <= CUTPNT <= N.
071: *
072: *  Z      (input) REAL array, dimension (N)
073: *         On input this vector contains the updating vector (the last
074: *         row of the first sub-eigenvector matrix and the first row of
075: *         the second sub-eigenvector matrix).  The contents of Z are
076: *         destroyed during the updating process.
077: *
078: *  DLAMDA (output) REAL array, dimension (N)
079: *         Contains a copy of the first K eigenvalues which will be used
080: *         by SLAED3 to form the secular equation.
081: *
082: *  Q2     (output) COMPLEX array, dimension (LDQ2,N)
083: *         If ICOMPQ = 0, Q2 is not referenced.  Otherwise,
084: *         Contains a copy of the first K eigenvectors which will be used
085: *         by SLAED7 in a matrix multiply (SGEMM) to update the new
086: *         eigenvectors.
087: *
088: *  LDQ2   (input) INTEGER
089: *         The leading dimension of the array Q2.  LDQ2 >= max( 1, N ).
090: *
091: *  W      (output) REAL array, dimension (N)
092: *         This will hold the first k values of the final
093: *         deflation-altered z-vector and will be passed to SLAED3.
094: *
095: *  INDXP  (workspace) INTEGER array, dimension (N)
096: *         This will contain the permutation used to place deflated
097: *         values of D at the end of the array. On output INDXP(1:K)
098: *         points to the nondeflated D-values and INDXP(K+1:N)
099: *         points to the deflated eigenvalues.
100: *
101: *  INDX   (workspace) INTEGER array, dimension (N)
102: *         This will contain the permutation used to sort the contents of
103: *         D into ascending order.
104: *
105: *  INDXQ  (input) INTEGER array, dimension (N)
106: *         This contains the permutation which separately sorts the two
107: *         sub-problems in D into ascending order.  Note that elements in
108: *         the second half of this permutation must first have CUTPNT
109: *         added to their values in order to be accurate.
110: *
111: *  PERM   (output) INTEGER array, dimension (N)
112: *         Contains the permutations (from deflation and sorting) to be
113: *         applied to each eigenblock.
114: *
115: *  GIVPTR (output) INTEGER
116: *         Contains the number of Givens rotations which took place in
117: *         this subproblem.
118: *
119: *  GIVCOL (output) INTEGER array, dimension (2, N)
120: *         Each pair of numbers indicates a pair of columns to take place
121: *         in a Givens rotation.
122: *
123: *  GIVNUM (output) REAL array, dimension (2, N)
124: *         Each number indicates the S value to be used in the
125: *         corresponding Givens rotation.
126: *
127: *  INFO   (output) INTEGER
128: *          = 0:  successful exit.
129: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
130: *
131: *  =====================================================================
132: *
133: *     .. Parameters ..
134:       REAL               MONE, ZERO, ONE, TWO, EIGHT
135:       PARAMETER          ( MONE = -1.0E0, ZERO = 0.0E0, ONE = 1.0E0,
136:      $                   TWO = 2.0E0, EIGHT = 8.0E0 )
137: *     ..
138: *     .. Local Scalars ..
139:       INTEGER            I, IMAX, J, JLAM, JMAX, JP, K2, N1, N1P1, N2
140:       REAL               C, EPS, S, T, TAU, TOL
141: *     ..
142: *     .. External Functions ..
143:       INTEGER            ISAMAX
144:       REAL               SLAMCH, SLAPY2
145:       EXTERNAL           ISAMAX, SLAMCH, SLAPY2
146: *     ..
147: *     .. External Subroutines ..
148:       EXTERNAL           CCOPY, CLACPY, CSROT, SCOPY, SLAMRG, SSCAL,
149:      $                   XERBLA
150: *     ..
151: *     .. Intrinsic Functions ..
152:       INTRINSIC          ABS, MAX, MIN, SQRT
153: *     ..
154: *     .. Executable Statements ..
155: *
156: *     Test the input parameters.
157: *
158:       INFO = 0
159: *
160:       IF( N.LT.0 ) THEN
161:          INFO = -2
162:       ELSE IF( QSIZ.LT.N ) THEN
163:          INFO = -3
164:       ELSE IF( LDQ.LT.MAX( 1, N ) ) THEN
165:          INFO = -5
166:       ELSE IF( CUTPNT.LT.MIN( 1, N ) .OR. CUTPNT.GT.N ) THEN
167:          INFO = -8
168:       ELSE IF( LDQ2.LT.MAX( 1, N ) ) THEN
169:          INFO = -12
170:       END IF
171:       IF( INFO.NE.0 ) THEN
172:          CALL XERBLA( 'CLAED8', -INFO )
173:          RETURN
174:       END IF
175: *
176: *     Quick return if possible
177: *
178:       IF( N.EQ.0 )
179:      $   RETURN
180: *
181:       N1 = CUTPNT
182:       N2 = N - N1
183:       N1P1 = N1 + 1
184: *
185:       IF( RHO.LT.ZERO ) THEN
186:          CALL SSCAL( N2, MONE, Z( N1P1 ), 1 )
187:       END IF
188: *
189: *     Normalize z so that norm(z) = 1
190: *
191:       T = ONE / SQRT( TWO )
192:       DO 10 J = 1, N
193:          INDX( J ) = J
194:    10 CONTINUE
195:       CALL SSCAL( N, T, Z, 1 )
196:       RHO = ABS( TWO*RHO )
197: *
198: *     Sort the eigenvalues into increasing order
199: *
200:       DO 20 I = CUTPNT + 1, N
201:          INDXQ( I ) = INDXQ( I ) + CUTPNT
202:    20 CONTINUE
203:       DO 30 I = 1, N
204:          DLAMDA( I ) = D( INDXQ( I ) )
205:          W( I ) = Z( INDXQ( I ) )
206:    30 CONTINUE
207:       I = 1
208:       J = CUTPNT + 1
209:       CALL SLAMRG( N1, N2, DLAMDA, 1, 1, INDX )
210:       DO 40 I = 1, N
211:          D( I ) = DLAMDA( INDX( I ) )
212:          Z( I ) = W( INDX( I ) )
213:    40 CONTINUE
214: *
215: *     Calculate the allowable deflation tolerance
216: *
217:       IMAX = ISAMAX( N, Z, 1 )
218:       JMAX = ISAMAX( N, D, 1 )
219:       EPS = SLAMCH( 'Epsilon' )
220:       TOL = EIGHT*EPS*ABS( D( JMAX ) )
221: *
222: *     If the rank-1 modifier is small enough, no more needs to be done
223: *     -- except to reorganize Q so that its columns correspond with the
224: *     elements in D.
225: *
226:       IF( RHO*ABS( Z( IMAX ) ).LE.TOL ) THEN
227:          K = 0
228:          DO 50 J = 1, N
229:             PERM( J ) = INDXQ( INDX( J ) )
230:             CALL CCOPY( QSIZ, Q( 1, PERM( J ) ), 1, Q2( 1, J ), 1 )
231:    50    CONTINUE
232:          CALL CLACPY( 'A', QSIZ, N, Q2( 1, 1 ), LDQ2, Q( 1, 1 ), LDQ )
233:          RETURN
234:       END IF
235: *
236: *     If there are multiple eigenvalues then the problem deflates.  Here
237: *     the number of equal eigenvalues are found.  As each equal
238: *     eigenvalue is found, an elementary reflector is computed to rotate
239: *     the corresponding eigensubspace so that the corresponding
240: *     components of Z are zero in this new basis.
241: *
242:       K = 0
243:       GIVPTR = 0
244:       K2 = N + 1
245:       DO 60 J = 1, N
246:          IF( RHO*ABS( Z( J ) ).LE.TOL ) THEN
247: *
248: *           Deflate due to small z component.
249: *
250:             K2 = K2 - 1
251:             INDXP( K2 ) = J
252:             IF( J.EQ.N )
253:      $         GO TO 100
254:          ELSE
255:             JLAM = J
256:             GO TO 70
257:          END IF
258:    60 CONTINUE
259:    70 CONTINUE
260:       J = J + 1
261:       IF( J.GT.N )
262:      $   GO TO 90
263:       IF( RHO*ABS( Z( J ) ).LE.TOL ) THEN
264: *
265: *        Deflate due to small z component.
266: *
267:          K2 = K2 - 1
268:          INDXP( K2 ) = J
269:       ELSE
270: *
271: *        Check if eigenvalues are close enough to allow deflation.
272: *
273:          S = Z( JLAM )
274:          C = Z( J )
275: *
276: *        Find sqrt(a**2+b**2) without overflow or
277: *        destructive underflow.
278: *
279:          TAU = SLAPY2( C, S )
280:          T = D( J ) - D( JLAM )
281:          C = C / TAU
282:          S = -S / TAU
283:          IF( ABS( T*C*S ).LE.TOL ) THEN
284: *
285: *           Deflation is possible.
286: *
287:             Z( J ) = TAU
288:             Z( JLAM ) = ZERO
289: *
290: *           Record the appropriate Givens rotation
291: *
292:             GIVPTR = GIVPTR + 1
293:             GIVCOL( 1, GIVPTR ) = INDXQ( INDX( JLAM ) )
294:             GIVCOL( 2, GIVPTR ) = INDXQ( INDX( J ) )
295:             GIVNUM( 1, GIVPTR ) = C
296:             GIVNUM( 2, GIVPTR ) = S
297:             CALL CSROT( QSIZ, Q( 1, INDXQ( INDX( JLAM ) ) ), 1,
298:      $                  Q( 1, INDXQ( INDX( J ) ) ), 1, C, S )
299:             T = D( JLAM )*C*C + D( J )*S*S
300:             D( J ) = D( JLAM )*S*S + D( J )*C*C
301:             D( JLAM ) = T
302:             K2 = K2 - 1
303:             I = 1
304:    80       CONTINUE
305:             IF( K2+I.LE.N ) THEN
306:                IF( D( JLAM ).LT.D( INDXP( K2+I ) ) ) THEN
307:                   INDXP( K2+I-1 ) = INDXP( K2+I )
308:                   INDXP( K2+I ) = JLAM
309:                   I = I + 1
310:                   GO TO 80
311:                ELSE
312:                   INDXP( K2+I-1 ) = JLAM
313:                END IF
314:             ELSE
315:                INDXP( K2+I-1 ) = JLAM
316:             END IF
317:             JLAM = J
318:          ELSE
319:             K = K + 1
320:             W( K ) = Z( JLAM )
321:             DLAMDA( K ) = D( JLAM )
322:             INDXP( K ) = JLAM
323:             JLAM = J
324:          END IF
325:       END IF
326:       GO TO 70
327:    90 CONTINUE
328: *
329: *     Record the last eigenvalue.
330: *
331:       K = K + 1
332:       W( K ) = Z( JLAM )
333:       DLAMDA( K ) = D( JLAM )
334:       INDXP( K ) = JLAM
335: *
336:   100 CONTINUE
337: *
338: *     Sort the eigenvalues and corresponding eigenvectors into DLAMDA
339: *     and Q2 respectively.  The eigenvalues/vectors which were not
340: *     deflated go into the first K slots of DLAMDA and Q2 respectively,
341: *     while those which were deflated go into the last N - K slots.
342: *
343:       DO 110 J = 1, N
344:          JP = INDXP( J )
345:          DLAMDA( J ) = D( JP )
346:          PERM( J ) = INDXQ( INDX( JP ) )
347:          CALL CCOPY( QSIZ, Q( 1, PERM( J ) ), 1, Q2( 1, J ), 1 )
348:   110 CONTINUE
349: *
350: *     The deflated eigenvalues and their corresponding vectors go back
351: *     into the last N - K slots of D and Q respectively.
352: *
353:       IF( K.LT.N ) THEN
354:          CALL SCOPY( N-K, DLAMDA( K+1 ), 1, D( K+1 ), 1 )
355:          CALL CLACPY( 'A', QSIZ, N-K, Q2( 1, K+1 ), LDQ2, Q( 1, K+1 ),
356:      $                LDQ )
357:       END IF
358: *
359:       RETURN
360: *
361: *     End of CLAED8
362: *
363:       END
364: