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