0001:       SUBROUTINE DHGEQZ( JOB, COMPQ, COMPZ, N, ILO, IHI, H, LDH, T, LDT,
0002:      $                   ALPHAR, ALPHAI, BETA, Q, LDQ, Z, LDZ, WORK,
0003:      $                   LWORK, INFO )
0004: *
0005: *  -- LAPACK routine (version 3.2.1)                                  --
0006: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
0007: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
0008: *  -- April 2009                                                      --
0009: *
0010: *     .. Scalar Arguments ..
0011:       CHARACTER          COMPQ, COMPZ, JOB
0012:       INTEGER            IHI, ILO, INFO, LDH, LDQ, LDT, LDZ, LWORK, N
0013: *     ..
0014: *     .. Array Arguments ..
0015:       DOUBLE PRECISION   ALPHAI( * ), ALPHAR( * ), BETA( * ),
0016:      $                   H( LDH, * ), Q( LDQ, * ), T( LDT, * ),
0017:      $                   WORK( * ), Z( LDZ, * )
0018: *     ..
0019: *
0020: *  Purpose
0021: *  =======
0022: *
0023: *  DHGEQZ computes the eigenvalues of a real matrix pair (H,T),
0024: *  where H is an upper Hessenberg matrix and T is upper triangular,
0025: *  using the double-shift QZ method.
0026: *  Matrix pairs of this type are produced by the reduction to
0027: *  generalized upper Hessenberg form of a real matrix pair (A,B):
0028: *
0029: *     A = Q1*H*Z1**T,  B = Q1*T*Z1**T,
0030: *
0031: *  as computed by DGGHRD.
0032: *
0033: *  If JOB='S', then the Hessenberg-triangular pair (H,T) is
0034: *  also reduced to generalized Schur form,
0035: *  
0036: *     H = Q*S*Z**T,  T = Q*P*Z**T,
0037: *  
0038: *  where Q and Z are orthogonal matrices, P is an upper triangular
0039: *  matrix, and S is a quasi-triangular matrix with 1-by-1 and 2-by-2
0040: *  diagonal blocks.
0041: *
0042: *  The 1-by-1 blocks correspond to real eigenvalues of the matrix pair
0043: *  (H,T) and the 2-by-2 blocks correspond to complex conjugate pairs of
0044: *  eigenvalues.
0045: *
0046: *  Additionally, the 2-by-2 upper triangular diagonal blocks of P
0047: *  corresponding to 2-by-2 blocks of S are reduced to positive diagonal
0048: *  form, i.e., if S(j+1,j) is non-zero, then P(j+1,j) = P(j,j+1) = 0,
0049: *  P(j,j) > 0, and P(j+1,j+1) > 0.
0050: *
0051: *  Optionally, the orthogonal matrix Q from the generalized Schur
0052: *  factorization may be postmultiplied into an input matrix Q1, and the
0053: *  orthogonal matrix Z may be postmultiplied into an input matrix Z1.
0054: *  If Q1 and Z1 are the orthogonal matrices from DGGHRD that reduced
0055: *  the matrix pair (A,B) to generalized upper Hessenberg form, then the
0056: *  output matrices Q1*Q and Z1*Z are the orthogonal factors from the
0057: *  generalized Schur factorization of (A,B):
0058: *
0059: *     A = (Q1*Q)*S*(Z1*Z)**T,  B = (Q1*Q)*P*(Z1*Z)**T.
0060: *  
0061: *  To avoid overflow, eigenvalues of the matrix pair (H,T) (equivalently,
0062: *  of (A,B)) are computed as a pair of values (alpha,beta), where alpha is
0063: *  complex and beta real.
0064: *  If beta is nonzero, lambda = alpha / beta is an eigenvalue of the
0065: *  generalized nonsymmetric eigenvalue problem (GNEP)
0066: *     A*x = lambda*B*x
0067: *  and if alpha is nonzero, mu = beta / alpha is an eigenvalue of the
0068: *  alternate form of the GNEP
0069: *     mu*A*y = B*y.
0070: *  Real eigenvalues can be read directly from the generalized Schur
0071: *  form: 
0072: *    alpha = S(i,i), beta = P(i,i).
0073: *
0074: *  Ref: C.B. Moler & G.W. Stewart, "An Algorithm for Generalized Matrix
0075: *       Eigenvalue Problems", SIAM J. Numer. Anal., 10(1973),
0076: *       pp. 241--256.
0077: *
0078: *  Arguments
0079: *  =========
0080: *
0081: *  JOB     (input) CHARACTER*1
0082: *          = 'E': Compute eigenvalues only;
0083: *          = 'S': Compute eigenvalues and the Schur form. 
0084: *
0085: *  COMPQ   (input) CHARACTER*1
0086: *          = 'N': Left Schur vectors (Q) are not computed;
0087: *          = 'I': Q is initialized to the unit matrix and the matrix Q
0088: *                 of left Schur vectors of (H,T) is returned;
0089: *          = 'V': Q must contain an orthogonal matrix Q1 on entry and
0090: *                 the product Q1*Q is returned.
0091: *
0092: *  COMPZ   (input) CHARACTER*1
0093: *          = 'N': Right Schur vectors (Z) are not computed;
0094: *          = 'I': Z is initialized to the unit matrix and the matrix Z
0095: *                 of right Schur vectors of (H,T) is returned;
0096: *          = 'V': Z must contain an orthogonal matrix Z1 on entry and
0097: *                 the product Z1*Z is returned.
0098: *
0099: *  N       (input) INTEGER
0100: *          The order of the matrices H, T, Q, and Z.  N >= 0.
0101: *
0102: *  ILO     (input) INTEGER
0103: *  IHI     (input) INTEGER
0104: *          ILO and IHI mark the rows and columns of H which are in
0105: *          Hessenberg form.  It is assumed that A is already upper
0106: *          triangular in rows and columns 1:ILO-1 and IHI+1:N.
0107: *          If N > 0, 1 <= ILO <= IHI <= N; if N = 0, ILO=1 and IHI=0.
0108: *
0109: *  H       (input/output) DOUBLE PRECISION array, dimension (LDH, N)
0110: *          On entry, the N-by-N upper Hessenberg matrix H.
0111: *          On exit, if JOB = 'S', H contains the upper quasi-triangular
0112: *          matrix S from the generalized Schur factorization;
0113: *          2-by-2 diagonal blocks (corresponding to complex conjugate
0114: *          pairs of eigenvalues) are returned in standard form, with
0115: *          H(i,i) = H(i+1,i+1) and H(i+1,i)*H(i,i+1) < 0.
0116: *          If JOB = 'E', the diagonal blocks of H match those of S, but
0117: *          the rest of H is unspecified.
0118: *
0119: *  LDH     (input) INTEGER
0120: *          The leading dimension of the array H.  LDH >= max( 1, N ).
0121: *
0122: *  T       (input/output) DOUBLE PRECISION array, dimension (LDT, N)
0123: *          On entry, the N-by-N upper triangular matrix T.
0124: *          On exit, if JOB = 'S', T contains the upper triangular
0125: *          matrix P from the generalized Schur factorization;
0126: *          2-by-2 diagonal blocks of P corresponding to 2-by-2 blocks of S
0127: *          are reduced to positive diagonal form, i.e., if H(j+1,j) is
0128: *          non-zero, then T(j+1,j) = T(j,j+1) = 0, T(j,j) > 0, and
0129: *          T(j+1,j+1) > 0.
0130: *          If JOB = 'E', the diagonal blocks of T match those of P, but
0131: *          the rest of T is unspecified.
0132: *
0133: *  LDT     (input) INTEGER
0134: *          The leading dimension of the array T.  LDT >= max( 1, N ).
0135: *
0136: *  ALPHAR  (output) DOUBLE PRECISION array, dimension (N)
0137: *          The real parts of each scalar alpha defining an eigenvalue
0138: *          of GNEP.
0139: *
0140: *  ALPHAI  (output) DOUBLE PRECISION array, dimension (N)
0141: *          The imaginary parts of each scalar alpha defining an
0142: *          eigenvalue of GNEP.
0143: *          If ALPHAI(j) is zero, then the j-th eigenvalue is real; if
0144: *          positive, then the j-th and (j+1)-st eigenvalues are a
0145: *          complex conjugate pair, with ALPHAI(j+1) = -ALPHAI(j).
0146: *
0147: *  BETA    (output) DOUBLE PRECISION array, dimension (N)
0148: *          The scalars beta that define the eigenvalues of GNEP.
0149: *          Together, the quantities alpha = (ALPHAR(j),ALPHAI(j)) and
0150: *          beta = BETA(j) represent the j-th eigenvalue of the matrix
0151: *          pair (A,B), in one of the forms lambda = alpha/beta or
0152: *          mu = beta/alpha.  Since either lambda or mu may overflow,
0153: *          they should not, in general, be computed.
0154: *
0155: *  Q       (input/output) DOUBLE PRECISION array, dimension (LDQ, N)
0156: *          On entry, if COMPZ = 'V', the orthogonal matrix Q1 used in
0157: *          the reduction of (A,B) to generalized Hessenberg form.
0158: *          On exit, if COMPZ = 'I', the orthogonal matrix of left Schur
0159: *          vectors of (H,T), and if COMPZ = 'V', the orthogonal matrix
0160: *          of left Schur vectors of (A,B).
0161: *          Not referenced if COMPZ = 'N'.
0162: *
0163: *  LDQ     (input) INTEGER
0164: *          The leading dimension of the array Q.  LDQ >= 1.
0165: *          If COMPQ='V' or 'I', then LDQ >= N.
0166: *
0167: *  Z       (input/output) DOUBLE PRECISION array, dimension (LDZ, N)
0168: *          On entry, if COMPZ = 'V', the orthogonal matrix Z1 used in
0169: *          the reduction of (A,B) to generalized Hessenberg form.
0170: *          On exit, if COMPZ = 'I', the orthogonal matrix of
0171: *          right Schur vectors of (H,T), and if COMPZ = 'V', the
0172: *          orthogonal matrix of right Schur vectors of (A,B).
0173: *          Not referenced if COMPZ = 'N'.
0174: *
0175: *  LDZ     (input) INTEGER
0176: *          The leading dimension of the array Z.  LDZ >= 1.
0177: *          If COMPZ='V' or 'I', then LDZ >= N.
0178: *
0179: *  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK))
0180: *          On exit, if INFO >= 0, WORK(1) returns the optimal LWORK.
0181: *
0182: *  LWORK   (input) INTEGER
0183: *          The dimension of the array WORK.  LWORK >= max(1,N).
0184: *
0185: *          If LWORK = -1, then a workspace query is assumed; the routine
0186: *          only calculates the optimal size of the WORK array, returns
0187: *          this value as the first entry of the WORK array, and no error
0188: *          message related to LWORK is issued by XERBLA.
0189: *
0190: *  INFO    (output) INTEGER
0191: *          = 0: successful exit
0192: *          < 0: if INFO = -i, the i-th argument had an illegal value
0193: *          = 1,...,N: the QZ iteration did not converge.  (H,T) is not
0194: *                     in Schur form, but ALPHAR(i), ALPHAI(i), and
0195: *                     BETA(i), i=INFO+1,...,N should be correct.
0196: *          = N+1,...,2*N: the shift calculation failed.  (H,T) is not
0197: *                     in Schur form, but ALPHAR(i), ALPHAI(i), and
0198: *                     BETA(i), i=INFO-N+1,...,N should be correct.
0199: *
0200: *  Further Details
0201: *  ===============
0202: *
0203: *  Iteration counters:
0204: *
0205: *  JITER  -- counts iterations.
0206: *  IITER  -- counts iterations run since ILAST was last
0207: *            changed.  This is therefore reset only when a 1-by-1 or
0208: *            2-by-2 block deflates off the bottom.
0209: *
0210: *  =====================================================================
0211: *
0212: *     .. Parameters ..
0213: *    $                     SAFETY = 1.0E+0 )
0214:       DOUBLE PRECISION   HALF, ZERO, ONE, SAFETY
0215:       PARAMETER          ( HALF = 0.5D+0, ZERO = 0.0D+0, ONE = 1.0D+0,
0216:      $                   SAFETY = 1.0D+2 )
0217: *     ..
0218: *     .. Local Scalars ..
0219:       LOGICAL            ILAZR2, ILAZRO, ILPIVT, ILQ, ILSCHR, ILZ,
0220:      $                   LQUERY
0221:       INTEGER            ICOMPQ, ICOMPZ, IFIRST, IFRSTM, IITER, ILAST,
0222:      $                   ILASTM, IN, ISCHUR, ISTART, J, JC, JCH, JITER,
0223:      $                   JR, MAXIT
0224:       DOUBLE PRECISION   A11, A12, A1I, A1R, A21, A22, A2I, A2R, AD11,
0225:      $                   AD11L, AD12, AD12L, AD21, AD21L, AD22, AD22L,
0226:      $                   AD32L, AN, ANORM, ASCALE, ATOL, B11, B1A, B1I,
0227:      $                   B1R, B22, B2A, B2I, B2R, BN, BNORM, BSCALE,
0228:      $                   BTOL, C, C11I, C11R, C12, C21, C22I, C22R, CL,
0229:      $                   CQ, CR, CZ, ESHIFT, S, S1, S1INV, S2, SAFMAX,
0230:      $                   SAFMIN, SCALE, SL, SQI, SQR, SR, SZI, SZR, T1,
0231:      $                   TAU, TEMP, TEMP2, TEMPI, TEMPR, U1, U12, U12L,
0232:      $                   U2, ULP, VS, W11, W12, W21, W22, WABS, WI, WR,
0233:      $                   WR2
0234: *     ..
0235: *     .. Local Arrays ..
0236:       DOUBLE PRECISION   V( 3 )
0237: *     ..
0238: *     .. External Functions ..
0239:       LOGICAL            LSAME
0240:       DOUBLE PRECISION   DLAMCH, DLANHS, DLAPY2, DLAPY3
0241:       EXTERNAL           LSAME, DLAMCH, DLANHS, DLAPY2, DLAPY3
0242: *     ..
0243: *     .. External Subroutines ..
0244:       EXTERNAL           DLAG2, DLARFG, DLARTG, DLASET, DLASV2, DROT,
0245:      $                   XERBLA
0246: *     ..
0247: *     .. Intrinsic Functions ..
0248:       INTRINSIC          ABS, DBLE, MAX, MIN, SQRT
0249: *     ..
0250: *     .. Executable Statements ..
0251: *
0252: *     Decode JOB, COMPQ, COMPZ
0253: *
0254:       IF( LSAME( JOB, 'E' ) ) THEN
0255:          ILSCHR = .FALSE.
0256:          ISCHUR = 1
0257:       ELSE IF( LSAME( JOB, 'S' ) ) THEN
0258:          ILSCHR = .TRUE.
0259:          ISCHUR = 2
0260:       ELSE
0261:          ISCHUR = 0
0262:       END IF
0263: *
0264:       IF( LSAME( COMPQ, 'N' ) ) THEN
0265:          ILQ = .FALSE.
0266:          ICOMPQ = 1
0267:       ELSE IF( LSAME( COMPQ, 'V' ) ) THEN
0268:          ILQ = .TRUE.
0269:          ICOMPQ = 2
0270:       ELSE IF( LSAME( COMPQ, 'I' ) ) THEN
0271:          ILQ = .TRUE.
0272:          ICOMPQ = 3
0273:       ELSE
0274:          ICOMPQ = 0
0275:       END IF
0276: *
0277:       IF( LSAME( COMPZ, 'N' ) ) THEN
0278:          ILZ = .FALSE.
0279:          ICOMPZ = 1
0280:       ELSE IF( LSAME( COMPZ, 'V' ) ) THEN
0281:          ILZ = .TRUE.
0282:          ICOMPZ = 2
0283:       ELSE IF( LSAME( COMPZ, 'I' ) ) THEN
0284:          ILZ = .TRUE.
0285:          ICOMPZ = 3
0286:       ELSE
0287:          ICOMPZ = 0
0288:       END IF
0289: *
0290: *     Check Argument Values
0291: *
0292:       INFO = 0
0293:       WORK( 1 ) = MAX( 1, N )
0294:       LQUERY = ( LWORK.EQ.-1 )
0295:       IF( ISCHUR.EQ.0 ) THEN
0296:          INFO = -1
0297:       ELSE IF( ICOMPQ.EQ.0 ) THEN
0298:          INFO = -2
0299:       ELSE IF( ICOMPZ.EQ.0 ) THEN
0300:          INFO = -3
0301:       ELSE IF( N.LT.0 ) THEN
0302:          INFO = -4
0303:       ELSE IF( ILO.LT.1 ) THEN
0304:          INFO = -5
0305:       ELSE IF( IHI.GT.N .OR. IHI.LT.ILO-1 ) THEN
0306:          INFO = -6
0307:       ELSE IF( LDH.LT.N ) THEN
0308:          INFO = -8
0309:       ELSE IF( LDT.LT.N ) THEN
0310:          INFO = -10
0311:       ELSE IF( LDQ.LT.1 .OR. ( ILQ .AND. LDQ.LT.N ) ) THEN
0312:          INFO = -15
0313:       ELSE IF( LDZ.LT.1 .OR. ( ILZ .AND. LDZ.LT.N ) ) THEN
0314:          INFO = -17
0315:       ELSE IF( LWORK.LT.MAX( 1, N ) .AND. .NOT.LQUERY ) THEN
0316:          INFO = -19
0317:       END IF
0318:       IF( INFO.NE.0 ) THEN
0319:          CALL XERBLA( 'DHGEQZ', -INFO )
0320:          RETURN
0321:       ELSE IF( LQUERY ) THEN
0322:          RETURN
0323:       END IF
0324: *
0325: *     Quick return if possible
0326: *
0327:       IF( N.LE.0 ) THEN
0328:          WORK( 1 ) = DBLE( 1 )
0329:          RETURN
0330:       END IF
0331: *
0332: *     Initialize Q and Z
0333: *
0334:       IF( ICOMPQ.EQ.3 )
0335:      $   CALL DLASET( 'Full', N, N, ZERO, ONE, Q, LDQ )
0336:       IF( ICOMPZ.EQ.3 )
0337:      $   CALL DLASET( 'Full', N, N, ZERO, ONE, Z, LDZ )
0338: *
0339: *     Machine Constants
0340: *
0341:       IN = IHI + 1 - ILO
0342:       SAFMIN = DLAMCH( 'S' )
0343:       SAFMAX = ONE / SAFMIN
0344:       ULP = DLAMCH( 'E' )*DLAMCH( 'B' )
0345:       ANORM = DLANHS( 'F', IN, H( ILO, ILO ), LDH, WORK )
0346:       BNORM = DLANHS( 'F', IN, T( ILO, ILO ), LDT, WORK )
0347:       ATOL = MAX( SAFMIN, ULP*ANORM )
0348:       BTOL = MAX( SAFMIN, ULP*BNORM )
0349:       ASCALE = ONE / MAX( SAFMIN, ANORM )
0350:       BSCALE = ONE / MAX( SAFMIN, BNORM )
0351: *
0352: *     Set Eigenvalues IHI+1:N
0353: *
0354:       DO 30 J = IHI + 1, N
0355:          IF( T( J, J ).LT.ZERO ) THEN
0356:             IF( ILSCHR ) THEN
0357:                DO 10 JR = 1, J
0358:                   H( JR, J ) = -H( JR, J )
0359:                   T( JR, J ) = -T( JR, J )
0360:    10          CONTINUE
0361:             ELSE
0362:                H( J, J ) = -H( J, J )
0363:                T( J, J ) = -T( J, J )
0364:             END IF
0365:             IF( ILZ ) THEN
0366:                DO 20 JR = 1, N
0367:                   Z( JR, J ) = -Z( JR, J )
0368:    20          CONTINUE
0369:             END IF
0370:          END IF
0371:          ALPHAR( J ) = H( J, J )
0372:          ALPHAI( J ) = ZERO
0373:          BETA( J ) = T( J, J )
0374:    30 CONTINUE
0375: *
0376: *     If IHI < ILO, skip QZ steps
0377: *
0378:       IF( IHI.LT.ILO )
0379:      $   GO TO 380
0380: *
0381: *     MAIN QZ ITERATION LOOP
0382: *
0383: *     Initialize dynamic indices
0384: *
0385: *     Eigenvalues ILAST+1:N have been found.
0386: *        Column operations modify rows IFRSTM:whatever.
0387: *        Row operations modify columns whatever:ILASTM.
0388: *
0389: *     If only eigenvalues are being computed, then
0390: *        IFRSTM is the row of the last splitting row above row ILAST;
0391: *        this is always at least ILO.
0392: *     IITER counts iterations since the last eigenvalue was found,
0393: *        to tell when to use an extraordinary shift.
0394: *     MAXIT is the maximum number of QZ sweeps allowed.
0395: *
0396:       ILAST = IHI
0397:       IF( ILSCHR ) THEN
0398:          IFRSTM = 1
0399:          ILASTM = N
0400:       ELSE
0401:          IFRSTM = ILO
0402:          ILASTM = IHI
0403:       END IF
0404:       IITER = 0
0405:       ESHIFT = ZERO
0406:       MAXIT = 30*( IHI-ILO+1 )
0407: *
0408:       DO 360 JITER = 1, MAXIT
0409: *
0410: *        Split the matrix if possible.
0411: *
0412: *        Two tests:
0413: *           1: H(j,j-1)=0  or  j=ILO
0414: *           2: T(j,j)=0
0415: *
0416:          IF( ILAST.EQ.ILO ) THEN
0417: *
0418: *           Special case: j=ILAST
0419: *
0420:             GO TO 80
0421:          ELSE
0422:             IF( ABS( H( ILAST, ILAST-1 ) ).LE.ATOL ) THEN
0423:                H( ILAST, ILAST-1 ) = ZERO
0424:                GO TO 80
0425:             END IF
0426:          END IF
0427: *
0428:          IF( ABS( T( ILAST, ILAST ) ).LE.BTOL ) THEN
0429:             T( ILAST, ILAST ) = ZERO
0430:             GO TO 70
0431:          END IF
0432: *
0433: *        General case: j<ILAST
0434: *
0435:          DO 60 J = ILAST - 1, ILO, -1
0436: *
0437: *           Test 1: for H(j,j-1)=0 or j=ILO
0438: *
0439:             IF( J.EQ.ILO ) THEN
0440:                ILAZRO = .TRUE.
0441:             ELSE
0442:                IF( ABS( H( J, J-1 ) ).LE.ATOL ) THEN
0443:                   H( J, J-1 ) = ZERO
0444:                   ILAZRO = .TRUE.
0445:                ELSE
0446:                   ILAZRO = .FALSE.
0447:                END IF
0448:             END IF
0449: *
0450: *           Test 2: for T(j,j)=0
0451: *
0452:             IF( ABS( T( J, J ) ).LT.BTOL ) THEN
0453:                T( J, J ) = ZERO
0454: *
0455: *              Test 1a: Check for 2 consecutive small subdiagonals in A
0456: *
0457:                ILAZR2 = .FALSE.
0458:                IF( .NOT.ILAZRO ) THEN
0459:                   TEMP = ABS( H( J, J-1 ) )
0460:                   TEMP2 = ABS( H( J, J ) )
0461:                   TEMPR = MAX( TEMP, TEMP2 )
0462:                   IF( TEMPR.LT.ONE .AND. TEMPR.NE.ZERO ) THEN
0463:                      TEMP = TEMP / TEMPR
0464:                      TEMP2 = TEMP2 / TEMPR
0465:                   END IF
0466:                   IF( TEMP*( ASCALE*ABS( H( J+1, J ) ) ).LE.TEMP2*
0467:      $                ( ASCALE*ATOL ) )ILAZR2 = .TRUE.
0468:                END IF
0469: *
0470: *              If both tests pass (1 & 2), i.e., the leading diagonal
0471: *              element of B in the block is zero, split a 1x1 block off
0472: *              at the top. (I.e., at the J-th row/column) The leading
0473: *              diagonal element of the remainder can also be zero, so
0474: *              this may have to be done repeatedly.
0475: *
0476:                IF( ILAZRO .OR. ILAZR2 ) THEN
0477:                   DO 40 JCH = J, ILAST - 1
0478:                      TEMP = H( JCH, JCH )
0479:                      CALL DLARTG( TEMP, H( JCH+1, JCH ), C, S,
0480:      $                            H( JCH, JCH ) )
0481:                      H( JCH+1, JCH ) = ZERO
0482:                      CALL DROT( ILASTM-JCH, H( JCH, JCH+1 ), LDH,
0483:      $                          H( JCH+1, JCH+1 ), LDH, C, S )
0484:                      CALL DROT( ILASTM-JCH, T( JCH, JCH+1 ), LDT,
0485:      $                          T( JCH+1, JCH+1 ), LDT, C, S )
0486:                      IF( ILQ )
0487:      $                  CALL DROT( N, Q( 1, JCH ), 1, Q( 1, JCH+1 ), 1,
0488:      $                             C, S )
0489:                      IF( ILAZR2 )
0490:      $                  H( JCH, JCH-1 ) = H( JCH, JCH-1 )*C
0491:                      ILAZR2 = .FALSE.
0492:                      IF( ABS( T( JCH+1, JCH+1 ) ).GE.BTOL ) THEN
0493:                         IF( JCH+1.GE.ILAST ) THEN
0494:                            GO TO 80
0495:                         ELSE
0496:                            IFIRST = JCH + 1
0497:                            GO TO 110
0498:                         END IF
0499:                      END IF
0500:                      T( JCH+1, JCH+1 ) = ZERO
0501:    40             CONTINUE
0502:                   GO TO 70
0503:                ELSE
0504: *
0505: *                 Only test 2 passed -- chase the zero to T(ILAST,ILAST)
0506: *                 Then process as in the case T(ILAST,ILAST)=0
0507: *
0508:                   DO 50 JCH = J, ILAST - 1
0509:                      TEMP = T( JCH, JCH+1 )
0510:                      CALL DLARTG( TEMP, T( JCH+1, JCH+1 ), C, S,
0511:      $                            T( JCH, JCH+1 ) )
0512:                      T( JCH+1, JCH+1 ) = ZERO
0513:                      IF( JCH.LT.ILASTM-1 )
0514:      $                  CALL DROT( ILASTM-JCH-1, T( JCH, JCH+2 ), LDT,
0515:      $                             T( JCH+1, JCH+2 ), LDT, C, S )
0516:                      CALL DROT( ILASTM-JCH+2, H( JCH, JCH-1 ), LDH,
0517:      $                          H( JCH+1, JCH-1 ), LDH, C, S )
0518:                      IF( ILQ )
0519:      $                  CALL DROT( N, Q( 1, JCH ), 1, Q( 1, JCH+1 ), 1,
0520:      $                             C, S )
0521:                      TEMP = H( JCH+1, JCH )
0522:                      CALL DLARTG( TEMP, H( JCH+1, JCH-1 ), C, S,
0523:      $                            H( JCH+1, JCH ) )
0524:                      H( JCH+1, JCH-1 ) = ZERO
0525:                      CALL DROT( JCH+1-IFRSTM, H( IFRSTM, JCH ), 1,
0526:      $                          H( IFRSTM, JCH-1 ), 1, C, S )
0527:                      CALL DROT( JCH-IFRSTM, T( IFRSTM, JCH ), 1,
0528:      $                          T( IFRSTM, JCH-1 ), 1, C, S )
0529:                      IF( ILZ )
0530:      $                  CALL DROT( N, Z( 1, JCH ), 1, Z( 1, JCH-1 ), 1,
0531:      $                             C, S )
0532:    50             CONTINUE
0533:                   GO TO 70
0534:                END IF
0535:             ELSE IF( ILAZRO ) THEN
0536: *
0537: *              Only test 1 passed -- work on J:ILAST
0538: *
0539:                IFIRST = J
0540:                GO TO 110
0541:             END IF
0542: *
0543: *           Neither test passed -- try next J
0544: *
0545:    60    CONTINUE
0546: *
0547: *        (Drop-through is "impossible")
0548: *
0549:          INFO = N + 1
0550:          GO TO 420
0551: *
0552: *        T(ILAST,ILAST)=0 -- clear H(ILAST,ILAST-1) to split off a
0553: *        1x1 block.
0554: *
0555:    70    CONTINUE
0556:          TEMP = H( ILAST, ILAST )
0557:          CALL DLARTG( TEMP, H( ILAST, ILAST-1 ), C, S,
0558:      $                H( ILAST, ILAST ) )
0559:          H( ILAST, ILAST-1 ) = ZERO
0560:          CALL DROT( ILAST-IFRSTM, H( IFRSTM, ILAST ), 1,
0561:      $              H( IFRSTM, ILAST-1 ), 1, C, S )
0562:          CALL DROT( ILAST-IFRSTM, T( IFRSTM, ILAST ), 1,
0563:      $              T( IFRSTM, ILAST-1 ), 1, C, S )
0564:          IF( ILZ )
0565:      $      CALL DROT( N, Z( 1, ILAST ), 1, Z( 1, ILAST-1 ), 1, C, S )
0566: *
0567: *        H(ILAST,ILAST-1)=0 -- Standardize B, set ALPHAR, ALPHAI,
0568: *                              and BETA
0569: *
0570:    80    CONTINUE
0571:          IF( T( ILAST, ILAST ).LT.ZERO ) THEN
0572:             IF( ILSCHR ) THEN
0573:                DO 90 J = IFRSTM, ILAST
0574:                   H( J, ILAST ) = -H( J, ILAST )
0575:                   T( J, ILAST ) = -T( J, ILAST )
0576:    90          CONTINUE
0577:             ELSE
0578:                H( ILAST, ILAST ) = -H( ILAST, ILAST )
0579:                T( ILAST, ILAST ) = -T( ILAST, ILAST )
0580:             END IF
0581:             IF( ILZ ) THEN
0582:                DO 100 J = 1, N
0583:                   Z( J, ILAST ) = -Z( J, ILAST )
0584:   100          CONTINUE
0585:             END IF
0586:          END IF
0587:          ALPHAR( ILAST ) = H( ILAST, ILAST )
0588:          ALPHAI( ILAST ) = ZERO
0589:          BETA( ILAST ) = T( ILAST, ILAST )
0590: *
0591: *        Go to next block -- exit if finished.
0592: *
0593:          ILAST = ILAST - 1
0594:          IF( ILAST.LT.ILO )
0595:      $      GO TO 380
0596: *
0597: *        Reset counters
0598: *
0599:          IITER = 0
0600:          ESHIFT = ZERO
0601:          IF( .NOT.ILSCHR ) THEN
0602:             ILASTM = ILAST
0603:             IF( IFRSTM.GT.ILAST )
0604:      $         IFRSTM = ILO
0605:          END IF
0606:          GO TO 350
0607: *
0608: *        QZ step
0609: *
0610: *        This iteration only involves rows/columns IFIRST:ILAST. We
0611: *        assume IFIRST < ILAST, and that the diagonal of B is non-zero.
0612: *
0613:   110    CONTINUE
0614:          IITER = IITER + 1
0615:          IF( .NOT.ILSCHR ) THEN
0616:             IFRSTM = IFIRST
0617:          END IF
0618: *
0619: *        Compute single shifts.
0620: *
0621: *        At this point, IFIRST < ILAST, and the diagonal elements of
0622: *        T(IFIRST:ILAST,IFIRST,ILAST) are larger than BTOL (in
0623: *        magnitude)
0624: *
0625:          IF( ( IITER / 10 )*10.EQ.IITER ) THEN
0626: *
0627: *           Exceptional shift.  Chosen for no particularly good reason.
0628: *           (Single shift only.)
0629: *
0630:             IF( ( DBLE( MAXIT )*SAFMIN )*ABS( H( ILAST-1, ILAST ) ).LT.
0631:      $          ABS( T( ILAST-1, ILAST-1 ) ) ) THEN
0632:                ESHIFT = ESHIFT + H( ILAST-1, ILAST ) /
0633:      $                  T( ILAST-1, ILAST-1 )
0634:             ELSE
0635:                ESHIFT = ESHIFT + ONE / ( SAFMIN*DBLE( MAXIT ) )
0636:             END IF
0637:             S1 = ONE
0638:             WR = ESHIFT
0639: *
0640:          ELSE
0641: *
0642: *           Shifts based on the generalized eigenvalues of the
0643: *           bottom-right 2x2 block of A and B. The first eigenvalue
0644: *           returned by DLAG2 is the Wilkinson shift (AEP p.512),
0645: *
0646:             CALL DLAG2( H( ILAST-1, ILAST-1 ), LDH,
0647:      $                  T( ILAST-1, ILAST-1 ), LDT, SAFMIN*SAFETY, S1,
0648:      $                  S2, WR, WR2, WI )
0649: *
0650:             TEMP = MAX( S1, SAFMIN*MAX( ONE, ABS( WR ), ABS( WI ) ) )
0651:             IF( WI.NE.ZERO )
0652:      $         GO TO 200
0653:          END IF
0654: *
0655: *        Fiddle with shift to avoid overflow
0656: *
0657:          TEMP = MIN( ASCALE, ONE )*( HALF*SAFMAX )
0658:          IF( S1.GT.TEMP ) THEN
0659:             SCALE = TEMP / S1
0660:          ELSE
0661:             SCALE = ONE
0662:          END IF
0663: *
0664:          TEMP = MIN( BSCALE, ONE )*( HALF*SAFMAX )
0665:          IF( ABS( WR ).GT.TEMP )
0666:      $      SCALE = MIN( SCALE, TEMP / ABS( WR ) )
0667:          S1 = SCALE*S1
0668:          WR = SCALE*WR
0669: *
0670: *        Now check for two consecutive small subdiagonals.
0671: *
0672:          DO 120 J = ILAST - 1, IFIRST + 1, -1
0673:             ISTART = J
0674:             TEMP = ABS( S1*H( J, J-1 ) )
0675:             TEMP2 = ABS( S1*H( J, J )-WR*T( J, J ) )
0676:             TEMPR = MAX( TEMP, TEMP2 )
0677:             IF( TEMPR.LT.ONE .AND. TEMPR.NE.ZERO ) THEN
0678:                TEMP = TEMP / TEMPR
0679:                TEMP2 = TEMP2 / TEMPR
0680:             END IF
0681:             IF( ABS( ( ASCALE*H( J+1, J ) )*TEMP ).LE.( ASCALE*ATOL )*
0682:      $          TEMP2 )GO TO 130
0683:   120    CONTINUE
0684: *
0685:          ISTART = IFIRST
0686:   130    CONTINUE
0687: *
0688: *        Do an implicit single-shift QZ sweep.
0689: *
0690: *        Initial Q
0691: *
0692:          TEMP = S1*H( ISTART, ISTART ) - WR*T( ISTART, ISTART )
0693:          TEMP2 = S1*H( ISTART+1, ISTART )
0694:          CALL DLARTG( TEMP, TEMP2, C, S, TEMPR )
0695: *
0696: *        Sweep
0697: *
0698:          DO 190 J = ISTART, ILAST - 1
0699:             IF( J.GT.ISTART ) THEN
0700:                TEMP = H( J, J-1 )
0701:                CALL DLARTG( TEMP, H( J+1, J-1 ), C, S, H( J, J-1 ) )
0702:                H( J+1, J-1 ) = ZERO
0703:             END IF
0704: *
0705:             DO 140 JC = J, ILASTM
0706:                TEMP = C*H( J, JC ) + S*H( J+1, JC )
0707:                H( J+1, JC ) = -S*H( J, JC ) + C*H( J+1, JC )
0708:                H( J, JC ) = TEMP
0709:                TEMP2 = C*T( J, JC ) + S*T( J+1, JC )
0710:                T( J+1, JC ) = -S*T( J, JC ) + C*T( J+1, JC )
0711:                T( J, JC ) = TEMP2
0712:   140       CONTINUE
0713:             IF( ILQ ) THEN
0714:                DO 150 JR = 1, N
0715:                   TEMP = C*Q( JR, J ) + S*Q( JR, J+1 )
0716:                   Q( JR, J+1 ) = -S*Q( JR, J ) + C*Q( JR, J+1 )
0717:                   Q( JR, J ) = TEMP
0718:   150          CONTINUE
0719:             END IF
0720: *
0721:             TEMP = T( J+1, J+1 )
0722:             CALL DLARTG( TEMP, T( J+1, J ), C, S, T( J+1, J+1 ) )
0723:             T( J+1, J ) = ZERO
0724: *
0725:             DO 160 JR = IFRSTM, MIN( J+2, ILAST )
0726:                TEMP = C*H( JR, J+1 ) + S*H( JR, J )
0727:                H( JR, J ) = -S*H( JR, J+1 ) + C*H( JR, J )
0728:                H( JR, J+1 ) = TEMP
0729:   160       CONTINUE
0730:             DO 170 JR = IFRSTM, J
0731:                TEMP = C*T( JR, J+1 ) + S*T( JR, J )
0732:                T( JR, J ) = -S*T( JR, J+1 ) + C*T( JR, J )
0733:                T( JR, J+1 ) = TEMP
0734:   170       CONTINUE
0735:             IF( ILZ ) THEN
0736:                DO 180 JR = 1, N
0737:                   TEMP = C*Z( JR, J+1 ) + S*Z( JR, J )
0738:                   Z( JR, J ) = -S*Z( JR, J+1 ) + C*Z( JR, J )
0739:                   Z( JR, J+1 ) = TEMP
0740:   180          CONTINUE
0741:             END IF
0742:   190    CONTINUE
0743: *
0744:          GO TO 350
0745: *
0746: *        Use Francis double-shift
0747: *
0748: *        Note: the Francis double-shift should work with real shifts,
0749: *              but only if the block is at least 3x3.
0750: *              This code may break if this point is reached with
0751: *              a 2x2 block with real eigenvalues.
0752: *
0753:   200    CONTINUE
0754:          IF( IFIRST+1.EQ.ILAST ) THEN
0755: *
0756: *           Special case -- 2x2 block with complex eigenvectors
0757: *
0758: *           Step 1: Standardize, that is, rotate so that
0759: *
0760: *                       ( B11  0  )
0761: *                   B = (         )  with B11 non-negative.
0762: *                       (  0  B22 )
0763: *
0764:             CALL DLASV2( T( ILAST-1, ILAST-1 ), T( ILAST-1, ILAST ),
0765:      $                   T( ILAST, ILAST ), B22, B11, SR, CR, SL, CL )
0766: *
0767:             IF( B11.LT.ZERO ) THEN
0768:                CR = -CR
0769:                SR = -SR
0770:                B11 = -B11
0771:                B22 = -B22
0772:             END IF
0773: *
0774:             CALL DROT( ILASTM+1-IFIRST, H( ILAST-1, ILAST-1 ), LDH,
0775:      $                 H( ILAST, ILAST-1 ), LDH, CL, SL )
0776:             CALL DROT( ILAST+1-IFRSTM, H( IFRSTM, ILAST-1 ), 1,
0777:      $                 H( IFRSTM, ILAST ), 1, CR, SR )
0778: *
0779:             IF( ILAST.LT.ILASTM )
0780:      $         CALL DROT( ILASTM-ILAST, T( ILAST-1, ILAST+1 ), LDT,
0781:      $                    T( ILAST, ILAST+1 ), LDT, CL, SL )
0782:             IF( IFRSTM.LT.ILAST-1 )
0783:      $         CALL DROT( IFIRST-IFRSTM, T( IFRSTM, ILAST-1 ), 1,
0784:      $                    T( IFRSTM, ILAST ), 1, CR, SR )
0785: *
0786:             IF( ILQ )
0787:      $         CALL DROT( N, Q( 1, ILAST-1 ), 1, Q( 1, ILAST ), 1, CL,
0788:      $                    SL )
0789:             IF( ILZ )
0790:      $         CALL DROT( N, Z( 1, ILAST-1 ), 1, Z( 1, ILAST ), 1, CR,
0791:      $                    SR )
0792: *
0793:             T( ILAST-1, ILAST-1 ) = B11
0794:             T( ILAST-1, ILAST ) = ZERO
0795:             T( ILAST, ILAST-1 ) = ZERO
0796:             T( ILAST, ILAST ) = B22
0797: *
0798: *           If B22 is negative, negate column ILAST
0799: *
0800:             IF( B22.LT.ZERO ) THEN
0801:                DO 210 J = IFRSTM, ILAST
0802:                   H( J, ILAST ) = -H( J, ILAST )
0803:                   T( J, ILAST ) = -T( J, ILAST )
0804:   210          CONTINUE
0805: *
0806:                IF( ILZ ) THEN
0807:                   DO 220 J = 1, N
0808:                      Z( J, ILAST ) = -Z( J, ILAST )
0809:   220             CONTINUE
0810:                END IF
0811:             END IF
0812: *
0813: *           Step 2: Compute ALPHAR, ALPHAI, and BETA (see refs.)
0814: *
0815: *           Recompute shift
0816: *
0817:             CALL DLAG2( H( ILAST-1, ILAST-1 ), LDH,
0818:      $                  T( ILAST-1, ILAST-1 ), LDT, SAFMIN*SAFETY, S1,
0819:      $                  TEMP, WR, TEMP2, WI )
0820: *
0821: *           If standardization has perturbed the shift onto real line,
0822: *           do another (real single-shift) QR step.
0823: *
0824:             IF( WI.EQ.ZERO )
0825:      $         GO TO 350
0826:             S1INV = ONE / S1
0827: *
0828: *           Do EISPACK (QZVAL) computation of alpha and beta
0829: *
0830:             A11 = H( ILAST-1, ILAST-1 )
0831:             A21 = H( ILAST, ILAST-1 )
0832:             A12 = H( ILAST-1, ILAST )
0833:             A22 = H( ILAST, ILAST )
0834: *
0835: *           Compute complex Givens rotation on right
0836: *           (Assume some element of C = (sA - wB) > unfl )
0837: *                            __
0838: *           (sA - wB) ( CZ   -SZ )
0839: *                     ( SZ    CZ )
0840: *
0841:             C11R = S1*A11 - WR*B11
0842:             C11I = -WI*B11
0843:             C12 = S1*A12
0844:             C21 = S1*A21
0845:             C22R = S1*A22 - WR*B22
0846:             C22I = -WI*B22
0847: *
0848:             IF( ABS( C11R )+ABS( C11I )+ABS( C12 ).GT.ABS( C21 )+
0849:      $          ABS( C22R )+ABS( C22I ) ) THEN
0850:                T1 = DLAPY3( C12, C11R, C11I )
0851:                CZ = C12 / T1
0852:                SZR = -C11R / T1
0853:                SZI = -C11I / T1
0854:             ELSE
0855:                CZ = DLAPY2( C22R, C22I )
0856:                IF( CZ.LE.SAFMIN ) THEN
0857:                   CZ = ZERO
0858:                   SZR = ONE
0859:                   SZI = ZERO
0860:                ELSE
0861:                   TEMPR = C22R / CZ
0862:                   TEMPI = C22I / CZ
0863:                   T1 = DLAPY2( CZ, C21 )
0864:                   CZ = CZ / T1
0865:                   SZR = -C21*TEMPR / T1
0866:                   SZI = C21*TEMPI / T1
0867:                END IF
0868:             END IF
0869: *
0870: *           Compute Givens rotation on left
0871: *
0872: *           (  CQ   SQ )
0873: *           (  __      )  A or B
0874: *           ( -SQ   CQ )
0875: *
0876:             AN = ABS( A11 ) + ABS( A12 ) + ABS( A21 ) + ABS( A22 )
0877:             BN = ABS( B11 ) + ABS( B22 )
0878:             WABS = ABS( WR ) + ABS( WI )
0879:             IF( S1*AN.GT.WABS*BN ) THEN
0880:                CQ = CZ*B11
0881:                SQR = SZR*B22
0882:                SQI = -SZI*B22
0883:             ELSE
0884:                A1R = CZ*A11 + SZR*A12
0885:                A1I = SZI*A12
0886:                A2R = CZ*A21 + SZR*A22
0887:                A2I = SZI*A22
0888:                CQ = DLAPY2( A1R, A1I )
0889:                IF( CQ.LE.SAFMIN ) THEN
0890:                   CQ = ZERO
0891:                   SQR = ONE
0892:                   SQI = ZERO
0893:                ELSE
0894:                   TEMPR = A1R / CQ
0895:                   TEMPI = A1I / CQ
0896:                   SQR = TEMPR*A2R + TEMPI*A2I
0897:                   SQI = TEMPI*A2R - TEMPR*A2I
0898:                END IF
0899:             END IF
0900:             T1 = DLAPY3( CQ, SQR, SQI )
0901:             CQ = CQ / T1
0902:             SQR = SQR / T1
0903:             SQI = SQI / T1
0904: *
0905: *           Compute diagonal elements of QBZ
0906: *
0907:             TEMPR = SQR*SZR - SQI*SZI
0908:             TEMPI = SQR*SZI + SQI*SZR
0909:             B1R = CQ*CZ*B11 + TEMPR*B22
0910:             B1I = TEMPI*B22
0911:             B1A = DLAPY2( B1R, B1I )
0912:             B2R = CQ*CZ*B22 + TEMPR*B11
0913:             B2I = -TEMPI*B11
0914:             B2A = DLAPY2( B2R, B2I )
0915: *
0916: *           Normalize so beta > 0, and Im( alpha1 ) > 0
0917: *
0918:             BETA( ILAST-1 ) = B1A
0919:             BETA( ILAST ) = B2A
0920:             ALPHAR( ILAST-1 ) = ( WR*B1A )*S1INV
0921:             ALPHAI( ILAST-1 ) = ( WI*B1A )*S1INV
0922:             ALPHAR( ILAST ) = ( WR*B2A )*S1INV
0923:             ALPHAI( ILAST ) = -( WI*B2A )*S1INV
0924: *
0925: *           Step 3: Go to next block -- exit if finished.
0926: *
0927:             ILAST = IFIRST - 1
0928:             IF( ILAST.LT.ILO )
0929:      $         GO TO 380
0930: *
0931: *           Reset counters
0932: *
0933:             IITER = 0
0934:             ESHIFT = ZERO
0935:             IF( .NOT.ILSCHR ) THEN
0936:                ILASTM = ILAST
0937:                IF( IFRSTM.GT.ILAST )
0938:      $            IFRSTM = ILO
0939:             END IF
0940:             GO TO 350
0941:          ELSE
0942: *
0943: *           Usual case: 3x3 or larger block, using Francis implicit
0944: *                       double-shift
0945: *
0946: *                                    2
0947: *           Eigenvalue equation is  w  - c w + d = 0,
0948: *
0949: *                                         -1 2        -1
0950: *           so compute 1st column of  (A B  )  - c A B   + d
0951: *           using the formula in QZIT (from EISPACK)
0952: *
0953: *           We assume that the block is at least 3x3
0954: *
0955:             AD11 = ( ASCALE*H( ILAST-1, ILAST-1 ) ) /
0956:      $             ( BSCALE*T( ILAST-1, ILAST-1 ) )
0957:             AD21 = ( ASCALE*H( ILAST, ILAST-1 ) ) /
0958:      $             ( BSCALE*T( ILAST-1, ILAST-1 ) )
0959:             AD12 = ( ASCALE*H( ILAST-1, ILAST ) ) /
0960:      $             ( BSCALE*T( ILAST, ILAST ) )
0961:             AD22 = ( ASCALE*H( ILAST, ILAST ) ) /
0962:      $             ( BSCALE*T( ILAST, ILAST ) )
0963:             U12 = T( ILAST-1, ILAST ) / T( ILAST, ILAST )
0964:             AD11L = ( ASCALE*H( IFIRST, IFIRST ) ) /
0965:      $              ( BSCALE*T( IFIRST, IFIRST ) )
0966:             AD21L = ( ASCALE*H( IFIRST+1, IFIRST ) ) /
0967:      $              ( BSCALE*T( IFIRST, IFIRST ) )
0968:             AD12L = ( ASCALE*H( IFIRST, IFIRST+1 ) ) /
0969:      $              ( BSCALE*T( IFIRST+1, IFIRST+1 ) )
0970:             AD22L = ( ASCALE*H( IFIRST+1, IFIRST+1 ) ) /
0971:      $              ( BSCALE*T( IFIRST+1, IFIRST+1 ) )
0972:             AD32L = ( ASCALE*H( IFIRST+2, IFIRST+1 ) ) /
0973:      $              ( BSCALE*T( IFIRST+1, IFIRST+1 ) )
0974:             U12L = T( IFIRST, IFIRST+1 ) / T( IFIRST+1, IFIRST+1 )
0975: *
0976:             V( 1 ) = ( AD11-AD11L )*( AD22-AD11L ) - AD12*AD21 +
0977:      $               AD21*U12*AD11L + ( AD12L-AD11L*U12L )*AD21L
0978:             V( 2 ) = ( ( AD22L-AD11L )-AD21L*U12L-( AD11-AD11L )-
0979:      $               ( AD22-AD11L )+AD21*U12 )*AD21L
0980:             V( 3 ) = AD32L*AD21L
0981: *
0982:             ISTART = IFIRST
0983: *
0984:             CALL DLARFG( 3, V( 1 ), V( 2 ), 1, TAU )
0985:             V( 1 ) = ONE
0986: *
0987: *           Sweep
0988: *
0989:             DO 290 J = ISTART, ILAST - 2
0990: *
0991: *              All but last elements: use 3x3 Householder transforms.
0992: *
0993: *              Zero (j-1)st column of A
0994: *
0995:                IF( J.GT.ISTART ) THEN
0996:                   V( 1 ) = H( J, J-1 )
0997:                   V( 2 ) = H( J+1, J-1 )
0998:                   V( 3 ) = H( J+2, J-1 )
0999: *
1000:                   CALL DLARFG( 3, H( J, J-1 ), V( 2 ), 1, TAU )
1001:                   V( 1 ) = ONE
1002:                   H( J+1, J-1 ) = ZERO
1003:                   H( J+2, J-1 ) = ZERO
1004:                END IF
1005: *
1006:                DO 230 JC = J, ILASTM
1007:                   TEMP = TAU*( H( J, JC )+V( 2 )*H( J+1, JC )+V( 3 )*
1008:      $                   H( J+2, JC ) )
1009:                   H( J, JC ) = H( J, JC ) - TEMP
1010:                   H( J+1, JC ) = H( J+1, JC ) - TEMP*V( 2 )
1011:                   H( J+2, JC ) = H( J+2, JC ) - TEMP*V( 3 )
1012:                   TEMP2 = TAU*( T( J, JC )+V( 2 )*T( J+1, JC )+V( 3 )*
1013:      $                    T( J+2, JC ) )
1014:                   T( J, JC ) = T( J, JC ) - TEMP2
1015:                   T( J+1, JC ) = T( J+1, JC ) - TEMP2*V( 2 )
1016:                   T( J+2, JC ) = T( J+2, JC ) - TEMP2*V( 3 )
1017:   230          CONTINUE
1018:                IF( ILQ ) THEN
1019:                   DO 240 JR = 1, N
1020:                      TEMP = TAU*( Q( JR, J )+V( 2 )*Q( JR, J+1 )+V( 3 )*
1021:      $                      Q( JR, J+2 ) )
1022:                      Q( JR, J ) = Q( JR, J ) - TEMP
1023:                      Q( JR, J+1 ) = Q( JR, J+1 ) - TEMP*V( 2 )
1024:                      Q( JR, J+2 ) = Q( JR, J+2 ) - TEMP*V( 3 )
1025:   240             CONTINUE
1026:                END IF
1027: *
1028: *              Zero j-th column of B (see DLAGBC for details)
1029: *
1030: *              Swap rows to pivot
1031: *
1032:                ILPIVT = .FALSE.
1033:                TEMP = MAX( ABS( T( J+1, J+1 ) ), ABS( T( J+1, J+2 ) ) )
1034:                TEMP2 = MAX( ABS( T( J+2, J+1 ) ), ABS( T( J+2, J+2 ) ) )
1035:                IF( MAX( TEMP, TEMP2 ).LT.SAFMIN ) THEN
1036:                   SCALE = ZERO
1037:                   U1 = ONE
1038:                   U2 = ZERO
1039:                   GO TO 250
1040:                ELSE IF( TEMP.GE.TEMP2 ) THEN
1041:                   W11 = T( J+1, J+1 )
1042:                   W21 = T( J+2, J+1 )
1043:                   W12 = T( J+1, J+2 )
1044:                   W22 = T( J+2, J+2 )
1045:                   U1 = T( J+1, J )
1046:                   U2 = T( J+2, J )
1047:                ELSE
1048:                   W21 = T( J+1, J+1 )
1049:                   W11 = T( J+2, J+1 )
1050:                   W22 = T( J+1, J+2 )
1051:                   W12 = T( J+2, J+2 )
1052:                   U2 = T( J+1, J )
1053:                   U1 = T( J+2, J )
1054:                END IF
1055: *
1056: *              Swap columns if nec.
1057: *
1058:                IF( ABS( W12 ).GT.ABS( W11 ) ) THEN
1059:                   ILPIVT = .TRUE.
1060:                   TEMP = W12
1061:                   TEMP2 = W22
1062:                   W12 = W11
1063:                   W22 = W21
1064:                   W11 = TEMP
1065:                   W21 = TEMP2
1066:                END IF
1067: *
1068: *              LU-factor
1069: *
1070:                TEMP = W21 / W11
1071:                U2 = U2 - TEMP*U1
1072:                W22 = W22 - TEMP*W12
1073:                W21 = ZERO
1074: *
1075: *              Compute SCALE
1076: *
1077:                SCALE = ONE
1078:                IF( ABS( W22 ).LT.SAFMIN ) THEN
1079:                   SCALE = ZERO
1080:                   U2 = ONE
1081:                   U1 = -W12 / W11
1082:                   GO TO 250
1083:                END IF
1084:                IF( ABS( W22 ).LT.ABS( U2 ) )
1085:      $            SCALE = ABS( W22 / U2 )
1086:                IF( ABS( W11 ).LT.ABS( U1 ) )
1087:      $            SCALE = MIN( SCALE, ABS( W11 / U1 ) )
1088: *
1089: *              Solve
1090: *
1091:                U2 = ( SCALE*U2 ) / W22
1092:                U1 = ( SCALE*U1-W12*U2 ) / W11
1093: *
1094:   250          CONTINUE
1095:                IF( ILPIVT ) THEN
1096:                   TEMP = U2
1097:                   U2 = U1
1098:                   U1 = TEMP
1099:                END IF
1100: *
1101: *              Compute Householder Vector
1102: *
1103:                T1 = SQRT( SCALE**2+U1**2+U2**2 )
1104:                TAU = ONE + SCALE / T1
1105:                VS = -ONE / ( SCALE+T1 )
1106:                V( 1 ) = ONE
1107:                V( 2 ) = VS*U1
1108:                V( 3 ) = VS*U2
1109: *
1110: *              Apply transformations from the right.
1111: *
1112:                DO 260 JR = IFRSTM, MIN( J+3, ILAST )
1113:                   TEMP = TAU*( H( JR, J )+V( 2 )*H( JR, J+1 )+V( 3 )*
1114:      $                   H( JR, J+2 ) )
1115:                   H( JR, J ) = H( JR, J ) - TEMP
1116:                   H( JR, J+1 ) = H( JR, J+1 ) - TEMP*V( 2 )
1117:                   H( JR, J+2 ) = H( JR, J+2 ) - TEMP*V( 3 )
1118:   260          CONTINUE
1119:                DO 270 JR = IFRSTM, J + 2
1120:                   TEMP = TAU*( T( JR, J )+V( 2 )*T( JR, J+1 )+V( 3 )*
1121:      $                   T( JR, J+2 ) )
1122:                   T( JR, J ) = T( JR, J ) - TEMP
1123:                   T( JR, J+1 ) = T( JR, J+1 ) - TEMP*V( 2 )
1124:                   T( JR, J+2 ) = T( JR, J+2 ) - TEMP*V( 3 )
1125:   270          CONTINUE
1126:                IF( ILZ ) THEN
1127:                   DO 280 JR = 1, N
1128:                      TEMP = TAU*( Z( JR, J )+V( 2 )*Z( JR, J+1 )+V( 3 )*
1129:      $                      Z( JR, J+2 ) )
1130:                      Z( JR, J ) = Z( JR, J ) - TEMP
1131:                      Z( JR, J+1 ) = Z( JR, J+1 ) - TEMP*V( 2 )
1132:                      Z( JR, J+2 ) = Z( JR, J+2 ) - TEMP*V( 3 )
1133:   280             CONTINUE
1134:                END IF
1135:                T( J+1, J ) = ZERO
1136:                T( J+2, J ) = ZERO
1137:   290       CONTINUE
1138: *
1139: *           Last elements: Use Givens rotations
1140: *
1141: *           Rotations from the left
1142: *
1143:             J = ILAST - 1
1144:             TEMP = H( J, J-1 )
1145:             CALL DLARTG( TEMP, H( J+1, J-1 ), C, S, H( J, J-1 ) )
1146:             H( J+1, J-1 ) = ZERO
1147: *
1148:             DO 300 JC = J, ILASTM
1149:                TEMP = C*H( J, JC ) + S*H( J+1, JC )
1150:                H( J+1, JC ) = -S*H( J, JC ) + C*H( J+1, JC )
1151:                H( J, JC ) = TEMP
1152:                TEMP2 = C*T( J, JC ) + S*T( J+1, JC )
1153:                T( J+1, JC ) = -S*T( J, JC ) + C*T( J+1, JC )
1154:                T( J, JC ) = TEMP2
1155:   300       CONTINUE
1156:             IF( ILQ ) THEN
1157:                DO 310 JR = 1, N
1158:                   TEMP = C*Q( JR, J ) + S*Q( JR, J+1 )
1159:                   Q( JR, J+1 ) = -S*Q( JR, J ) + C*Q( JR, J+1 )
1160:                   Q( JR, J ) = TEMP
1161:   310          CONTINUE
1162:             END IF
1163: *
1164: *           Rotations from the right.
1165: *
1166:             TEMP = T( J+1, J+1 )
1167:             CALL DLARTG( TEMP, T( J+1, J ), C, S, T( J+1, J+1 ) )
1168:             T( J+1, J ) = ZERO
1169: *
1170:             DO 320 JR = IFRSTM, ILAST
1171:                TEMP = C*H( JR, J+1 ) + S*H( JR, J )
1172:                H( JR, J ) = -S*H( JR, J+1 ) + C*H( JR, J )
1173:                H( JR, J+1 ) = TEMP
1174:   320       CONTINUE
1175:             DO 330 JR = IFRSTM, ILAST - 1
1176:                TEMP = C*T( JR, J+1 ) + S*T( JR, J )
1177:                T( JR, J ) = -S*T( JR, J+1 ) + C*T( JR, J )
1178:                T( JR, J+1 ) = TEMP
1179:   330       CONTINUE
1180:             IF( ILZ ) THEN
1181:                DO 340 JR = 1, N
1182:                   TEMP = C*Z( JR, J+1 ) + S*Z( JR, J )
1183:                   Z( JR, J ) = -S*Z( JR, J+1 ) + C*Z( JR, J )
1184:                   Z( JR, J+1 ) = TEMP
1185:   340          CONTINUE
1186:             END IF
1187: *
1188: *           End of Double-Shift code
1189: *
1190:          END IF
1191: *
1192:          GO TO 350
1193: *
1194: *        End of iteration loop
1195: *
1196:   350    CONTINUE
1197:   360 CONTINUE
1198: *
1199: *     Drop-through = non-convergence
1200: *
1201:       INFO = ILAST
1202:       GO TO 420
1203: *
1204: *     Successful completion of all QZ steps
1205: *
1206:   380 CONTINUE
1207: *
1208: *     Set Eigenvalues 1:ILO-1
1209: *
1210:       DO 410 J = 1, ILO - 1
1211:          IF( T( J, J ).LT.ZERO ) THEN
1212:             IF( ILSCHR ) THEN
1213:                DO 390 JR = 1, J
1214:                   H( JR, J ) = -H( JR, J )
1215:                   T( JR, J ) = -T( JR, J )
1216:   390          CONTINUE
1217:             ELSE
1218:                H( J, J ) = -H( J, J )
1219:                T( J, J ) = -T( J, J )
1220:             END IF
1221:             IF( ILZ ) THEN
1222:                DO 400 JR = 1, N
1223:                   Z( JR, J ) = -Z( JR, J )
1224:   400          CONTINUE
1225:             END IF
1226:          END IF
1227:          ALPHAR( J ) = H( J, J )
1228:          ALPHAI( J ) = ZERO
1229:          BETA( J ) = T( J, J )
1230:   410 CONTINUE
1231: *
1232: *     Normal Termination
1233: *
1234:       INFO = 0
1235: *
1236: *     Exit (other than argument error) -- return optimal workspace size
1237: *
1238:   420 CONTINUE
1239:       WORK( 1 ) = DBLE( N )
1240:       RETURN
1241: *
1242: *     End of DHGEQZ
1243: *
1244:       END
1245: