001:       SUBROUTINE DLAQR4( WANTT, WANTZ, N, ILO, IHI, H, LDH, WR, WI,
002:      $                   ILOZ, IHIZ, Z, LDZ, WORK, LWORK, INFO )
003: *
004: *  -- LAPACK auxiliary routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       INTEGER            IHI, IHIZ, ILO, ILOZ, INFO, LDH, LDZ, LWORK, N
010:       LOGICAL            WANTT, WANTZ
011: *     ..
012: *     .. Array Arguments ..
013:       DOUBLE PRECISION   H( LDH, * ), WI( * ), WORK( * ), WR( * ),
014:      $                   Z( LDZ, * )
015: *     ..
016: *
017: *     This subroutine implements one level of recursion for DLAQR0.
018: *     It is a complete implementation of the small bulge multi-shift
019: *     QR algorithm.  It may be called by DLAQR0 and, for large enough
020: *     deflation window size, it may be called by DLAQR3.  This
021: *     subroutine is identical to DLAQR0 except that it calls DLAQR2
022: *     instead of DLAQR3.
023: *
024: *     Purpose
025: *     =======
026: *
027: *     DLAQR4 computes the eigenvalues of a Hessenberg matrix H
028: *     and, optionally, the matrices T and Z from the Schur decomposition
029: *     H = Z T Z**T, where T is an upper quasi-triangular matrix (the
030: *     Schur form), and Z is the orthogonal matrix of Schur vectors.
031: *
032: *     Optionally Z may be postmultiplied into an input orthogonal
033: *     matrix Q so that this routine can give the Schur factorization
034: *     of a matrix A which has been reduced to the Hessenberg form H
035: *     by the orthogonal matrix Q:  A = Q*H*Q**T = (QZ)*T*(QZ)**T.
036: *
037: *     Arguments
038: *     =========
039: *
040: *     WANTT   (input) LOGICAL
041: *          = .TRUE. : the full Schur form T is required;
042: *          = .FALSE.: only eigenvalues are required.
043: *
044: *     WANTZ   (input) LOGICAL
045: *          = .TRUE. : the matrix of Schur vectors Z is required;
046: *          = .FALSE.: Schur vectors are not required.
047: *
048: *     N     (input) INTEGER
049: *           The order of the matrix H.  N .GE. 0.
050: *
051: *     ILO   (input) INTEGER
052: *     IHI   (input) INTEGER
053: *           It is assumed that H is already upper triangular in rows
054: *           and columns 1:ILO-1 and IHI+1:N and, if ILO.GT.1,
055: *           H(ILO,ILO-1) is zero. ILO and IHI are normally set by a
056: *           previous call to DGEBAL, and then passed to DGEHRD when the
057: *           matrix output by DGEBAL is reduced to Hessenberg form.
058: *           Otherwise, ILO and IHI should be set to 1 and N,
059: *           respectively.  If N.GT.0, then 1.LE.ILO.LE.IHI.LE.N.
060: *           If N = 0, then ILO = 1 and IHI = 0.
061: *
062: *     H     (input/output) DOUBLE PRECISION array, dimension (LDH,N)
063: *           On entry, the upper Hessenberg matrix H.
064: *           On exit, if INFO = 0 and WANTT is .TRUE., then H contains
065: *           the upper quasi-triangular matrix T from the Schur
066: *           decomposition (the Schur form); 2-by-2 diagonal blocks
067: *           (corresponding to complex conjugate pairs of eigenvalues)
068: *           are returned in standard form, with H(i,i) = H(i+1,i+1)
069: *           and H(i+1,i)*H(i,i+1).LT.0. If INFO = 0 and WANTT is
070: *           .FALSE., then the contents of H are unspecified on exit.
071: *           (The output value of H when INFO.GT.0 is given under the
072: *           description of INFO below.)
073: *
074: *           This subroutine may explicitly set H(i,j) = 0 for i.GT.j and
075: *           j = 1, 2, ... ILO-1 or j = IHI+1, IHI+2, ... N.
076: *
077: *     LDH   (input) INTEGER
078: *           The leading dimension of the array H. LDH .GE. max(1,N).
079: *
080: *     WR    (output) DOUBLE PRECISION array, dimension (IHI)
081: *     WI    (output) DOUBLE PRECISION array, dimension (IHI)
082: *           The real and imaginary parts, respectively, of the computed
083: *           eigenvalues of H(ILO:IHI,ILO:IHI) are stored in WR(ILO:IHI)
084: *           and WI(ILO:IHI). If two eigenvalues are computed as a
085: *           complex conjugate pair, they are stored in consecutive
086: *           elements of WR and WI, say the i-th and (i+1)th, with
087: *           WI(i) .GT. 0 and WI(i+1) .LT. 0. If WANTT is .TRUE., then
088: *           the eigenvalues are stored in the same order as on the
089: *           diagonal of the Schur form returned in H, with
090: *           WR(i) = H(i,i) and, if H(i:i+1,i:i+1) is a 2-by-2 diagonal
091: *           block, WI(i) = sqrt(-H(i+1,i)*H(i,i+1)) and
092: *           WI(i+1) = -WI(i).
093: *
094: *     ILOZ     (input) INTEGER
095: *     IHIZ     (input) INTEGER
096: *           Specify the rows of Z to which transformations must be
097: *           applied if WANTZ is .TRUE..
098: *           1 .LE. ILOZ .LE. ILO; IHI .LE. IHIZ .LE. N.
099: *
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( 1, 1 )
221: *     ..
222: *     .. External Subroutines ..
223:       EXTERNAL           DLACPY, DLAHQR, DLANV2, DLAQR2, DLAQR5
224: *     ..
225: *     .. Intrinsic Functions ..
226:       INTRINSIC          ABS, DBLE, INT, MAX, MIN, MOD
227: *     ..
228: *     .. Executable Statements ..
229:       INFO = 0
230: *
231: *     ==== Quick return for N = 0: nothing to do. ====
232: *
233:       IF( N.EQ.0 ) THEN
234:          WORK( 1 ) = ONE
235:          RETURN
236:       END IF
237: *
238:       IF( N.LE.NTINY ) THEN
239: *
240: *        ==== Tiny matrices must use DLAHQR. ====
241: *
242:          LWKOPT = 1
243:          IF( LWORK.NE.-1 )
244:      $      CALL DLAHQR( WANTT, WANTZ, N, ILO, IHI, H, LDH, WR, WI,
245:      $                   ILOZ, IHIZ, Z, LDZ, INFO )
246:       ELSE
247: *
248: *        ==== Use small bulge multi-shift QR with aggressive early
249: *        .    deflation on larger-than-tiny matrices. ====
250: *
251: *        ==== Hope for the best. ====
252: *
253:          INFO = 0
254: *
255: *        ==== Set up job flags for ILAENV. ====
256: *
257:          IF( WANTT ) THEN
258:             JBCMPZ( 1: 1 ) = 'S'
259:          ELSE
260:             JBCMPZ( 1: 1 ) = 'E'
261:          END IF
262:          IF( WANTZ ) THEN
263:             JBCMPZ( 2: 2 ) = 'V'
264:          ELSE
265:             JBCMPZ( 2: 2 ) = 'N'
266:          END IF
267: *
268: *        ==== NWR = recommended deflation window size.  At this
269: *        .    point,  N .GT. NTINY = 11, so there is enough
270: *        .    subdiagonal workspace for NWR.GE.2 as required.
271: *        .    (In fact, there is enough subdiagonal space for
272: *        .    NWR.GE.3.) ====
273: *
274:          NWR = ILAENV( 13, 'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
275:          NWR = MAX( 2, NWR )
276:          NWR = MIN( IHI-ILO+1, ( N-1 ) / 3, NWR )
277: *
278: *        ==== NSR = recommended number of simultaneous shifts.
279: *        .    At this point N .GT. NTINY = 11, so there is at
280: *        .    enough subdiagonal workspace for NSR to be even
281: *        .    and greater than or equal to two as required. ====
282: *
283:          NSR = ILAENV( 15, 'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
284:          NSR = MIN( NSR, ( N+6 ) / 9, IHI-ILO )
285:          NSR = MAX( 2, NSR-MOD( NSR, 2 ) )
286: *
287: *        ==== Estimate optimal workspace ====
288: *
289: *        ==== Workspace query call to DLAQR2 ====
290: *
291:          CALL DLAQR2( WANTT, WANTZ, N, ILO, IHI, NWR+1, H, LDH, ILOZ,
292:      $                IHIZ, Z, LDZ, LS, LD, WR, WI, H, LDH, N, H, LDH,
293:      $                N, H, LDH, WORK, -1 )
294: *
295: *        ==== Optimal workspace = MAX(DLAQR5, DLAQR2) ====
296: *
297:          LWKOPT = MAX( 3*NSR / 2, INT( WORK( 1 ) ) )
298: *
299: *        ==== Quick return in case of workspace query. ====
300: *
301:          IF( LWORK.EQ.-1 ) THEN
302:             WORK( 1 ) = DBLE( LWKOPT )
303:             RETURN
304:          END IF
305: *
306: *        ==== DLAHQR/DLAQR0 crossover point ====
307: *
308:          NMIN = ILAENV( 12, 'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
309:          NMIN = MAX( NTINY, NMIN )
310: *
311: *        ==== Nibble crossover point ====
312: *
313:          NIBBLE = ILAENV( 14, 'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
314:          NIBBLE = MAX( 0, NIBBLE )
315: *
316: *        ==== Accumulate reflections during ttswp?  Use block
317: *        .    2-by-2 structure during matrix-matrix multiply? ====
318: *
319:          KACC22 = ILAENV( 16, 'DLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
320:          KACC22 = MAX( 0, KACC22 )
321:          KACC22 = MIN( 2, KACC22 )
322: *
323: *        ==== NWMAX = the largest possible deflation window for
324: *        .    which there is sufficient workspace. ====
325: *
326:          NWMAX = MIN( ( N-1 ) / 3, LWORK / 2 )
327:          NW = NWMAX
328: *
329: *        ==== NSMAX = the Largest number of simultaneous shifts
330: *        .    for which there is sufficient workspace. ====
331: *
332:          NSMAX = MIN( ( N+6 ) / 9, 2*LWORK / 3 )
333:          NSMAX = NSMAX - MOD( NSMAX, 2 )
334: *
335: *        ==== NDFL: an iteration count restarted at deflation. ====
336: *
337:          NDFL = 1
338: *
339: *        ==== ITMAX = iteration limit ====
340: *
341:          ITMAX = MAX( 30, 2*KEXSH )*MAX( 10, ( IHI-ILO+1 ) )
342: *
343: *        ==== Last row and column in the active block ====
344: *
345:          KBOT = IHI
346: *
347: *        ==== Main Loop ====
348: *
349:          DO 80 IT = 1, ITMAX
350: *
351: *           ==== Done when KBOT falls below ILO ====
352: *
353:             IF( KBOT.LT.ILO )
354:      $         GO TO 90
355: *
356: *           ==== Locate active block ====
357: *
358:             DO 10 K = KBOT, ILO + 1, -1
359:                IF( H( K, K-1 ).EQ.ZERO )
360:      $            GO TO 20
361:    10       CONTINUE
362:             K = ILO
363:    20       CONTINUE
364:             KTOP = K
365: *
366: *           ==== Select deflation window size:
367: *           .    Typical Case:
368: *           .      If possible and advisable, nibble the entire
369: *           .      active block.  If not, use size MIN(NWR,NWMAX)
370: *           .      or MIN(NWR+1,NWMAX) depending upon which has
371: *           .      the smaller corresponding subdiagonal entry
372: *           .      (a heuristic).
373: *           .
374: *           .    Exceptional Case:
375: *           .      If there have been no deflations in KEXNW or
376: *           .      more iterations, then vary the deflation window
377: *           .      size.   At first, because, larger windows are,
378: *           .      in general, more powerful than smaller ones,
379: *           .      rapidly increase the window to the maximum possible.
380: *           .      Then, gradually reduce the window size. ====
381: *
382:             NH = KBOT - KTOP + 1
383:             NWUPBD = MIN( NH, NWMAX )
384:             IF( NDFL.LT.KEXNW ) THEN
385:                NW = MIN( NWUPBD, NWR )
386:             ELSE
387:                NW = MIN( NWUPBD, 2*NW )
388:             END IF
389:             IF( NW.LT.NWMAX ) THEN
390:                IF( NW.GE.NH-1 ) THEN
391:                   NW = NH
392:                ELSE
393:                   KWTOP = KBOT - NW + 1
394:                   IF( ABS( H( KWTOP, KWTOP-1 ) ).GT.
395:      $                ABS( H( KWTOP-1, KWTOP-2 ) ) )NW = NW + 1
396:                END IF
397:             END IF
398:             IF( NDFL.LT.KEXNW ) THEN
399:                NDEC = -1
400:             ELSE IF( NDEC.GE.0 .OR. NW.GE.NWUPBD ) THEN
401:                NDEC = NDEC + 1
402:                IF( NW-NDEC.LT.2 )
403:      $            NDEC = 0
404:                NW = NW - NDEC
405:             END IF
406: *
407: *           ==== Aggressive early deflation:
408: *           .    split workspace under the subdiagonal into
409: *           .      - an nw-by-nw work array V in the lower
410: *           .        left-hand-corner,
411: *           .      - an NW-by-at-least-NW-but-more-is-better
412: *           .        (NW-by-NHO) horizontal work array along
413: *           .        the bottom edge,
414: *           .      - an at-least-NW-but-more-is-better (NHV-by-NW)
415: *           .        vertical work array along the left-hand-edge.
416: *           .        ====
417: *
418:             KV = N - NW + 1
419:             KT = NW + 1
420:             NHO = ( N-NW-1 ) - KT + 1
421:             KWV = NW + 2
422:             NVE = ( N-NW ) - KWV + 1
423: *
424: *           ==== Aggressive early deflation ====
425: *
426:             CALL DLAQR2( WANTT, WANTZ, N, KTOP, KBOT, NW, H, LDH, ILOZ,
427:      $                   IHIZ, Z, LDZ, LS, LD, WR, WI, H( KV, 1 ), LDH,
428:      $                   NHO, H( KV, KT ), LDH, NVE, H( KWV, 1 ), LDH,
429:      $                   WORK, LWORK )
430: *
431: *           ==== Adjust KBOT accounting for new deflations. ====
432: *
433:             KBOT = KBOT - LD
434: *
435: *           ==== KS points to the shifts. ====
436: *
437:             KS = KBOT - LS + 1
438: *
439: *           ==== Skip an expensive QR sweep if there is a (partly
440: *           .    heuristic) reason to expect that many eigenvalues
441: *           .    will deflate without it.  Here, the QR sweep is
442: *           .    skipped if many eigenvalues have just been deflated
443: *           .    or if the remaining active block is small.
444: *
445:             IF( ( LD.EQ.0 ) .OR. ( ( 100*LD.LE.NW*NIBBLE ) .AND. ( KBOT-
446:      $          KTOP+1.GT.MIN( NMIN, NWMAX ) ) ) ) THEN
447: *
448: *              ==== NS = nominal number of simultaneous shifts.
449: *              .    This may be lowered (slightly) if DLAQR2
450: *              .    did not provide that many shifts. ====
451: *
452:                NS = MIN( NSMAX, NSR, MAX( 2, KBOT-KTOP ) )
453:                NS = NS - MOD( NS, 2 )
454: *
455: *              ==== If there have been no deflations
456: *              .    in a multiple of KEXSH iterations,
457: *              .    then try exceptional shifts.
458: *              .    Otherwise use shifts provided by
459: *              .    DLAQR2 above or from the eigenvalues
460: *              .    of a trailing principal submatrix. ====
461: *
462:                IF( MOD( NDFL, KEXSH ).EQ.0 ) THEN
463:                   KS = KBOT - NS + 1
464:                   DO 30 I = KBOT, MAX( KS+1, KTOP+2 ), -2
465:                      SS = ABS( H( I, I-1 ) ) + ABS( H( I-1, I-2 ) )
466:                      AA = WILK1*SS + H( I, I )
467:                      BB = SS
468:                      CC = WILK2*SS
469:                      DD = AA
470:                      CALL DLANV2( AA, BB, CC, DD, WR( I-1 ), WI( I-1 ),
471:      $                            WR( I ), WI( I ), CS, SN )
472:    30             CONTINUE
473:                   IF( KS.EQ.KTOP ) THEN
474:                      WR( KS+1 ) = H( KS+1, KS+1 )
475:                      WI( KS+1 ) = ZERO
476:                      WR( KS ) = WR( KS+1 )
477:                      WI( KS ) = WI( KS+1 )
478:                   END IF
479:                ELSE
480: *
481: *                 ==== Got NS/2 or fewer shifts? Use DLAHQR
482: *                 .    on a trailing principal submatrix to
483: *                 .    get more. (Since NS.LE.NSMAX.LE.(N+6)/9,
484: *                 .    there is enough space below the subdiagonal
485: *                 .    to fit an NS-by-NS scratch array.) ====
486: *
487:                   IF( KBOT-KS+1.LE.NS / 2 ) THEN
488:                      KS = KBOT - NS + 1
489:                      KT = N - NS + 1
490:                      CALL DLACPY( 'A', NS, NS, H( KS, KS ), LDH,
491:      $                            H( KT, 1 ), LDH )
492:                      CALL DLAHQR( .false., .false., NS, 1, NS,
493:      $                            H( KT, 1 ), LDH, WR( KS ), WI( KS ),
494:      $                            1, 1, ZDUM, 1, INF )
495:                      KS = KS + INF
496: *
497: *                    ==== In case of a rare QR failure use
498: *                    .    eigenvalues of the trailing 2-by-2
499: *                    .    principal submatrix.  ====
500: *
501:                      IF( KS.GE.KBOT ) THEN
502:                         AA = H( KBOT-1, KBOT-1 )
503:                         CC = H( KBOT, KBOT-1 )
504:                         BB = H( KBOT-1, KBOT )
505:                         DD = H( KBOT, KBOT )
506:                         CALL DLANV2( AA, BB, CC, DD, WR( KBOT-1 ),
507:      $                               WI( KBOT-1 ), WR( KBOT ),
508:      $                               WI( KBOT ), CS, SN )
509:                         KS = KBOT - 1
510:                      END IF
511:                   END IF
512: *
513:                   IF( KBOT-KS+1.GT.NS ) THEN
514: *
515: *                    ==== Sort the shifts (Helps a little)
516: *                    .    Bubble sort keeps complex conjugate
517: *                    .    pairs together. ====
518: *
519:                      SORTED = .false.
520:                      DO 50 K = KBOT, KS + 1, -1
521:                         IF( SORTED )
522:      $                     GO TO 60
523:                         SORTED = .true.
524:                         DO 40 I = KS, K - 1
525:                            IF( ABS( WR( I ) )+ABS( WI( I ) ).LT.
526:      $                         ABS( WR( I+1 ) )+ABS( WI( I+1 ) ) ) THEN
527:                               SORTED = .false.
528: *
529:                               SWAP = WR( I )
530:                               WR( I ) = WR( I+1 )
531:                               WR( I+1 ) = SWAP
532: *
533:                               SWAP = WI( I )
534:                               WI( I ) = WI( I+1 )
535:                               WI( I+1 ) = SWAP
536:                            END IF
537:    40                   CONTINUE
538:    50                CONTINUE
539:    60                CONTINUE
540:                   END IF
541: *
542: *                 ==== Shuffle shifts into pairs of real shifts
543: *                 .    and pairs of complex conjugate shifts
544: *                 .    assuming complex conjugate shifts are
545: *                 .    already adjacent to one another. (Yes,
546: *                 .    they are.)  ====
547: *
548:                   DO 70 I = KBOT, KS + 2, -2
549:                      IF( WI( I ).NE.-WI( I-1 ) ) THEN
550: *
551:                         SWAP = WR( I )
552:                         WR( I ) = WR( I-1 )
553:                         WR( I-1 ) = WR( I-2 )
554:                         WR( I-2 ) = SWAP
555: *
556:                         SWAP = WI( I )
557:                         WI( I ) = WI( I-1 )
558:                         WI( I-1 ) = WI( I-2 )
559:                         WI( I-2 ) = SWAP
560:                      END IF
561:    70             CONTINUE
562:                END IF
563: *
564: *              ==== If there are only two shifts and both are
565: *              .    real, then use only one.  ====
566: *
567:                IF( KBOT-KS+1.EQ.2 ) THEN
568:                   IF( WI( KBOT ).EQ.ZERO ) THEN
569:                      IF( ABS( WR( KBOT )-H( KBOT, KBOT ) ).LT.
570:      $                   ABS( WR( KBOT-1 )-H( KBOT, KBOT ) ) ) THEN
571:                         WR( KBOT-1 ) = WR( KBOT )
572:                      ELSE
573:                         WR( KBOT ) = WR( KBOT-1 )
574:                      END IF
575:                   END IF
576:                END IF
577: *
578: *              ==== Use up to NS of the the smallest magnatiude
579: *              .    shifts.  If there aren't NS shifts available,
580: *              .    then use them all, possibly dropping one to
581: *              .    make the number of shifts even. ====
582: *
583:                NS = MIN( NS, KBOT-KS+1 )
584:                NS = NS - MOD( NS, 2 )
585:                KS = KBOT - NS + 1
586: *
587: *              ==== Small-bulge multi-shift QR sweep:
588: *              .    split workspace under the subdiagonal into
589: *              .    - a KDU-by-KDU work array U in the lower
590: *              .      left-hand-corner,
591: *              .    - a KDU-by-at-least-KDU-but-more-is-better
592: *              .      (KDU-by-NHo) horizontal work array WH along
593: *              .      the bottom edge,
594: *              .    - and an at-least-KDU-but-more-is-better-by-KDU
595: *              .      (NVE-by-KDU) vertical work WV arrow along
596: *              .      the left-hand-edge. ====
597: *
598:                KDU = 3*NS - 3
599:                KU = N - KDU + 1
600:                KWH = KDU + 1
601:                NHO = ( N-KDU+1-4 ) - ( KDU+1 ) + 1
602:                KWV = KDU + 4
603:                NVE = N - KDU - KWV + 1
604: *
605: *              ==== Small-bulge multi-shift QR sweep ====
606: *
607:                CALL DLAQR5( WANTT, WANTZ, KACC22, N, KTOP, KBOT, NS,
608:      $                      WR( KS ), WI( KS ), H, LDH, ILOZ, IHIZ, Z,
609:      $                      LDZ, WORK, 3, H( KU, 1 ), LDH, NVE,
610:      $                      H( KWV, 1 ), LDH, NHO, H( KU, KWH ), LDH )
611:             END IF
612: *
613: *           ==== Note progress (or the lack of it). ====
614: *
615:             IF( LD.GT.0 ) THEN
616:                NDFL = 1
617:             ELSE
618:                NDFL = NDFL + 1
619:             END IF
620: *
621: *           ==== End of main loop ====
622:    80    CONTINUE
623: *
624: *        ==== Iteration limit exceeded.  Set INFO to show where
625: *        .    the problem occurred and exit. ====
626: *
627:          INFO = KBOT
628:    90    CONTINUE
629:       END IF
630: *
631: *     ==== Return the optimal value of LWORK. ====
632: *
633:       WORK( 1 ) = DBLE( LWKOPT )
634: *
635: *     ==== End of DLAQR4 ====
636: *
637:       END
638: