1 *> \brief \b SLAQR4 computes the eigenvalues of a Hessenberg matrix, and optionally the matrices from the Schur decomposition.
3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download SLAQR4 + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/slaqr4.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/slaqr4.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/slaqr4.f">
21 * SUBROUTINE SLAQR4( WANTT, WANTZ, N, ILO, IHI, H, LDH, WR, WI,
22 * ILOZ, IHIZ, Z, LDZ, WORK, LWORK, INFO )
24 * .. Scalar Arguments ..
25 * INTEGER IHI, IHIZ, ILO, ILOZ, INFO, LDH, LDZ, LWORK, N
26 * LOGICAL WANTT, WANTZ
28 * .. Array Arguments ..
29 * REAL H( LDH, * ), WI( * ), WORK( * ), WR( * ),
39 *> SLAQR4 implements one level of recursion for SLAQR0.
40 *> It is a complete implementation of the small bulge multi-shift
41 *> QR algorithm. It may be called by SLAQR0 and, for large enough
42 *> deflation window size, it may be called by SLAQR3. This
43 *> subroutine is identical to SLAQR0 except that it calls SLAQR2
46 *> SLAQR4 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**T, where T is an upper quasi-triangular matrix (the
49 *> Schur form), and Z is the orthogonal matrix of Schur vectors.
51 *> Optionally Z may be postmultiplied into an input orthogonal
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 orthogonal matrix Q: A = Q*H*Q**T = (QZ)*T*(QZ)**T.
63 *> = .TRUE. : the full Schur form T is required;
64 *> = .FALSE.: only eigenvalues are required.
70 *> = .TRUE. : the matrix of Schur vectors Z is required;
71 *> = .FALSE.: Schur vectors are not required.
77 *> The order of the matrix H. N .GE. 0.
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.GT.1,
90 *> H(ILO,ILO-1) is zero. ILO and IHI are normally set by a
91 *> previous call to SGEBAL, and then passed to SGEHRD when the
92 *> matrix output by SGEBAL is reduced to Hessenberg form.
93 *> Otherwise, ILO and IHI should be set to 1 and N,
94 *> respectively. If N.GT.0, then 1.LE.ILO.LE.IHI.LE.N.
95 *> If N = 0, then ILO = 1 and IHI = 0.
100 *> H is REAL array, dimension (LDH,N)
101 *> On entry, the upper Hessenberg matrix H.
102 *> On exit, if INFO = 0 and WANTT is .TRUE., then H contains
103 *> the upper quasi-triangular matrix T from the Schur
104 *> decomposition (the Schur form); 2-by-2 diagonal blocks
105 *> (corresponding to complex conjugate pairs of eigenvalues)
106 *> are returned in standard form, with H(i,i) = H(i+1,i+1)
107 *> and H(i+1,i)*H(i,i+1).LT.0. If INFO = 0 and WANTT is
108 *> .FALSE., then the contents of H are unspecified on exit.
109 *> (The output value of H when INFO.GT.0 is given under the
110 *> description of INFO below.)
112 *> This subroutine may explicitly set H(i,j) = 0 for i.GT.j and
113 *> j = 1, 2, ... ILO-1 or j = IHI+1, IHI+2, ... N.
119 *> The leading dimension of the array H. LDH .GE. max(1,N).
124 *> WR is REAL array, dimension (IHI)
129 *> WI is REAL array, dimension (IHI)
130 *> The real and imaginary parts, respectively, of the computed
131 *> eigenvalues of H(ILO:IHI,ILO:IHI) are stored in WR(ILO:IHI)
132 *> and WI(ILO:IHI). If two eigenvalues are computed as a
133 *> complex conjugate pair, they are stored in consecutive
134 *> elements of WR and WI, say the i-th and (i+1)th, with
135 *> WI(i) .GT. 0 and WI(i+1) .LT. 0. If WANTT is .TRUE., then
136 *> the eigenvalues are stored in the same order as on the
137 *> diagonal of the Schur form returned in H, with
138 *> WR(i) = H(i,i) and, if H(i:i+1,i:i+1) is a 2-by-2 diagonal
139 *> block, WI(i) = sqrt(-H(i+1,i)*H(i,i+1)) and
151 *> Specify the rows of Z to which transformations must be
152 *> applied if WANTZ is .TRUE..
153 *> 1 .LE. ILOZ .LE. ILO; IHI .LE. IHIZ .LE. N.
158 *> Z is REAL array, dimension (LDZ,IHI)
159 *> If WANTZ is .FALSE., then Z is not referenced.
160 *> If WANTZ is .TRUE., then Z(ILO:IHI,ILOZ:IHIZ) is
161 *> replaced by Z(ILO:IHI,ILOZ:IHIZ)*U where U is the
162 *> orthogonal Schur factor of H(ILO:IHI,ILO:IHI).
163 *> (The output value of Z when INFO.GT.0 is given under
164 *> the description of INFO below.)
170 *> The leading dimension of the array Z. if WANTZ is .TRUE.
171 *> then LDZ.GE.MAX(1,IHIZ). Otherwize, LDZ.GE.1.
176 *> WORK is REAL array, dimension LWORK
177 *> On exit, if LWORK = -1, WORK(1) returns an estimate of
178 *> the optimal value for LWORK.
184 *> The dimension of the array WORK. LWORK .GE. max(1,N)
185 *> is sufficient, but LWORK typically as large as 6*N may
186 *> be required for optimal performance. A workspace query
187 *> to determine the optimal workspace size is recommended.
189 *> If LWORK = -1, then SLAQR4 does a workspace query.
190 *> In this case, SLAQR4 checks the input parameters and
191 *> estimates the optimal workspace size for the given
192 *> values of N, ILO and IHI. The estimate is returned
193 *> in WORK(1). No error message related to LWORK is
194 *> issued by XERBLA. Neither H nor Z are accessed.
202 *> = 0: successful exit
203 *> .GT. 0: if INFO = i, SLAQR4 failed to compute all of
204 *> the eigenvalues. Elements 1:ilo-1 and i+1:n of WR
205 *> and WI contain those eigenvalues which have been
206 *> successfully computed. (Failures are rare.)
208 *> If INFO .GT. 0 and WANT is .FALSE., then on exit,
209 *> the remaining unconverged eigenvalues are the eigen-
210 *> values of the upper Hessenberg matrix rows and
211 *> columns ILO through INFO of the final, output
214 *> If INFO .GT. 0 and WANTT is .TRUE., then on exit
216 *> (*) (initial value of H)*U = U*(final value of H)
218 *> where U is a orthogonal matrix. The final
219 *> value of H is upper Hessenberg and triangular in
220 *> rows and columns INFO+1 through IHI.
222 *> If INFO .GT. 0 and WANTZ is .TRUE., then on exit
224 *> (final value of Z(ILO:IHI,ILOZ:IHIZ)
225 *> = (initial value of Z(ILO:IHI,ILOZ:IHIZ)*U
227 *> where U is the orthogonal matrix in (*) (regard-
228 *> less of the value of WANTT.)
230 *> If INFO .GT. 0 and WANTZ is .FALSE., then Z is not
237 *> \author Univ. of Tennessee
238 *> \author Univ. of California Berkeley
239 *> \author Univ. of Colorado Denver
242 *> \date September 2012
244 *> \ingroup realOTHERauxiliary
246 *> \par Contributors:
249 *> Karen Braman and Ralph Byers, Department of Mathematics,
250 *> University of Kansas, USA
255 *> K. Braman, R. Byers and R. Mathias, The Multi-Shift QR
256 *> Algorithm Part I: Maintaining Well Focused Shifts, and Level 3
257 *> Performance, SIAM Journal of Matrix Analysis, volume 23, pages
260 *> K. Braman, R. Byers and R. Mathias, The Multi-Shift QR
261 *> Algorithm Part II: Aggressive Early Deflation, SIAM Journal
262 *> of Matrix Analysis, volume 23, pages 948--973, 2002.
264 * =====================================================================
265 SUBROUTINE SLAQR4( WANTT, WANTZ, N, ILO, IHI, H, LDH, WR, WI,
266 $ ILOZ, IHIZ, Z, LDZ, WORK, LWORK, INFO )
268 * -- LAPACK auxiliary routine (version 3.4.2) --
269 * -- LAPACK is a software package provided by Univ. of Tennessee, --
270 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
273 * .. Scalar Arguments ..
274 INTEGER IHI, IHIZ, ILO, ILOZ, INFO, LDH, LDZ, LWORK, N
277 * .. Array Arguments ..
278 REAL H( LDH, * ), WI( * ), WORK( * ), WR( * ),
282 * ================================================================
286 * ==== Matrices of order NTINY or smaller must be processed by
287 * . SLAHQR because of insufficient subdiagonal scratch space.
288 * . (This is a hard limit.) ====
290 PARAMETER ( NTINY = 11 )
292 * ==== Exceptional deflation windows: try to cure rare
293 * . slow convergence by varying the size of the
294 * . deflation window after KEXNW iterations. ====
296 PARAMETER ( KEXNW = 5 )
298 * ==== Exceptional shifts: try to cure rare slow convergence
299 * . with ad-hoc exceptional shifts every KEXSH iterations.
302 PARAMETER ( KEXSH = 6 )
304 * ==== The constants WILK1 and WILK2 are used to form the
305 * . exceptional shifts. ====
307 PARAMETER ( WILK1 = 0.75e0, WILK2 = -0.4375e0 )
309 PARAMETER ( ZERO = 0.0e0, ONE = 1.0e0 )
311 * .. Local Scalars ..
312 REAL AA, BB, CC, CS, DD, SN, SS, SWAP
313 INTEGER I, INF, IT, ITMAX, K, KACC22, KBOT, KDU, KS,
314 $ KT, KTOP, KU, KV, KWH, KWTOP, KWV, LD, LS,
315 $ LWKOPT, NDEC, NDFL, NH, NHO, NIBBLE, NMIN, NS,
316 $ NSMAX, NSR, NVE, NW, NWMAX, NWR, NWUPBD
320 * .. External Functions ..
327 * .. External Subroutines ..
328 EXTERNAL SLACPY, SLAHQR, SLANV2, SLAQR2, SLAQR5
330 * .. Intrinsic Functions ..
331 INTRINSIC ABS, INT, MAX, MIN, MOD, REAL
333 * .. Executable Statements ..
336 * ==== Quick return for N = 0: nothing to do. ====
343 IF( N.LE.NTINY ) THEN
345 * ==== Tiny matrices must use SLAHQR. ====
349 $ CALL SLAHQR( WANTT, WANTZ, N, ILO, IHI, H, LDH, WR, WI,
350 $ ILOZ, IHIZ, Z, LDZ, INFO )
353 * ==== Use small bulge multi-shift QR with aggressive early
354 * . deflation on larger-than-tiny matrices. ====
356 * ==== Hope for the best. ====
360 * ==== Set up job flags for ILAENV. ====
373 * ==== NWR = recommended deflation window size. At this
374 * . point, N .GT. NTINY = 11, so there is enough
375 * . subdiagonal workspace for NWR.GE.2 as required.
376 * . (In fact, there is enough subdiagonal space for
379 NWR = ILAENV( 13, 'SLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
381 NWR = MIN( IHI-ILO+1, ( N-1 ) / 3, NWR )
383 * ==== NSR = recommended number of simultaneous shifts.
384 * . At this point N .GT. NTINY = 11, so there is at
385 * . enough subdiagonal workspace for NSR to be even
386 * . and greater than or equal to two as required. ====
388 NSR = ILAENV( 15, 'SLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
389 NSR = MIN( NSR, ( N+6 ) / 9, IHI-ILO )
390 NSR = MAX( 2, NSR-MOD( NSR, 2 ) )
392 * ==== Estimate optimal workspace ====
394 * ==== Workspace query call to SLAQR2 ====
396 CALL SLAQR2( WANTT, WANTZ, N, ILO, IHI, NWR+1, H, LDH, ILOZ,
397 $ IHIZ, Z, LDZ, LS, LD, WR, WI, H, LDH, N, H, LDH,
398 $ N, H, LDH, WORK, -1 )
400 * ==== Optimal workspace = MAX(SLAQR5, SLAQR2) ====
402 LWKOPT = MAX( 3*NSR / 2, INT( WORK( 1 ) ) )
404 * ==== Quick return in case of workspace query. ====
406 IF( LWORK.EQ.-1 ) THEN
407 WORK( 1 ) = REAL( LWKOPT )
411 * ==== SLAHQR/SLAQR0 crossover point ====
413 NMIN = ILAENV( 12, 'SLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
414 NMIN = MAX( NTINY, NMIN )
416 * ==== Nibble crossover point ====
418 NIBBLE = ILAENV( 14, 'SLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
419 NIBBLE = MAX( 0, NIBBLE )
421 * ==== Accumulate reflections during ttswp? Use block
422 * . 2-by-2 structure during matrix-matrix multiply? ====
424 KACC22 = ILAENV( 16, 'SLAQR4', JBCMPZ, N, ILO, IHI, LWORK )
425 KACC22 = MAX( 0, KACC22 )
426 KACC22 = MIN( 2, KACC22 )
428 * ==== NWMAX = the largest possible deflation window for
429 * . which there is sufficient workspace. ====
431 NWMAX = MIN( ( N-1 ) / 3, LWORK / 2 )
434 * ==== NSMAX = the Largest number of simultaneous shifts
435 * . for which there is sufficient workspace. ====
437 NSMAX = MIN( ( N+6 ) / 9, 2*LWORK / 3 )
438 NSMAX = NSMAX - MOD( NSMAX, 2 )
440 * ==== NDFL: an iteration count restarted at deflation. ====
444 * ==== ITMAX = iteration limit ====
446 ITMAX = MAX( 30, 2*KEXSH )*MAX( 10, ( IHI-ILO+1 ) )
448 * ==== Last row and column in the active block ====
452 * ==== Main Loop ====
456 * ==== Done when KBOT falls below ILO ====
461 * ==== Locate active block ====
463 DO 10 K = KBOT, ILO + 1, -1
464 IF( H( K, K-1 ).EQ.ZERO )
471 * ==== Select deflation window size:
473 * . If possible and advisable, nibble the entire
474 * . active block. If not, use size MIN(NWR,NWMAX)
475 * . or MIN(NWR+1,NWMAX) depending upon which has
476 * . the smaller corresponding subdiagonal entry
479 * . Exceptional Case:
480 * . If there have been no deflations in KEXNW or
481 * . more iterations, then vary the deflation window
482 * . size. At first, because, larger windows are,
483 * . in general, more powerful than smaller ones,
484 * . rapidly increase the window to the maximum possible.
485 * . Then, gradually reduce the window size. ====
488 NWUPBD = MIN( NH, NWMAX )
489 IF( NDFL.LT.KEXNW ) THEN
490 NW = MIN( NWUPBD, NWR )
492 NW = MIN( NWUPBD, 2*NW )
494 IF( NW.LT.NWMAX ) THEN
495 IF( NW.GE.NH-1 ) THEN
498 KWTOP = KBOT - NW + 1
499 IF( ABS( H( KWTOP, KWTOP-1 ) ).GT.
500 $ ABS( H( KWTOP-1, KWTOP-2 ) ) )NW = NW + 1
503 IF( NDFL.LT.KEXNW ) THEN
505 ELSE IF( NDEC.GE.0 .OR. NW.GE.NWUPBD ) THEN
512 * ==== Aggressive early deflation:
513 * . split workspace under the subdiagonal into
514 * . - an nw-by-nw work array V in the lower
515 * . left-hand-corner,
516 * . - an NW-by-at-least-NW-but-more-is-better
517 * . (NW-by-NHO) horizontal work array along
519 * . - an at-least-NW-but-more-is-better (NHV-by-NW)
520 * . vertical work array along the left-hand-edge.
525 NHO = ( N-NW-1 ) - KT + 1
527 NVE = ( N-NW ) - KWV + 1
529 * ==== Aggressive early deflation ====
531 CALL SLAQR2( WANTT, WANTZ, N, KTOP, KBOT, NW, H, LDH, ILOZ,
532 $ IHIZ, Z, LDZ, LS, LD, WR, WI, H( KV, 1 ), LDH,
533 $ NHO, H( KV, KT ), LDH, NVE, H( KWV, 1 ), LDH,
536 * ==== Adjust KBOT accounting for new deflations. ====
540 * ==== KS points to the shifts. ====
544 * ==== Skip an expensive QR sweep if there is a (partly
545 * . heuristic) reason to expect that many eigenvalues
546 * . will deflate without it. Here, the QR sweep is
547 * . skipped if many eigenvalues have just been deflated
548 * . or if the remaining active block is small.
550 IF( ( LD.EQ.0 ) .OR. ( ( 100*LD.LE.NW*NIBBLE ) .AND. ( KBOT-
551 $ KTOP+1.GT.MIN( NMIN, NWMAX ) ) ) ) THEN
553 * ==== NS = nominal number of simultaneous shifts.
554 * . This may be lowered (slightly) if SLAQR2
555 * . did not provide that many shifts. ====
557 NS = MIN( NSMAX, NSR, MAX( 2, KBOT-KTOP ) )
558 NS = NS - MOD( NS, 2 )
560 * ==== If there have been no deflations
561 * . in a multiple of KEXSH iterations,
562 * . then try exceptional shifts.
563 * . Otherwise use shifts provided by
564 * . SLAQR2 above or from the eigenvalues
565 * . of a trailing principal submatrix. ====
567 IF( MOD( NDFL, KEXSH ).EQ.0 ) THEN
569 DO 30 I = KBOT, MAX( KS+1, KTOP+2 ), -2
570 SS = ABS( H( I, I-1 ) ) + ABS( H( I-1, I-2 ) )
571 AA = WILK1*SS + H( I, I )
575 CALL SLANV2( AA, BB, CC, DD, WR( I-1 ), WI( I-1 ),
576 $ WR( I ), WI( I ), CS, SN )
578 IF( KS.EQ.KTOP ) THEN
579 WR( KS+1 ) = H( KS+1, KS+1 )
581 WR( KS ) = WR( KS+1 )
582 WI( KS ) = WI( KS+1 )
586 * ==== Got NS/2 or fewer shifts? Use SLAHQR
587 * . on a trailing principal submatrix to
588 * . get more. (Since NS.LE.NSMAX.LE.(N+6)/9,
589 * . there is enough space below the subdiagonal
590 * . to fit an NS-by-NS scratch array.) ====
592 IF( KBOT-KS+1.LE.NS / 2 ) THEN
595 CALL SLACPY( 'A', NS, NS, H( KS, KS ), LDH,
597 CALL SLAHQR( .false., .false., NS, 1, NS,
598 $ H( KT, 1 ), LDH, WR( KS ), WI( KS ),
599 $ 1, 1, ZDUM, 1, INF )
602 * ==== In case of a rare QR failure use
603 * . eigenvalues of the trailing 2-by-2
604 * . principal submatrix. ====
606 IF( KS.GE.KBOT ) THEN
607 AA = H( KBOT-1, KBOT-1 )
608 CC = H( KBOT, KBOT-1 )
609 BB = H( KBOT-1, KBOT )
611 CALL SLANV2( AA, BB, CC, DD, WR( KBOT-1 ),
612 $ WI( KBOT-1 ), WR( KBOT ),
613 $ WI( KBOT ), CS, SN )
618 IF( KBOT-KS+1.GT.NS ) THEN
620 * ==== Sort the shifts (Helps a little)
621 * . Bubble sort keeps complex conjugate
622 * . pairs together. ====
625 DO 50 K = KBOT, KS + 1, -1
630 IF( ABS( WR( I ) )+ABS( WI( I ) ).LT.
631 $ ABS( WR( I+1 ) )+ABS( WI( I+1 ) ) ) THEN
647 * ==== Shuffle shifts into pairs of real shifts
648 * . and pairs of complex conjugate shifts
649 * . assuming complex conjugate shifts are
650 * . already adjacent to one another. (Yes,
653 DO 70 I = KBOT, KS + 2, -2
654 IF( WI( I ).NE.-WI( I-1 ) ) THEN
658 WR( I-1 ) = WR( I-2 )
663 WI( I-1 ) = WI( I-2 )
669 * ==== If there are only two shifts and both are
670 * . real, then use only one. ====
672 IF( KBOT-KS+1.EQ.2 ) THEN
673 IF( WI( KBOT ).EQ.ZERO ) THEN
674 IF( ABS( WR( KBOT )-H( KBOT, KBOT ) ).LT.
675 $ ABS( WR( KBOT-1 )-H( KBOT, KBOT ) ) ) THEN
676 WR( KBOT-1 ) = WR( KBOT )
678 WR( KBOT ) = WR( KBOT-1 )
683 * ==== Use up to NS of the the smallest magnatiude
684 * . shifts. If there aren't NS shifts available,
685 * . then use them all, possibly dropping one to
686 * . make the number of shifts even. ====
688 NS = MIN( NS, KBOT-KS+1 )
689 NS = NS - MOD( NS, 2 )
692 * ==== Small-bulge multi-shift QR sweep:
693 * . split workspace under the subdiagonal into
694 * . - a KDU-by-KDU work array U in the lower
695 * . left-hand-corner,
696 * . - a KDU-by-at-least-KDU-but-more-is-better
697 * . (KDU-by-NHo) horizontal work array WH along
699 * . - and an at-least-KDU-but-more-is-better-by-KDU
700 * . (NVE-by-KDU) vertical work WV arrow along
701 * . the left-hand-edge. ====
706 NHO = ( N-KDU+1-4 ) - ( KDU+1 ) + 1
708 NVE = N - KDU - KWV + 1
710 * ==== Small-bulge multi-shift QR sweep ====
712 CALL SLAQR5( WANTT, WANTZ, KACC22, N, KTOP, KBOT, NS,
713 $ WR( KS ), WI( KS ), H, LDH, ILOZ, IHIZ, Z,
714 $ LDZ, WORK, 3, H( KU, 1 ), LDH, NVE,
715 $ H( KWV, 1 ), LDH, NHO, H( KU, KWH ), LDH )
718 * ==== Note progress (or the lack of it). ====
726 * ==== End of main loop ====
729 * ==== Iteration limit exceeded. Set INFO to show where
730 * . the problem occurred and exit. ====
736 * ==== Return the optimal value of LWORK. ====
738 WORK( 1 ) = REAL( LWKOPT )
740 * ==== End of SLAQR4 ====