1       SUBROUTINE DLAQR4( WANTT, WANTZ, N, ILO, IHI, H, LDH, WR, WI,
  2      $                   ILOZ, IHIZ, Z, LDZ, WORK, LWORK, INFO )
  3 *
  4 *  -- LAPACK auxiliary routine (version 3.2) --
  5 *     Univ. of Tennessee, Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..
  6 *     November 2006
  7 *
  8 *     .. Scalar Arguments ..
  9       INTEGER            IHI, IHIZ, ILO, ILOZ, INFO, LDH, LDZ, LWORK, N
 10       LOGICAL            WANTT, WANTZ
 11 *     ..
 12 *     .. Array Arguments ..
 13       DOUBLE PRECISION   H( LDH, * ), WI( * ), WORK( * ), WR( * ),
 14      $                   Z( LDZ, * )
 15 *     ..
 16 *
 17 *     This subroutine implements one level of recursion for DLAQR0.
 18 *     It is a complete implementation of the small bulge multi-shift
 19 *     QR algorithm.  It may be called by DLAQR0 and, for large enough
 20 *     deflation window size, it may be called by DLAQR3.  This
 21 *     subroutine is identical to DLAQR0 except that it calls DLAQR2
 22 *     instead of DLAQR3.
 23 *
 24 *     Purpose
 25 *     =======
 26 *
 27 *     DLAQR4 computes the eigenvalues of a Hessenberg matrix H
 28 *     and, optionally, the matrices T and Z from the Schur decomposition
 29 *     H = Z T Z**T, where T is an upper quasi-triangular matrix (the
 30 *     Schur form), and Z is the orthogonal matrix of Schur vectors.
 31 *
 32 *     Optionally Z may be postmultiplied into an input orthogonal
 33 *     matrix Q so that this routine can give the Schur factorization
 34 *     of a matrix A which has been reduced to the Hessenberg form H
 35 *     by the orthogonal matrix Q:  A = Q*H*Q**T = (QZ)*T*(QZ)**T.
 36 *
 37 *     Arguments
 38 *     =========
 39 *
 40 *     WANTT   (input) LOGICAL
 41 *          = .TRUE. : the full Schur form T is required;
 42 *          = .FALSE.: only eigenvalues are required.
 43 *
 44 *     WANTZ   (input) LOGICAL
 45 *          = .TRUE. : the matrix of Schur vectors Z is required;
 46 *          = .FALSE.: Schur vectors are not required.
 47 *
 48 *     N     (input) INTEGER
 49 *           The order of the matrix H.  N .GE. 0.
 50 *
 51 *     ILO   (input) INTEGER
 52 *     IHI   (input) INTEGER
 53 *           It is assumed that H is already upper triangular in rows
 54 *           and columns 1:ILO-1 and IHI+1:N and, if ILO.GT.1,
 55 *           H(ILO,ILO-1) is zero. ILO and IHI are normally set by a
 56 *           previous call to DGEBAL, and then passed to DGEHRD when the
 57 *           matrix output by DGEBAL is reduced to Hessenberg form.
 58 *           Otherwise, ILO and IHI should be set to 1 and N,
 59 *           respectively.  If N.GT.0, then 1.LE.ILO.LE.IHI.LE.N.
 60 *           If N = 0, then ILO = 1 and IHI = 0.
 61 *
 62 *     H     (input/output) DOUBLE PRECISION array, dimension (LDH,N)
 63 *           On entry, the upper Hessenberg matrix H.
 64 *           On exit, if INFO = 0 and WANTT is .TRUE., then H contains
 65 *           the upper quasi-triangular matrix T from the Schur
 66 *           decomposition (the Schur form); 2-by-2 diagonal blocks
 67 *           (corresponding to complex conjugate pairs of eigenvalues)
 68 *           are returned in standard form, with H(i,i) = H(i+1,i+1)
 69 *           and H(i+1,i)*H(i,i+1).LT.0. If INFO = 0 and WANTT is
 70 *           .FALSE., then the contents of H are unspecified on exit.
 71 *           (The output value of H when INFO.GT.0 is given under the
 72 *           description of INFO below.)
 73 *
 74 *           This subroutine may explicitly set H(i,j) = 0 for i.GT.j and
 75 *           j = 1, 2, ... ILO-1 or j = IHI+1, IHI+2, ... N.
 76 *
 77 *     LDH   (input) INTEGER
 78 *           The leading dimension of the array H. LDH .GE. max(1,N).
 79 *
 80 *     WR    (output) DOUBLE PRECISION array, dimension (IHI)
 81 *     WI    (output) DOUBLE PRECISION array, dimension (IHI)
 82 *           The real and imaginary parts, respectively, of the computed
 83 *           eigenvalues of H(ILO:IHI,ILO:IHI) are stored in WR(ILO:IHI)
 84 *           and WI(ILO:IHI). If two eigenvalues are computed as a
 85 *           complex conjugate pair, they are stored in consecutive
 86 *           elements of WR and WI, say the i-th and (i+1)th, with
 87 *           WI(i) .GT. 0 and WI(i+1) .LT. 0. If WANTT is .TRUE., then
 88 *           the eigenvalues are stored in the same order as on the
 89 *           diagonal of the Schur form returned in H, with
 90 *           WR(i) = H(i,i) and, if H(i:i+1,i:i+1) is a 2-by-2 diagonal
 91 *           block, WI(i) = sqrt(-H(i+1,i)*H(i,i+1)) and
 92 *           WI(i+1) = -WI(i).
 93 *
 94 *     ILOZ     (input) INTEGER
 95 *     IHIZ     (input) INTEGER
 96 *           Specify the rows of Z to which transformations must be
 97 *           applied if WANTZ is .TRUE..
 98 *           1 .LE. ILOZ .LE. ILO; IHI .LE. IHIZ .LE. N.
 99 *
100 *     Z     (input/output) DOUBLE PRECISION array, dimension (LDZ,IHI)
101 *           If WANTZ is .FALSE., then Z is not referenced.
102 *           If WANTZ is .TRUE., then Z(ILO:IHI,ILOZ:IHIZ) is
103 *           replaced by Z(ILO:IHI,ILOZ:IHIZ)*U where U is the
104 *           orthogonal Schur factor of H(ILO:IHI,ILO:IHI).
105 *           (The output value of Z when INFO.GT.0 is given under
106 *           the description of INFO below.)
107 *
108 *     LDZ   (input) INTEGER
109 *           The leading dimension of the array Z.  if WANTZ is .TRUE.
110 *           then LDZ.GE.MAX(1,IHIZ).  Otherwize, LDZ.GE.1.
111 *
112 *     WORK  (workspace/output) DOUBLE PRECISION array, dimension LWORK
113 *           On exit, if LWORK = -1, WORK(1) returns an estimate of
114 *           the optimal value for LWORK.
115 *
116 *     LWORK (input) INTEGER
117 *           The dimension of the array WORK.  LWORK .GE. max(1,N)
118 *           is sufficient, but LWORK typically as large as 6*N may
119 *           be required for optimal performance.  A workspace query
120 *           to determine the optimal workspace size is recommended.
121 *
122 *           If LWORK = -1, then DLAQR4 does a workspace query.
123 *           In this case, DLAQR4 checks the input parameters and
124 *           estimates the optimal workspace size for the given
125 *           values of N, ILO and IHI.  The estimate is returned
126 *           in WORK(1).  No error message related to LWORK is
127 *           issued by XERBLA.  Neither H nor Z are accessed.
128 *
129 *
130 *     INFO  (output) INTEGER
131 *             =  0:  successful exit
132 *           .GT. 0:  if INFO = i, DLAQR4 failed to compute all of
133 *                the eigenvalues.  Elements 1:ilo-1 and i+1:n of WR
134 *                and WI contain those eigenvalues which have been
135 *                successfully computed.  (Failures are rare.)
136 *
137 *                If INFO .GT. 0 and WANT is .FALSE., then on exit,
138 *                the remaining unconverged eigenvalues are the eigen-
139 *                values of the upper Hessenberg matrix rows and
140 *                columns ILO through INFO of the final, output
141 *                value of H.
142 *
143 *                If INFO .GT. 0 and WANTT is .TRUE., then on exit
144 *
145 *           (*)  (initial value of H)*U  = U*(final value of H)
146 *
147 *                where U is an orthogonal matrix.  The final
148 *                value of H is upper Hessenberg and quasi-triangular
149 *                in rows and columns INFO+1 through IHI.
150 *
151 *                If INFO .GT. 0 and WANTZ is .TRUE., then on exit
152 *
153 *                  (final value of Z(ILO:IHI,ILOZ:IHIZ)
154 *                   =  (initial value of Z(ILO:IHI,ILOZ:IHIZ)*U
155 *
156 *                where U is the orthogonal matrix in (*) (regard-
157 *                less of the value of WANTT.)
158 *
159 *                If INFO .GT. 0 and WANTZ is .FALSE., then Z is not
160 *                accessed.
161 *
162 *     ================================================================
163 *     Based on contributions by
164 *        Karen Braman and Ralph Byers, Department of Mathematics,
165 *        University of Kansas, USA
166 *
167 *     ================================================================
168 *     References:
169 *       K. Braman, R. Byers and R. Mathias, The Multi-Shift QR
170 *       Algorithm Part I: Maintaining Well Focused Shifts, and Level 3
171 *       Performance, SIAM Journal of Matrix Analysis, volume 23, pages
172 *       929--947, 2002.
173 *
174 *       K. Braman, R. Byers and R. Mathias, The Multi-Shift QR
175 *       Algorithm Part II: Aggressive Early Deflation, SIAM Journal
176 *       of Matrix Analysis, volume 23, pages 948--973, 2002.
177 *
178 *     ================================================================
179 *     .. Parameters ..
180 *
181 *     ==== Matrices of order NTINY or smaller must be processed by
182 *     .    DLAHQR because of insufficient subdiagonal scratch space.
183 *     .    (This is a hard limit.) ====
184       INTEGER            NTINY
185       PARAMETER          ( NTINY = 11 )
186 *
187 *     ==== Exceptional deflation windows:  try to cure rare
188 *     .    slow convergence by varying the size of the
189 *     .    deflation window after KEXNW iterations. ====
190       INTEGER            KEXNW
191       PARAMETER          ( KEXNW = 5 )
192 *
193 *     ==== Exceptional shifts: try to cure rare slow convergence
194 *     .    with ad-hoc exceptional shifts every KEXSH iterations.
195 *     .    ====
196       INTEGER            KEXSH
197       PARAMETER          ( KEXSH = 6 )
198 *
199 *     ==== The constants WILK1 and WILK2 are used to form the
200 *     .    exceptional shifts. ====
201       DOUBLE PRECISION   WILK1, WILK2
202       PARAMETER          ( WILK1 = 0.75d0, WILK2 = -0.4375d0 )
203       DOUBLE PRECISION   ZERO, ONE
204       PARAMETER          ( ZERO = 0.0d0, ONE = 1.0d0 )
205 *     ..
206 *     .. Local Scalars ..
207       DOUBLE PRECISION   AA, BB, CC, CS, DD, SN, SS, SWAP
208       INTEGER            I, INF, IT, ITMAX, K, KACC22, KBOT, KDU, KS,
209      $                   KT, KTOP, KU, KV, KWH, KWTOP, KWV, LD, LS,
210      $                   LWKOPT, NDEC, NDFL, NH, NHO, NIBBLE, NMIN, NS,
211      $                   NSMAX, NSR, NVE, NW, NWMAX, NWR, NWUPBD
212       LOGICAL            SORTED
213       CHARACTER          JBCMPZ*2
214 *     ..
215 *     .. External Functions ..
216       INTEGER            ILAENV
217       EXTERNAL           ILAENV
218 *     ..
219 *     .. Local Arrays ..
220       DOUBLE PRECISION   ZDUM( 11 )
221 *     ..
222 *     .. External Subroutines ..
223       EXTERNAL           DLACPY, DLAHQR, DLANV2, DLAQR2, DLAQR5
224 *     ..
225 *     .. Intrinsic Functions ..
226       INTRINSIC          ABSDBLEINTMAXMINMOD
227 *     ..
228 *     .. Executable Statements ..
229       INFO = 0
230 
231 *
232 *     ==== Quick return for N = 0: nothing to do. ====
233 *
234       IF( N.EQ.0 ) THEN
235          WORK( 1 ) = ONE
236          RETURN
237       END IF
238 *
239       IF( N.LE.NTINY ) THEN
240 *
241 *        ==== Tiny matrices must use DLAHQR. ====
242 *
243          LWKOPT = 1
244          IF( LWORK.NE.-1 )
245      $      CALL DLAHQR( WANTT, WANTZ, N, ILO, IHI, H, LDH, WR, WI,
246      $                   ILOZ, IHIZ, Z, LDZ, INFO )
247       ELSE
248 *
249 *        ==== Use small bulge multi-shift QR with aggressive early
250 *        .    deflation on larger-than-tiny matrices. ====
251 *
252 *        ==== Hope for the best. ====
253 *
254          INFO = 0
255 *
256 *        ==== Set up job flags for ILAENV. ====
257 *
258          IF( WANTT ) THEN
259             JBCMPZ( 11 ) = 'S'
260          ELSE
261             JBCMPZ( 11 ) = 'E'
262          END IF
263          IF( WANTZ ) THEN
264             JBCMPZ( 22 ) = 'V'
265          ELSE
266             JBCMPZ( 22 ) = 'N'
267          END IF
268 *
269 *        ==== NWR = recommended deflation window size.  At this
270 *        .    point,  N .GT. NTINY = 11, so there is enough
271 *        .    subdiagonal workspace for NWR.GE.2 as required.
272 *        .    (In fact, there is enough subdiagonal space for
273 *        .    NWR.GE.3.) ====
274 *
275          NWR = ILAENV( 13'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
276          NWR = MAX2, NWR )
277          NWR = MIN( IHI-ILO+1, ( N-1 ) / 3, NWR )
278 *
279 *        ==== NSR = recommended number of simultaneous shifts.
280 *        .    At this point N .GT. NTINY = 11, so there is at
281 *        .    enough subdiagonal workspace for NSR to be even
282 *        .    and greater than or equal to two as required. ====
283 *
284          NSR = ILAENV( 15'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
285          NSR = MIN( NSR, ( N+6 ) / 9, IHI-ILO )
286          NSR = MAX2, NSR-MOD( NSR, 2 ) )
287 *
288 *        ==== Estimate optimal workspace ====
289 *
290 *        ==== Workspace query call to DLAQR2 ====
291 *
292          CALL DLAQR2( WANTT, WANTZ, N, ILO, IHI, NWR+1, H, LDH, ILOZ,
293      $                IHIZ, Z, LDZ, LS, LD, WR, WI, H, LDH, N, H, LDH,
294      $                N, H, LDH, WORK, -1 )
295 *
296 *        ==== Optimal workspace = MAX(DLAQR5, DLAQR2) ====
297 *
298          LWKOPT = MAX3*NSR / 2INT( WORK( 1 ) ) )
299 *
300 *        ==== Quick return in case of workspace query. ====
301 *
302          IF( LWORK.EQ.-1 ) THEN
303             WORK( 1 ) = DBLE( LWKOPT )
304             RETURN
305          END IF
306 *
307 *        ==== DLAHQR/DLAQR0 crossover point ====
308 *
309          NMIN = ILAENV( 12'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
310          NMIN = MAX( NTINY, NMIN )
311 *
312 *        ==== Nibble crossover point ====
313 *
314          NIBBLE = ILAENV( 14'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
315          NIBBLE = MAX0, NIBBLE )
316 *
317 *        ==== Accumulate reflections during ttswp?  Use block
318 *        .    2-by-2 structure during matrix-matrix multiply? ====
319 *
320          KACC22 = ILAENV( 16'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
321          KACC22 = MAX0, KACC22 )
322          KACC22 = MIN2, KACC22 )
323 *
324 *        ==== NWMAX = the largest possible deflation window for
325 *        .    which there is sufficient workspace. ====
326 *
327          NWMAX = MIN( ( N-1 ) / 3, LWORK / 2 )
328          NW = NWMAX
329 *
330 *        ==== NSMAX = the Largest number of simultaneous shifts
331 *        .    for which there is sufficient workspace. ====
332 *
333          NSMAX = MIN( ( N+6 ) / 92*LWORK / 3 )
334          NSMAX = NSMAX - MOD( NSMAX, 2 )
335 *
336 *        ==== NDFL: an iteration count restarted at deflation. ====
337 *
338          NDFL = 1
339 *
340 *        ==== ITMAX = iteration limit ====
341 *
342          ITMAX = MAX302*KEXSH )*MAX10, ( IHI-ILO+1 ) )
343 *
344 *        ==== Last row and column in the active block ====
345 *
346          KBOT = IHI
347 *
348 *        ==== Main Loop ====
349 *
350          DO 80 IT = 1, ITMAX
351 *
352 *           ==== Done when KBOT falls below ILO ====
353 *
354             IF( KBOT.LT.ILO )
355      $         GO TO 90
356 *
357 *           ==== Locate active block ====
358 *
359             DO 10 K = KBOT, ILO + 1-1
360                IF( H( K, K-1 ).EQ.ZERO )
361      $            GO TO 20
362    10       CONTINUE
363             K = ILO
364    20       CONTINUE
365             KTOP = K
366 *
367 *           ==== Select deflation window size:
368 *           .    Typical Case:
369 *           .      If possible and advisable, nibble the entire
370 *           .      active block.  If not, use size MIN(NWR,NWMAX)
371 *           .      or MIN(NWR+1,NWMAX) depending upon which has
372 *           .      the smaller corresponding subdiagonal entry
373 *           .      (a heuristic).
374 *           .
375 *           .    Exceptional Case:
376 *           .      If there have been no deflations in KEXNW or
377 *           .      more iterations, then vary the deflation window
378 *           .      size.   At first, because, larger windows are,
379 *           .      in general, more powerful than smaller ones,
380 *           .      rapidly increase the window to the maximum possible.
381 *           .      Then, gradually reduce the window size. ====
382 *
383             NH = KBOT - KTOP + 1
384             NWUPBD = MIN( NH, NWMAX )
385             IF( NDFL.LT.KEXNW ) THEN
386                NW = MIN( NWUPBD, NWR )
387             ELSE
388                NW = MIN( NWUPBD, 2*NW )
389             END IF
390             IF( NW.LT.NWMAX ) THEN
391                IF( NW.GE.NH-1 ) THEN
392                   NW = NH
393                ELSE
394                   KWTOP = KBOT - NW + 1
395                   IFABS( H( KWTOP, KWTOP-1 ) ).GT.
396      $                ABS( H( KWTOP-1, KWTOP-2 ) ) )NW = NW + 1
397                END IF
398             END IF
399             IF( NDFL.LT.KEXNW ) THEN
400                NDEC = -1
401             ELSE IF( NDEC.GE.0 .OR. NW.GE.NWUPBD ) THEN
402                NDEC = NDEC + 1
403                IF( NW-NDEC.LT.2 )
404      $            NDEC = 0
405                NW = NW - NDEC
406             END IF
407 *
408 *           ==== Aggressive early deflation:
409 *           .    split workspace under the subdiagonal into
410 *           .      - an nw-by-nw work array V in the lower
411 *           .        left-hand-corner,
412 *           .      - an NW-by-at-least-NW-but-more-is-better
413 *           .        (NW-by-NHO) horizontal work array along
414 *           .        the bottom edge,
415 *           .      - an at-least-NW-but-more-is-better (NHV-by-NW)
416 *           .        vertical work array along the left-hand-edge.
417 *           .        ====
418 *
419             KV = N - NW + 1
420             KT = NW + 1
421             NHO = ( N-NW-1 ) - KT + 1
422             KWV = NW + 2
423             NVE = ( N-NW ) - KWV + 1
424 *
425 *           ==== Aggressive early deflation ====
426 *
427             CALL DLAQR2( WANTT, WANTZ, N, KTOP, KBOT, NW, H, LDH, ILOZ,
428      $                   IHIZ, Z, LDZ, LS, LD, WR, WI, H( KV, 1 ), LDH,
429      $                   NHO, H( KV, KT ), LDH, NVE, H( KWV, 1 ), LDH,
430      $                   WORK, LWORK )
431 *
432 *           ==== Adjust KBOT accounting for new deflations. ====
433 *
434             KBOT = KBOT - LD
435 *
436 *           ==== KS points to the shifts. ====
437 *
438             KS = KBOT - LS + 1
439 *
440 *           ==== Skip an expensive QR sweep if there is a (partly
441 *           .    heuristic) reason to expect that many eigenvalues
442 *           .    will deflate without it.  Here, the QR sweep is
443 *           .    skipped if many eigenvalues have just been deflated
444 *           .    or if the remaining active block is small.
445 *
446             IF( ( LD.EQ.0 ) .OR. ( ( 100*LD.LE.NW*NIBBLE ) .AND. ( KBOT-
447      $          KTOP+1.GT.MIN( NMIN, NWMAX ) ) ) ) THEN
448 *
449 *              ==== NS = nominal number of simultaneous shifts.
450 *              .    This may be lowered (slightly) if DLAQR2
451 *              .    did not provide that many shifts. ====
452 *
453                NS = MIN( NSMAX, NSR, MAX2, KBOT-KTOP ) )
454                NS = NS - MOD( NS, 2 )
455 *
456 *              ==== If there have been no deflations
457 *              .    in a multiple of KEXSH iterations,
458 *              .    then try exceptional shifts.
459 *              .    Otherwise use shifts provided by
460 *              .    DLAQR2 above or from the eigenvalues
461 *              .    of a trailing principal submatrix. ====
462 *
463                IFMOD( NDFL, KEXSH ).EQ.0 ) THEN
464                   KS = KBOT - NS + 1
465                   DO 30 I = KBOT, MAX( KS+1, KTOP+2 ), -2
466                      SS = ABS( H( I, I-1 ) ) + ABS( H( I-1, I-2 ) )
467                      AA = WILK1*SS + H( I, I )
468                      BB = SS
469                      CC = WILK2*SS
470                      DD = AA
471                      CALL DLANV2( AA, BB, CC, DD, WR( I-1 ), WI( I-1 ),
472      $                            WR( I ), WI( I ), CS, SN )
473    30             CONTINUE
474                   IF( KS.EQ.KTOP ) THEN
475                      WR( KS+1 ) = H( KS+1, KS+1 )
476                      WI( KS+1 ) = ZERO
477                      WR( KS ) = WR( KS+1 )
478                      WI( KS ) = WI( KS+1 )
479                   END IF
480                ELSE
481 *
482 *                 ==== Got NS/2 or fewer shifts? Use DLAHQR
483 *                 .    on a trailing principal submatrix to
484 *                 .    get more. (Since NS.LE.NSMAX.LE.(N+6)/9,
485 *                 .    there is enough space below the subdiagonal
486 *                 .    to fit an NS-by-NS scratch array.) ====
487 *
488                   IF( KBOT-KS+1.LE.NS / 2 ) THEN
489                      KS = KBOT - NS + 1
490                      KT = N - NS + 1
491                      CALL DLACPY( 'A', NS, NS, H( KS, KS ), LDH,
492      $                            H( KT, 1 ), LDH )
493                      CALL DLAHQR( .false..false., NS, 1, NS,
494      $                            H( KT, 1 ), LDH, WR( KS ), WI( KS ),
495      $                            11, ZDUM, 1, INF )
496                      KS = KS + INF
497 *
498 *                    ==== In case of a rare QR failure use
499 *                    .    eigenvalues of the trailing 2-by-2
500 *                    .    principal submatrix.  ====
501 *
502                      IF( KS.GE.KBOT ) THEN
503                         AA = H( KBOT-1, KBOT-1 )
504                         CC = H( KBOT, KBOT-1 )
505                         BB = H( KBOT-1, KBOT )
506                         DD = H( KBOT, KBOT )
507                         CALL DLANV2( AA, BB, CC, DD, WR( KBOT-1 ),
508      $                               WI( KBOT-1 ), WR( KBOT ),
509      $                               WI( KBOT ), CS, SN )
510                         KS = KBOT - 1
511                      END IF
512                   END IF
513 *
514                   IF( KBOT-KS+1.GT.NS ) THEN
515 *
516 *                    ==== Sort the shifts (Helps a little)
517 *                    .    Bubble sort keeps complex conjugate
518 *                    .    pairs together. ====
519 *
520                      SORTED = .false.
521                      DO 50 K = KBOT, KS + 1-1
522                         IF( SORTED )
523      $                     GO TO 60
524                         SORTED = .true.
525                         DO 40 I = KS, K - 1
526                            IFABS( WR( I ) )+ABS( WI( I ) ).LT.
527      $                         ABS( WR( I+1 ) )+ABS( WI( I+1 ) ) ) THEN
528                               SORTED = .false.
529 *
530                               SWAP = WR( I )
531                               WR( I ) = WR( I+1 )
532                               WR( I+1 ) = SWAP
533 *
534                               SWAP = WI( I )
535                               WI( I ) = WI( I+1 )
536                               WI( I+1 ) = SWAP
537                            END IF
538    40                   CONTINUE
539    50                CONTINUE
540    60                CONTINUE
541                   END IF
542 *
543 *                 ==== Shuffle shifts into pairs of real shifts
544 *                 .    and pairs of complex conjugate shifts
545 *                 .    assuming complex conjugate shifts are
546 *                 .    already adjacent to one another. (Yes,
547 *                 .    they are.)  ====
548 *
549                   DO 70 I = KBOT, KS + 2-2
550                      IF( WI( I ).NE.-WI( I-1 ) ) THEN
551 *
552                         SWAP = WR( I )
553                         WR( I ) = WR( I-1 )
554                         WR( I-1 ) = WR( I-2 )
555                         WR( I-2 ) = SWAP
556 *
557                         SWAP = WI( I )
558                         WI( I ) = WI( I-1 )
559                         WI( I-1 ) = WI( I-2 )
560                         WI( I-2 ) = SWAP
561                      END IF
562    70             CONTINUE
563                END IF
564 *
565 *              ==== If there are only two shifts and both are
566 *              .    real, then use only one.  ====
567 *
568                IF( KBOT-KS+1.EQ.2 ) THEN
569                   IF( WI( KBOT ).EQ.ZERO ) THEN
570                      IFABS( WR( KBOT )-H( KBOT, KBOT ) ).LT.
571      $                   ABS( WR( KBOT-1 )-H( KBOT, KBOT ) ) ) THEN
572                         WR( KBOT-1 ) = WR( KBOT )
573                      ELSE
574                         WR( KBOT ) = WR( KBOT-1 )
575                      END IF
576                   END IF
577                END IF
578 *
579 *              ==== Use up to NS of the the smallest magnatiude
580 *              .    shifts.  If there aren't NS shifts available,
581 *              .    then use them all, possibly dropping one to
582 *              .    make the number of shifts even. ====
583 *
584                NS = MIN( NS, KBOT-KS+1 )
585                NS = NS - MOD( NS, 2 )
586                KS = KBOT - NS + 1
587 *
588 *              ==== Small-bulge multi-shift QR sweep:
589 *              .    split workspace under the subdiagonal into
590 *              .    - a KDU-by-KDU work array U in the lower
591 *              .      left-hand-corner,
592 *              .    - a KDU-by-at-least-KDU-but-more-is-better
593 *              .      (KDU-by-NHo) horizontal work array WH along
594 *              .      the bottom edge,
595 *              .    - and an at-least-KDU-but-more-is-better-by-KDU
596 *              .      (NVE-by-KDU) vertical work WV arrow along
597 *              .      the left-hand-edge. ====
598 *
599                KDU = 3*NS - 3
600                KU = N - KDU + 1
601                KWH = KDU + 1
602                NHO = ( N-KDU+1-4 ) - ( KDU+1 ) + 1
603                KWV = KDU + 4
604                NVE = N - KDU - KWV + 1
605 *
606 *              ==== Small-bulge multi-shift QR sweep ====
607 *
608                CALL DLAQR5( WANTT, WANTZ, KACC22, N, KTOP, KBOT, NS,
609      $                      WR( KS ), WI( KS ), H, LDH, ILOZ, IHIZ, Z,
610      $                      LDZ, WORK, 3, H( KU, 1 ), LDH, NVE,
611      $                      H( KWV, 1 ), LDH, NHO, H( KU, KWH ), LDH )
612             END IF
613 *
614 *           ==== Note progress (or the lack of it). ====
615 *
616             IF( LD.GT.0 ) THEN
617                NDFL = 1
618             ELSE
619                NDFL = NDFL + 1
620             END IF
621 *
622 *           ==== End of main loop ====
623    80    CONTINUE
624 *
625 *        ==== Iteration limit exceeded.  Set INFO to show where
626 *        .    the problem occurred and exit. ====
627 *
628          INFO = KBOT
629    90    CONTINUE
630       END IF
631 *
632 *     ==== Return the optimal value of LWORK. ====
633 *
634       WORK( 1 ) = DBLE( LWKOPT )
635 *
636 *     ==== End of DLAQR4 ====
637 *
638       END