SUBROUTINE ZHGEQZ( JOB, COMPQ, COMPZ, N, ILO, IHI, H, LDH, T, LDT,
$ ALPHA, BETA, Q, LDQ, Z, LDZ, WORK, LWORK,
$ RWORK, INFO )
CHARACTER COMPQ, COMPZ, JOB
INTEGER IHI, ILO, INFO, LDH, LDQ, LDT, LDZ, LWORK, N
DOUBLE PRECISION RWORK( * )
COMPLEX*16 ALPHA( * ), BETA( * ), H( LDH, * ),
$ Q( LDQ, * ), T( LDT, * ), WORK( * ),
$ Z( LDZ, * )
COMPLEX*16 CZERO, CONE
PARAMETER ( CZERO = ( 0.0D+0, 0.0D+0 ),
$ CONE = ( 1.0D+0, 0.0D+0 ) )
DOUBLE PRECISION ZERO, ONE
PARAMETER ( ZERO = 0.0D+0, ONE = 1.0D+0 )
DOUBLE PRECISION HALF
PARAMETER ( HALF = 0.5D+0 )
LOGICAL ILAZR2, ILAZRO, ILQ, ILSCHR, ILZ, LQUERY
INTEGER ICOMPQ, ICOMPZ, IFIRST, IFRSTM, IITER, ILAST,
$ ILASTM, IN, ISCHUR, ISTART, J, JC, JCH, JITER,
$ JR, MAXIT
DOUBLE PRECISION ABSB, ANORM, ASCALE, ATOL, BNORM, BSCALE, BTOL,
$ C, SAFMIN, TEMP, TEMP2, TEMPR, ULP
COMPLEX*16 ABI22, AD11, AD12, AD21, AD22, CTEMP, CTEMP2,
$ CTEMP3, ESHIFT, RTDISC, S, SHIFT, SIGNBC, T1,
$ U12, X
LOGICAL LSAME
DOUBLE PRECISION DLAMCH, ZLANHS
EXTERNAL LSAME, DLAMCH, ZLANHS
EXTERNAL XERBLA, ZLARTG, ZLASET, ZROT, ZSCAL
INTRINSIC ABS, DBLE, DCMPLX, DCONJG, DIMAG, MAX, MIN,
$ SQRT
DOUBLE PRECISION ABS1
ABS1( X ) = ABS( DBLE( X ) ) + ABS( DIMAG( X ) )
IF( LSAME( JOB, 'E' ) ) THEN
ILSCHR = .FALSE.
ISCHUR = 1
ELSE IF( LSAME( JOB, 'S' ) ) THEN
ILSCHR = .TRUE.
ISCHUR = 2
ELSE
ISCHUR = 0
END IF
IF( LSAME( COMPQ, 'N' ) ) THEN
ILQ = .FALSE.
ICOMPQ = 1
ELSE IF( LSAME( COMPQ, 'V' ) ) THEN
ILQ = .TRUE.
ICOMPQ = 2
ELSE IF( LSAME( COMPQ, 'I' ) ) THEN
ILQ = .TRUE.
ICOMPQ = 3
ELSE
ICOMPQ = 0
END IF
IF( LSAME( COMPZ, 'N' ) ) THEN
ILZ = .FALSE.
ICOMPZ = 1
ELSE IF( LSAME( COMPZ, 'V' ) ) THEN
ILZ = .TRUE.
ICOMPZ = 2
ELSE IF( LSAME( COMPZ, 'I' ) ) THEN
ILZ = .TRUE.
ICOMPZ = 3
ELSE
ICOMPZ = 0
END IF
INFO = 0
WORK( 1 ) = MAX( 1, N )
LQUERY = ( LWORK.EQ.-1 )
IF( ISCHUR.EQ.0 ) THEN
INFO = -1
ELSE IF( ICOMPQ.EQ.0 ) THEN
INFO = -2
ELSE IF( ICOMPZ.EQ.0 ) THEN
INFO = -3
ELSE IF( N.LT.0 ) THEN
INFO = -4
ELSE IF( ILO.LT.1 ) THEN
INFO = -5
ELSE IF( IHI.GT.N .OR. IHI.LT.ILO-1 ) THEN
INFO = -6
ELSE IF( LDH.LT.N ) THEN
INFO = -8
ELSE IF( LDT.LT.N ) THEN
INFO = -10
ELSE IF( LDQ.LT.1 .OR. ( ILQ .AND. LDQ.LT.N ) ) THEN
INFO = -14
ELSE IF( LDZ.LT.1 .OR. ( ILZ .AND. LDZ.LT.N ) ) THEN
INFO = -16
ELSE IF( LWORK.LT.MAX( 1, N ) .AND. .NOT.LQUERY ) THEN
INFO = -18
END IF
IF( INFO.NE.0 ) THEN
CALL XERBLA( 'ZHGEQZ', -INFO )
RETURN
ELSE IF( LQUERY ) THEN
RETURN
END IF
IF( N.LE.0 ) THEN
WORK( 1 ) = DCMPLX( 1 )
RETURN
END IF
IF( ICOMPQ.EQ.3 )
$ CALL ZLASET( 'Full', N, N, CZERO, CONE, Q, LDQ )
IF( ICOMPZ.EQ.3 )
$ CALL ZLASET( 'Full', N, N, CZERO, CONE, Z, LDZ )
IN = IHI + 1 - ILO
SAFMIN = DLAMCH( 'S' )
ULP = DLAMCH( 'E' )*DLAMCH( 'B' )
ANORM = ZLANHS( 'F', IN, H( ILO, ILO ), LDH, RWORK )
BNORM = ZLANHS( 'F', IN, T( ILO, ILO ), LDT, RWORK )
ATOL = MAX( SAFMIN, ULP*ANORM )
BTOL = MAX( SAFMIN, ULP*BNORM )
ASCALE = ONE / MAX( SAFMIN, ANORM )
BSCALE = ONE / MAX( SAFMIN, BNORM )
DO 10 J = IHI + 1, N
ABSB = ABS( T( J, J ) )
IF( ABSB.GT.SAFMIN ) THEN
SIGNBC = DCONJG( T( J, J ) / ABSB )
T( J, J ) = ABSB
IF( ILSCHR ) THEN
CALL ZSCAL( J-1, SIGNBC, T( 1, J ), 1 )
CALL ZSCAL( J, SIGNBC, H( 1, J ), 1 )
ELSE
H( J, J ) = H( J, J )*SIGNBC
END IF
IF( ILZ )
$ CALL ZSCAL( N, SIGNBC, Z( 1, J ), 1 )
ELSE
T( J, J ) = CZERO
END IF
ALPHA( J ) = H( J, J )
BETA( J ) = T( J, J )
10 CONTINUE
IF( IHI.LT.ILO )
$ GO TO 190
ILAST = IHI
IF( ILSCHR ) THEN
IFRSTM = 1
ILASTM = N
ELSE
IFRSTM = ILO
ILASTM = IHI
END IF
IITER = 0
ESHIFT = CZERO
MAXIT = 30*( IHI-ILO+1 )
DO 170 JITER = 1, MAXIT
IF( JITER.GT.MAXIT )
$ GO TO 180
IF( ILAST.EQ.ILO ) THEN
GO TO 60
ELSE
IF( ABS1( H( ILAST, ILAST-1 ) ).LE.ATOL ) THEN
H( ILAST, ILAST-1 ) = CZERO
GO TO 60
END IF
END IF
IF( ABS( T( ILAST, ILAST ) ).LE.BTOL ) THEN
T( ILAST, ILAST ) = CZERO
GO TO 50
END IF
DO 40 J = ILAST - 1, ILO, -1
IF( J.EQ.ILO ) THEN
ILAZRO = .TRUE.
ELSE
IF( ABS1( H( J, J-1 ) ).LE.ATOL ) THEN
H( J, J-1 ) = CZERO
ILAZRO = .TRUE.
ELSE
ILAZRO = .FALSE.
END IF
END IF
IF( ABS( T( J, J ) ).LT.BTOL ) THEN
T( J, J ) = CZERO
ILAZR2 = .FALSE.
IF( .NOT.ILAZRO ) THEN
IF( ABS1( H( J, J-1 ) )*( ASCALE*ABS1( H( J+1,
$ J ) ) ).LE.ABS1( H( J, J ) )*( ASCALE*ATOL ) )
$ ILAZR2 = .TRUE.
END IF
IF( ILAZRO .OR. ILAZR2 ) THEN
DO 20 JCH = J, ILAST - 1
CTEMP = H( JCH, JCH )
CALL ZLARTG( CTEMP, H( JCH+1, JCH ), C, S,
$ H( JCH, JCH ) )
H( JCH+1, JCH ) = CZERO
CALL ZROT( ILASTM-JCH, H( JCH, JCH+1 ), LDH,
$ H( JCH+1, JCH+1 ), LDH, C, S )
CALL ZROT( ILASTM-JCH, T( JCH, JCH+1 ), LDT,
$ T( JCH+1, JCH+1 ), LDT, C, S )
IF( ILQ )
$ CALL ZROT( N, Q( 1, JCH ), 1, Q( 1, JCH+1 ), 1,
$ C, DCONJG( S ) )
IF( ILAZR2 )
$ H( JCH, JCH-1 ) = H( JCH, JCH-1 )*C
ILAZR2 = .FALSE.
IF( ABS1( T( JCH+1, JCH+1 ) ).GE.BTOL ) THEN
IF( JCH+1.GE.ILAST ) THEN
GO TO 60
ELSE
IFIRST = JCH + 1
GO TO 70
END IF
END IF
T( JCH+1, JCH+1 ) = CZERO
20 CONTINUE
GO TO 50
ELSE
DO 30 JCH = J, ILAST - 1
CTEMP = T( JCH, JCH+1 )
CALL ZLARTG( CTEMP, T( JCH+1, JCH+1 ), C, S,
$ T( JCH, JCH+1 ) )
T( JCH+1, JCH+1 ) = CZERO
IF( JCH.LT.ILASTM-1 )
$ CALL ZROT( ILASTM-JCH-1, T( JCH, JCH+2 ), LDT,
$ T( JCH+1, JCH+2 ), LDT, C, S )
CALL ZROT( ILASTM-JCH+2, H( JCH, JCH-1 ), LDH,
$ H( JCH+1, JCH-1 ), LDH, C, S )
IF( ILQ )
$ CALL ZROT( N, Q( 1, JCH ), 1, Q( 1, JCH+1 ), 1,
$ C, DCONJG( S ) )
CTEMP = H( JCH+1, JCH )
CALL ZLARTG( CTEMP, H( JCH+1, JCH-1 ), C, S,
$ H( JCH+1, JCH ) )
H( JCH+1, JCH-1 ) = CZERO
CALL ZROT( JCH+1-IFRSTM, H( IFRSTM, JCH ), 1,
$ H( IFRSTM, JCH-1 ), 1, C, S )
CALL ZROT( JCH-IFRSTM, T( IFRSTM, JCH ), 1,
$ T( IFRSTM, JCH-1 ), 1, C, S )
IF( ILZ )
$ CALL ZROT( N, Z( 1, JCH ), 1, Z( 1, JCH-1 ), 1,
$ C, S )
30 CONTINUE
GO TO 50
END IF
ELSE IF( ILAZRO ) THEN
IFIRST = J
GO TO 70
END IF
40 CONTINUE
INFO = 2*N + 1
GO TO 210
50 CONTINUE
CTEMP = H( ILAST, ILAST )
CALL ZLARTG( CTEMP, H( ILAST, ILAST-1 ), C, S,
$ H( ILAST, ILAST ) )
H( ILAST, ILAST-1 ) = CZERO
CALL ZROT( ILAST-IFRSTM, H( IFRSTM, ILAST ), 1,
$ H( IFRSTM, ILAST-1 ), 1, C, S )
CALL ZROT( ILAST-IFRSTM, T( IFRSTM, ILAST ), 1,
$ T( IFRSTM, ILAST-1 ), 1, C, S )
IF( ILZ )
$ CALL ZROT( N, Z( 1, ILAST ), 1, Z( 1, ILAST-1 ), 1, C, S )
60 CONTINUE
ABSB = ABS( T( ILAST, ILAST ) )
IF( ABSB.GT.SAFMIN ) THEN
SIGNBC = DCONJG( T( ILAST, ILAST ) / ABSB )
T( ILAST, ILAST ) = ABSB
IF( ILSCHR ) THEN
CALL ZSCAL( ILAST-IFRSTM, SIGNBC, T( IFRSTM, ILAST ), 1 )
CALL ZSCAL( ILAST+1-IFRSTM, SIGNBC, H( IFRSTM, ILAST ),
$ 1 )
ELSE
H( ILAST, ILAST ) = H( ILAST, ILAST )*SIGNBC
END IF
IF( ILZ )
$ CALL ZSCAL( N, SIGNBC, Z( 1, ILAST ), 1 )
ELSE
T( ILAST, ILAST ) = CZERO
END IF
ALPHA( ILAST ) = H( ILAST, ILAST )
BETA( ILAST ) = T( ILAST, ILAST )
ILAST = ILAST - 1
IF( ILAST.LT.ILO )
$ GO TO 190
IITER = 0
ESHIFT = CZERO
IF( .NOT.ILSCHR ) THEN
ILASTM = ILAST
IF( IFRSTM.GT.ILAST )
$ IFRSTM = ILO
END IF
GO TO 160
70 CONTINUE
IITER = IITER + 1
IF( .NOT.ILSCHR ) THEN
IFRSTM = IFIRST
END IF
IF( ( IITER / 10 )*10.NE.IITER ) THEN
U12 = ( BSCALE*T( ILAST-1, ILAST ) ) /
$ ( BSCALE*T( ILAST, ILAST ) )
AD11 = ( ASCALE*H( ILAST-1, ILAST-1 ) ) /
$ ( BSCALE*T( ILAST-1, ILAST-1 ) )
AD21 = ( ASCALE*H( ILAST, ILAST-1 ) ) /
$ ( BSCALE*T( ILAST-1, ILAST-1 ) )
AD12 = ( ASCALE*H( ILAST-1, ILAST ) ) /
$ ( BSCALE*T( ILAST, ILAST ) )
AD22 = ( ASCALE*H( ILAST, ILAST ) ) /
$ ( BSCALE*T( ILAST, ILAST ) )
ABI22 = AD22 - U12*AD21
T1 = HALF*( AD11+ABI22 )
RTDISC = SQRT( T1**2+AD12*AD21-AD11*AD22 )
TEMP = DBLE( T1-ABI22 )*DBLE( RTDISC ) +
$ DIMAG( T1-ABI22 )*DIMAG( RTDISC )
IF( TEMP.LE.ZERO ) THEN
SHIFT = T1 + RTDISC
ELSE
SHIFT = T1 - RTDISC
END IF
ELSE
ESHIFT = ESHIFT + DCONJG( ( ASCALE*H( ILAST-1, ILAST ) ) /
$ ( BSCALE*T( ILAST-1, ILAST-1 ) ) )
SHIFT = ESHIFT
END IF
DO 80 J = ILAST - 1, IFIRST + 1, -1
ISTART = J
CTEMP = ASCALE*H( J, J ) - SHIFT*( BSCALE*T( J, J ) )
TEMP = ABS1( CTEMP )
TEMP2 = ASCALE*ABS1( H( J+1, J ) )
TEMPR = MAX( TEMP, TEMP2 )
IF( TEMPR.LT.ONE .AND. TEMPR.NE.ZERO ) THEN
TEMP = TEMP / TEMPR
TEMP2 = TEMP2 / TEMPR
END IF
IF( ABS1( H( J, J-1 ) )*TEMP2.LE.TEMP*ATOL )
$ GO TO 90
80 CONTINUE
ISTART = IFIRST
CTEMP = ASCALE*H( IFIRST, IFIRST ) -
$ SHIFT*( BSCALE*T( IFIRST, IFIRST ) )
90 CONTINUE
CTEMP2 = ASCALE*H( ISTART+1, ISTART )
CALL ZLARTG( CTEMP, CTEMP2, C, S, CTEMP3 )
DO 150 J = ISTART, ILAST - 1
IF( J.GT.ISTART ) THEN
CTEMP = H( J, J-1 )
CALL ZLARTG( CTEMP, H( J+1, J-1 ), C, S, H( J, J-1 ) )
H( J+1, J-1 ) = CZERO
END IF
DO 100 JC = J, ILASTM
CTEMP = C*H( J, JC ) + S*H( J+1, JC )
H( J+1, JC ) = -DCONJG( S )*H( J, JC ) + C*H( J+1, JC )
H( J, JC ) = CTEMP
CTEMP2 = C*T( J, JC ) + S*T( J+1, JC )
T( J+1, JC ) = -DCONJG( S )*T( J, JC ) + C*T( J+1, JC )
T( J, JC ) = CTEMP2
100 CONTINUE
IF( ILQ ) THEN
DO 110 JR = 1, N
CTEMP = C*Q( JR, J ) + DCONJG( S )*Q( JR, J+1 )
Q( JR, J+1 ) = -S*Q( JR, J ) + C*Q( JR, J+1 )
Q( JR, J ) = CTEMP
110 CONTINUE
END IF
CTEMP = T( J+1, J+1 )
CALL ZLARTG( CTEMP, T( J+1, J ), C, S, T( J+1, J+1 ) )
T( J+1, J ) = CZERO
DO 120 JR = IFRSTM, MIN( J+2, ILAST )
CTEMP = C*H( JR, J+1 ) + S*H( JR, J )
H( JR, J ) = -DCONJG( S )*H( JR, J+1 ) + C*H( JR, J )
H( JR, J+1 ) = CTEMP
120 CONTINUE
DO 130 JR = IFRSTM, J
CTEMP = C*T( JR, J+1 ) + S*T( JR, J )
T( JR, J ) = -DCONJG( S )*T( JR, J+1 ) + C*T( JR, J )
T( JR, J+1 ) = CTEMP
130 CONTINUE
IF( ILZ ) THEN
DO 140 JR = 1, N
CTEMP = C*Z( JR, J+1 ) + S*Z( JR, J )
Z( JR, J ) = -DCONJG( S )*Z( JR, J+1 ) + C*Z( JR, J )
Z( JR, J+1 ) = CTEMP
140 CONTINUE
END IF
150 CONTINUE
160 CONTINUE
170 CONTINUE
180 CONTINUE
INFO = ILAST
GO TO 210
190 CONTINUE
DO 200 J = 1, ILO - 1
ABSB = ABS( T( J, J ) )
IF( ABSB.GT.SAFMIN ) THEN
SIGNBC = DCONJG( T( J, J ) / ABSB )
T( J, J ) = ABSB
IF( ILSCHR ) THEN
CALL ZSCAL( J-1, SIGNBC, T( 1, J ), 1 )
CALL ZSCAL( J, SIGNBC, H( 1, J ), 1 )
ELSE
H( J, J ) = H( J, J )*SIGNBC
END IF
IF( ILZ )
$ CALL ZSCAL( N, SIGNBC, Z( 1, J ), 1 )
ELSE
T( J, J ) = CZERO
END IF
ALPHA( J ) = H( J, J )
BETA( J ) = T( J, J )
200 CONTINUE
INFO = 0
210 CONTINUE
WORK( 1 ) = DCMPLX( N )
RETURN
END