001:       SUBROUTINE ZTGSEN( IJOB, WANTQ, WANTZ, SELECT, N, A, LDA, B, LDB,
002:      $                   ALPHA, BETA, Q, LDQ, Z, LDZ, M, PL, PR, DIF,
003:      $                   WORK, LWORK, IWORK, LIWORK, INFO )
004: *
005: *  -- LAPACK routine (version 3.2) --
006: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
007: *     January 2007
008: *
009: *     Modified to call ZLACN2 in place of ZLACON, 10 Feb 03, SJH.
010: *
011: *     .. Scalar Arguments ..
012:       LOGICAL            WANTQ, WANTZ
013:       INTEGER            IJOB, INFO, LDA, LDB, LDQ, LDZ, LIWORK, LWORK,
014:      $                   M, N
015:       DOUBLE PRECISION   PL, PR
016: *     ..
017: *     .. Array Arguments ..
018:       LOGICAL            SELECT( * )
019:       INTEGER            IWORK( * )
020:       DOUBLE PRECISION   DIF( * )
021:       COMPLEX*16         A( LDA, * ), ALPHA( * ), B( LDB, * ),
022:      $                   BETA( * ), Q( LDQ, * ), WORK( * ), Z( LDZ, * )
023: *     ..
024: *
025: *  Purpose
026: *  =======
027: *
028: *  ZTGSEN reorders the generalized Schur decomposition of a complex
029: *  matrix pair (A, B) (in terms of an unitary equivalence trans-
030: *  formation Q' * (A, B) * Z), so that a selected cluster of eigenvalues
031: *  appears in the leading diagonal blocks of the pair (A,B). The leading
032: *  columns of Q and Z form unitary bases of the corresponding left and
033: *  right eigenspaces (deflating subspaces). (A, B) must be in
034: *  generalized Schur canonical form, that is, A and B are both upper
035: *  triangular.
036: *
037: *  ZTGSEN also computes the generalized eigenvalues
038: *
039: *           w(j)= ALPHA(j) / BETA(j)
040: *
041: *  of the reordered matrix pair (A, B).
042: *
043: *  Optionally, the routine computes estimates of reciprocal condition
044: *  numbers for eigenvalues and eigenspaces. These are Difu[(A11,B11),
045: *  (A22,B22)] and Difl[(A11,B11), (A22,B22)], i.e. the separation(s)
046: *  between the matrix pairs (A11, B11) and (A22,B22) that correspond to
047: *  the selected cluster and the eigenvalues outside the cluster, resp.,
048: *  and norms of "projections" onto left and right eigenspaces w.r.t.
049: *  the selected cluster in the (1,1)-block.
050: *
051: *
052: *  Arguments
053: *  =========
054: *
055: *  IJOB    (input) integer
056: *          Specifies whether condition numbers are required for the
057: *          cluster of eigenvalues (PL and PR) or the deflating subspaces
058: *          (Difu and Difl):
059: *           =0: Only reorder w.r.t. SELECT. No extras.
060: *           =1: Reciprocal of norms of "projections" onto left and right
061: *               eigenspaces w.r.t. the selected cluster (PL and PR).
062: *           =2: Upper bounds on Difu and Difl. F-norm-based estimate
063: *               (DIF(1:2)).
064: *           =3: Estimate of Difu and Difl. 1-norm-based estimate
065: *               (DIF(1:2)).
066: *               About 5 times as expensive as IJOB = 2.
067: *           =4: Compute PL, PR and DIF (i.e. 0, 1 and 2 above): Economic
068: *               version to get it all.
069: *           =5: Compute PL, PR and DIF (i.e. 0, 1 and 3 above)
070: *
071: *  WANTQ   (input) LOGICAL
072: *          .TRUE. : update the left transformation matrix Q;
073: *          .FALSE.: do not update Q.
074: *
075: *  WANTZ   (input) LOGICAL
076: *          .TRUE. : update the right transformation matrix Z;
077: *          .FALSE.: do not update Z.
078: *
079: *  SELECT  (input) LOGICAL array, dimension (N)
080: *          SELECT specifies the eigenvalues in the selected cluster. To
081: *          select an eigenvalue w(j), SELECT(j) must be set to
082: *          .TRUE..
083: *
084: *  N       (input) INTEGER
085: *          The order of the matrices A and B. N >= 0.
086: *
087: *  A       (input/output) COMPLEX*16 array, dimension(LDA,N)
088: *          On entry, the upper triangular matrix A, in generalized
089: *          Schur canonical form.
090: *          On exit, A is overwritten by the reordered matrix A.
091: *
092: *  LDA     (input) INTEGER
093: *          The leading dimension of the array A. LDA >= max(1,N).
094: *
095: *  B       (input/output) COMPLEX*16 array, dimension(LDB,N)
096: *          On entry, the upper triangular matrix B, in generalized
097: *          Schur canonical form.
098: *          On exit, B is overwritten by the reordered matrix B.
099: *
100: *  LDB     (input) INTEGER
101: *          The leading dimension of the array B. LDB >= max(1,N).
102: *
103: *  ALPHA   (output) COMPLEX*16 array, dimension (N)
104: *  BETA    (output) COMPLEX*16 array, dimension (N)
105: *          The diagonal elements of A and B, respectively,
106: *          when the pair (A,B) has been reduced to generalized Schur
107: *          form.  ALPHA(i)/BETA(i) i=1,...,N are the generalized
108: *          eigenvalues.
109: *
110: *  Q       (input/output) COMPLEX*16 array, dimension (LDQ,N)
111: *          On entry, if WANTQ = .TRUE., Q is an N-by-N matrix.
112: *          On exit, Q has been postmultiplied by the left unitary
113: *          transformation matrix which reorder (A, B); The leading M
114: *          columns of Q form orthonormal bases for the specified pair of
115: *          left eigenspaces (deflating subspaces).
116: *          If WANTQ = .FALSE., Q is not referenced.
117: *
118: *  LDQ     (input) INTEGER
119: *          The leading dimension of the array Q. LDQ >= 1.
120: *          If WANTQ = .TRUE., LDQ >= N.
121: *
122: *  Z       (input/output) COMPLEX*16 array, dimension (LDZ,N)
123: *          On entry, if WANTZ = .TRUE., Z is an N-by-N matrix.
124: *          On exit, Z has been postmultiplied by the left unitary
125: *          transformation matrix which reorder (A, B); The leading M
126: *          columns of Z form orthonormal bases for the specified pair of
127: *          left eigenspaces (deflating subspaces).
128: *          If WANTZ = .FALSE., Z is not referenced.
129: *
130: *  LDZ     (input) INTEGER
131: *          The leading dimension of the array Z. LDZ >= 1.
132: *          If WANTZ = .TRUE., LDZ >= N.
133: *
134: *  M       (output) INTEGER
135: *          The dimension of the specified pair of left and right
136: *          eigenspaces, (deflating subspaces) 0 <= M <= N.
137: *
138: *  PL      (output) DOUBLE PRECISION
139: *  PR      (output) DOUBLE PRECISION
140: *          If IJOB = 1, 4 or 5, PL, PR are lower bounds on the
141: *          reciprocal  of the norm of "projections" onto left and right
142: *          eigenspace with respect to the selected cluster.
143: *          0 < PL, PR <= 1.
144: *          If M = 0 or M = N, PL = PR  = 1.
145: *          If IJOB = 0, 2 or 3 PL, PR are not referenced.
146: *
147: *  DIF     (output) DOUBLE PRECISION array, dimension (2).
148: *          If IJOB >= 2, DIF(1:2) store the estimates of Difu and Difl.
149: *          If IJOB = 2 or 4, DIF(1:2) are F-norm-based upper bounds on
150: *          Difu and Difl. If IJOB = 3 or 5, DIF(1:2) are 1-norm-based
151: *          estimates of Difu and Difl, computed using reversed
152: *          communication with ZLACN2.
153: *          If M = 0 or N, DIF(1:2) = F-norm([A, B]).
154: *          If IJOB = 0 or 1, DIF is not referenced.
155: *
156: *  WORK    (workspace/output) COMPLEX*16 array, dimension (MAX(1,LWORK))
157: *          IF IJOB = 0, WORK is not referenced.  Otherwise,
158: *          on exit, if INFO = 0, WORK(1) returns the optimal LWORK.
159: *
160: *  LWORK   (input) INTEGER
161: *          The dimension of the array WORK. LWORK >=  1
162: *          If IJOB = 1, 2 or 4, LWORK >=  2*M*(N-M)
163: *          If IJOB = 3 or 5, LWORK >=  4*M*(N-M)
164: *
165: *          If LWORK = -1, then a workspace query is assumed; the routine
166: *          only calculates the optimal size of the WORK array, returns
167: *          this value as the first entry of the WORK array, and no error
168: *          message related to LWORK is issued by XERBLA.
169: *
170: *  IWORK   (workspace/output) INTEGER array, dimension (MAX(1,LIWORK))
171: *          IF IJOB = 0, IWORK is not referenced.  Otherwise,
172: *          on exit, if INFO = 0, IWORK(1) returns the optimal LIWORK.
173: *
174: *  LIWORK  (input) INTEGER
175: *          The dimension of the array IWORK. LIWORK >= 1.
176: *          If IJOB = 1, 2 or 4, LIWORK >=  N+2;
177: *          If IJOB = 3 or 5, LIWORK >= MAX(N+2, 2*M*(N-M));
178: *
179: *          If LIWORK = -1, then a workspace query is assumed; the
180: *          routine only calculates the optimal size of the IWORK array,
181: *          returns this value as the first entry of the IWORK array, and
182: *          no error message related to LIWORK is issued by XERBLA.
183: *
184: *  INFO    (output) INTEGER
185: *            =0: Successful exit.
186: *            <0: If INFO = -i, the i-th argument had an illegal value.
187: *            =1: Reordering of (A, B) failed because the transformed
188: *                matrix pair (A, B) would be too far from generalized
189: *                Schur form; the problem is very ill-conditioned.
190: *                (A, B) may have been partially reordered.
191: *                If requested, 0 is returned in DIF(*), PL and PR.
192: *
193: *
194: *  Further Details
195: *  ===============
196: *
197: *  ZTGSEN first collects the selected eigenvalues by computing unitary
198: *  U and W that move them to the top left corner of (A, B). In other
199: *  words, the selected eigenvalues are the eigenvalues of (A11, B11) in
200: *
201: *                U'*(A, B)*W = (A11 A12) (B11 B12) n1
202: *                              ( 0  A22),( 0  B22) n2
203: *                                n1  n2    n1  n2
204: *
205: *  where N = n1+n2 and U' means the conjugate transpose of U. The first
206: *  n1 columns of U and W span the specified pair of left and right
207: *  eigenspaces (deflating subspaces) of (A, B).
208: *
209: *  If (A, B) has been obtained from the generalized real Schur
210: *  decomposition of a matrix pair (C, D) = Q*(A, B)*Z', then the
211: *  reordered generalized Schur form of (C, D) is given by
212: *
213: *           (C, D) = (Q*U)*(U'*(A, B)*W)*(Z*W)',
214: *
215: *  and the first n1 columns of Q*U and Z*W span the corresponding
216: *  deflating subspaces of (C, D) (Q and Z store Q*U and Z*W, resp.).
217: *
218: *  Note that if the selected eigenvalue is sufficiently ill-conditioned,
219: *  then its value may differ significantly from its value before
220: *  reordering.
221: *
222: *  The reciprocal condition numbers of the left and right eigenspaces
223: *  spanned by the first n1 columns of U and W (or Q*U and Z*W) may
224: *  be returned in DIF(1:2), corresponding to Difu and Difl, resp.
225: *
226: *  The Difu and Difl are defined as:
227: *
228: *       Difu[(A11, B11), (A22, B22)] = sigma-min( Zu )
229: *  and
230: *       Difl[(A11, B11), (A22, B22)] = Difu[(A22, B22), (A11, B11)],
231: *
232: *  where sigma-min(Zu) is the smallest singular value of the
233: *  (2*n1*n2)-by-(2*n1*n2) matrix
234: *
235: *       Zu = [ kron(In2, A11)  -kron(A22', In1) ]
236: *            [ kron(In2, B11)  -kron(B22', In1) ].
237: *
238: *  Here, Inx is the identity matrix of size nx and A22' is the
239: *  transpose of A22. kron(X, Y) is the Kronecker product between
240: *  the matrices X and Y.
241: *
242: *  When DIF(2) is small, small changes in (A, B) can cause large changes
243: *  in the deflating subspace. An approximate (asymptotic) bound on the
244: *  maximum angular error in the computed deflating subspaces is
245: *
246: *       EPS * norm((A, B)) / DIF(2),
247: *
248: *  where EPS is the machine precision.
249: *
250: *  The reciprocal norm of the projectors on the left and right
251: *  eigenspaces associated with (A11, B11) may be returned in PL and PR.
252: *  They are computed as follows. First we compute L and R so that
253: *  P*(A, B)*Q is block diagonal, where
254: *
255: *       P = ( I -L ) n1           Q = ( I R ) n1
256: *           ( 0  I ) n2    and        ( 0 I ) n2
257: *             n1 n2                    n1 n2
258: *
259: *  and (L, R) is the solution to the generalized Sylvester equation
260: *
261: *       A11*R - L*A22 = -A12
262: *       B11*R - L*B22 = -B12
263: *
264: *  Then PL = (F-norm(L)**2+1)**(-1/2) and PR = (F-norm(R)**2+1)**(-1/2).
265: *  An approximate (asymptotic) bound on the average absolute error of
266: *  the selected eigenvalues is
267: *
268: *       EPS * norm((A, B)) / PL.
269: *
270: *  There are also global error bounds which valid for perturbations up
271: *  to a certain restriction:  A lower bound (x) on the smallest
272: *  F-norm(E,F) for which an eigenvalue of (A11, B11) may move and
273: *  coalesce with an eigenvalue of (A22, B22) under perturbation (E,F),
274: *  (i.e. (A + E, B + F), is
275: *
276: *   x = min(Difu,Difl)/((1/(PL*PL)+1/(PR*PR))**(1/2)+2*max(1/PL,1/PR)).
277: *
278: *  An approximate bound on x can be computed from DIF(1:2), PL and PR.
279: *
280: *  If y = ( F-norm(E,F) / x) <= 1, the angles between the perturbed
281: *  (L', R') and unperturbed (L, R) left and right deflating subspaces
282: *  associated with the selected cluster in the (1,1)-blocks can be
283: *  bounded as
284: *
285: *   max-angle(L, L') <= arctan( y * PL / (1 - y * (1 - PL * PL)**(1/2))
286: *   max-angle(R, R') <= arctan( y * PR / (1 - y * (1 - PR * PR)**(1/2))
287: *
288: *  See LAPACK User's Guide section 4.11 or the following references
289: *  for more information.
290: *
291: *  Note that if the default method for computing the Frobenius-norm-
292: *  based estimate DIF is not wanted (see ZLATDF), then the parameter
293: *  IDIFJB (see below) should be changed from 3 to 4 (routine ZLATDF
294: *  (IJOB = 2 will be used)). See ZTGSYL for more details.
295: *
296: *  Based on contributions by
297: *     Bo Kagstrom and Peter Poromaa, Department of Computing Science,
298: *     Umea University, S-901 87 Umea, Sweden.
299: *
300: *  References
301: *  ==========
302: *
303: *  [1] B. Kagstrom; A Direct Method for Reordering Eigenvalues in the
304: *      Generalized Real Schur Form of a Regular Matrix Pair (A, B), in
305: *      M.S. Moonen et al (eds), Linear Algebra for Large Scale and
306: *      Real-Time Applications, Kluwer Academic Publ. 1993, pp 195-218.
307: *
308: *  [2] B. Kagstrom and P. Poromaa; Computing Eigenspaces with Specified
309: *      Eigenvalues of a Regular Matrix Pair (A, B) and Condition
310: *      Estimation: Theory, Algorithms and Software, Report
311: *      UMINF - 94.04, Department of Computing Science, Umea University,
312: *      S-901 87 Umea, Sweden, 1994. Also as LAPACK Working Note 87.
313: *      To appear in Numerical Algorithms, 1996.
314: *
315: *  [3] B. Kagstrom and P. Poromaa, LAPACK-Style Algorithms and Software
316: *      for Solving the Generalized Sylvester Equation and Estimating the
317: *      Separation between Regular Matrix Pairs, Report UMINF - 93.23,
318: *      Department of Computing Science, Umea University, S-901 87 Umea,
319: *      Sweden, December 1993, Revised April 1994, Also as LAPACK working
320: *      Note 75. To appear in ACM Trans. on Math. Software, Vol 22, No 1,
321: *      1996.
322: *
323: *  =====================================================================
324: *
325: *     .. Parameters ..
326:       INTEGER            IDIFJB
327:       PARAMETER          ( IDIFJB = 3 )
328:       DOUBLE PRECISION   ZERO, ONE
329:       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+0 )
330: *     ..
331: *     .. Local Scalars ..
332:       LOGICAL            LQUERY, SWAP, WANTD, WANTD1, WANTD2, WANTP
333:       INTEGER            I, IERR, IJB, K, KASE, KS, LIWMIN, LWMIN, MN2,
334:      $                   N1, N2
335:       DOUBLE PRECISION   DSCALE, DSUM, RDSCAL, SAFMIN
336:       COMPLEX*16         TEMP1, TEMP2
337: *     ..
338: *     .. Local Arrays ..
339:       INTEGER            ISAVE( 3 )
340: *     ..
341: *     .. External Subroutines ..
342:       EXTERNAL           XERBLA, ZLACN2, ZLACPY, ZLASSQ, ZSCAL, ZTGEXC,
343:      $                   ZTGSYL
344: *     ..
345: *     .. Intrinsic Functions ..
346:       INTRINSIC          ABS, DCMPLX, DCONJG, MAX, SQRT
347: *     ..
348: *     .. External Functions ..
349:       DOUBLE PRECISION   DLAMCH
350:       EXTERNAL           DLAMCH
351: *     ..
352: *     .. Executable Statements ..
353: *
354: *     Decode and test the input parameters
355: *
356:       INFO = 0
357:       LQUERY = ( LWORK.EQ.-1 .OR. LIWORK.EQ.-1 )
358: *
359:       IF( IJOB.LT.0 .OR. IJOB.GT.5 ) THEN
360:          INFO = -1
361:       ELSE IF( N.LT.0 ) THEN
362:          INFO = -5
363:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
364:          INFO = -7
365:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
366:          INFO = -9
367:       ELSE IF( LDQ.LT.1 .OR. ( WANTQ .AND. LDQ.LT.N ) ) THEN
368:          INFO = -13
369:       ELSE IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
370:          INFO = -15
371:       END IF
372: *
373:       IF( INFO.NE.0 ) THEN
374:          CALL XERBLA( 'ZTGSEN', -INFO )
375:          RETURN
376:       END IF
377: *
378:       IERR = 0
379: *
380:       WANTP = IJOB.EQ.1 .OR. IJOB.GE.4
381:       WANTD1 = IJOB.EQ.2 .OR. IJOB.EQ.4
382:       WANTD2 = IJOB.EQ.3 .OR. IJOB.EQ.5
383:       WANTD = WANTD1 .OR. WANTD2
384: *
385: *     Set M to the dimension of the specified pair of deflating
386: *     subspaces.
387: *
388:       M = 0
389:       DO 10 K = 1, N
390:          ALPHA( K ) = A( K, K )
391:          BETA( K ) = B( K, K )
392:          IF( K.LT.N ) THEN
393:             IF( SELECT( K ) )
394:      $         M = M + 1
395:          ELSE
396:             IF( SELECT( N ) )
397:      $         M = M + 1
398:          END IF
399:    10 CONTINUE
400: *
401:       IF( IJOB.EQ.1 .OR. IJOB.EQ.2 .OR. IJOB.EQ.4 ) THEN
402:          LWMIN = MAX( 1, 2*M*( N-M ) )
403:          LIWMIN = MAX( 1, N+2 )
404:       ELSE IF( IJOB.EQ.3 .OR. IJOB.EQ.5 ) THEN
405:          LWMIN = MAX( 1, 4*M*( N-M ) )
406:          LIWMIN = MAX( 1, 2*M*( N-M ), N+2 )
407:       ELSE
408:          LWMIN = 1
409:          LIWMIN = 1
410:       END IF
411: *
412:       WORK( 1 ) = LWMIN
413:       IWORK( 1 ) = LIWMIN
414: *
415:       IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
416:          INFO = -21
417:       ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
418:          INFO = -23
419:       END IF
420: *
421:       IF( INFO.NE.0 ) THEN
422:          CALL XERBLA( 'ZTGSEN', -INFO )
423:          RETURN
424:       ELSE IF( LQUERY ) THEN
425:          RETURN
426:       END IF
427: *
428: *     Quick return if possible.
429: *
430:       IF( M.EQ.N .OR. M.EQ.0 ) THEN
431:          IF( WANTP ) THEN
432:             PL = ONE
433:             PR = ONE
434:          END IF
435:          IF( WANTD ) THEN
436:             DSCALE = ZERO
437:             DSUM = ONE
438:             DO 20 I = 1, N
439:                CALL ZLASSQ( N, A( 1, I ), 1, DSCALE, DSUM )
440:                CALL ZLASSQ( N, B( 1, I ), 1, DSCALE, DSUM )
441:    20       CONTINUE
442:             DIF( 1 ) = DSCALE*SQRT( DSUM )
443:             DIF( 2 ) = DIF( 1 )
444:          END IF
445:          GO TO 70
446:       END IF
447: *
448: *     Get machine constant
449: *
450:       SAFMIN = DLAMCH( 'S' )
451: *
452: *     Collect the selected blocks at the top-left corner of (A, B).
453: *
454:       KS = 0
455:       DO 30 K = 1, N
456:          SWAP = SELECT( K )
457:          IF( SWAP ) THEN
458:             KS = KS + 1
459: *
460: *           Swap the K-th block to position KS. Compute unitary Q
461: *           and Z that will swap adjacent diagonal blocks in (A, B).
462: *
463:             IF( K.NE.KS )
464:      $         CALL ZTGEXC( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
465:      $                      LDZ, K, KS, IERR )
466: *
467:             IF( IERR.GT.0 ) THEN
468: *
469: *              Swap is rejected: exit.
470: *
471:                INFO = 1
472:                IF( WANTP ) THEN
473:                   PL = ZERO
474:                   PR = ZERO
475:                END IF
476:                IF( WANTD ) THEN
477:                   DIF( 1 ) = ZERO
478:                   DIF( 2 ) = ZERO
479:                END IF
480:                GO TO 70
481:             END IF
482:          END IF
483:    30 CONTINUE
484:       IF( WANTP ) THEN
485: *
486: *        Solve generalized Sylvester equation for R and L:
487: *                   A11 * R - L * A22 = A12
488: *                   B11 * R - L * B22 = B12
489: *
490:          N1 = M
491:          N2 = N - M
492:          I = N1 + 1
493:          CALL ZLACPY( 'Full', N1, N2, A( 1, I ), LDA, WORK, N1 )
494:          CALL ZLACPY( 'Full', N1, N2, B( 1, I ), LDB, WORK( N1*N2+1 ),
495:      $                N1 )
496:          IJB = 0
497:          CALL ZTGSYL( 'N', IJB, N1, N2, A, LDA, A( I, I ), LDA, WORK,
498:      $                N1, B, LDB, B( I, I ), LDB, WORK( N1*N2+1 ), N1,
499:      $                DSCALE, DIF( 1 ), WORK( N1*N2*2+1 ),
500:      $                LWORK-2*N1*N2, IWORK, IERR )
501: *
502: *        Estimate the reciprocal of norms of "projections" onto
503: *        left and right eigenspaces
504: *
505:          RDSCAL = ZERO
506:          DSUM = ONE
507:          CALL ZLASSQ( N1*N2, WORK, 1, RDSCAL, DSUM )
508:          PL = RDSCAL*SQRT( DSUM )
509:          IF( PL.EQ.ZERO ) THEN
510:             PL = ONE
511:          ELSE
512:             PL = DSCALE / ( SQRT( DSCALE*DSCALE / PL+PL )*SQRT( PL ) )
513:          END IF
514:          RDSCAL = ZERO
515:          DSUM = ONE
516:          CALL ZLASSQ( N1*N2, WORK( N1*N2+1 ), 1, RDSCAL, DSUM )
517:          PR = RDSCAL*SQRT( DSUM )
518:          IF( PR.EQ.ZERO ) THEN
519:             PR = ONE
520:          ELSE
521:             PR = DSCALE / ( SQRT( DSCALE*DSCALE / PR+PR )*SQRT( PR ) )
522:          END IF
523:       END IF
524:       IF( WANTD ) THEN
525: *
526: *        Compute estimates Difu and Difl.
527: *
528:          IF( WANTD1 ) THEN
529:             N1 = M
530:             N2 = N - M
531:             I = N1 + 1
532:             IJB = IDIFJB
533: *
534: *           Frobenius norm-based Difu estimate.
535: *
536:             CALL ZTGSYL( 'N', IJB, N1, N2, A, LDA, A( I, I ), LDA, WORK,
537:      $                   N1, B, LDB, B( I, I ), LDB, WORK( N1*N2+1 ),
538:      $                   N1, DSCALE, DIF( 1 ), WORK( N1*N2*2+1 ),
539:      $                   LWORK-2*N1*N2, IWORK, IERR )
540: *
541: *           Frobenius norm-based Difl estimate.
542: *
543:             CALL ZTGSYL( 'N', IJB, N2, N1, A( I, I ), LDA, A, LDA, WORK,
544:      $                   N2, B( I, I ), LDB, B, LDB, WORK( N1*N2+1 ),
545:      $                   N2, DSCALE, DIF( 2 ), WORK( N1*N2*2+1 ),
546:      $                   LWORK-2*N1*N2, IWORK, IERR )
547:          ELSE
548: *
549: *           Compute 1-norm-based estimates of Difu and Difl using
550: *           reversed communication with ZLACN2. In each step a
551: *           generalized Sylvester equation or a transposed variant
552: *           is solved.
553: *
554:             KASE = 0
555:             N1 = M
556:             N2 = N - M
557:             I = N1 + 1
558:             IJB = 0
559:             MN2 = 2*N1*N2
560: *
561: *           1-norm-based estimate of Difu.
562: *
563:    40       CONTINUE
564:             CALL ZLACN2( MN2, WORK( MN2+1 ), WORK, DIF( 1 ), KASE,
565:      $                   ISAVE )
566:             IF( KASE.NE.0 ) THEN
567:                IF( KASE.EQ.1 ) THEN
568: *
569: *                 Solve generalized Sylvester equation
570: *
571:                   CALL ZTGSYL( 'N', IJB, N1, N2, A, LDA, A( I, I ), LDA,
572:      $                         WORK, N1, B, LDB, B( I, I ), LDB,
573:      $                         WORK( N1*N2+1 ), N1, DSCALE, DIF( 1 ),
574:      $                         WORK( N1*N2*2+1 ), LWORK-2*N1*N2, IWORK,
575:      $                         IERR )
576:                ELSE
577: *
578: *                 Solve the transposed variant.
579: *
580:                   CALL ZTGSYL( 'C', IJB, N1, N2, A, LDA, A( I, I ), LDA,
581:      $                         WORK, N1, B, LDB, B( I, I ), LDB,
582:      $                         WORK( N1*N2+1 ), N1, DSCALE, DIF( 1 ),
583:      $                         WORK( N1*N2*2+1 ), LWORK-2*N1*N2, IWORK,
584:      $                         IERR )
585:                END IF
586:                GO TO 40
587:             END IF
588:             DIF( 1 ) = DSCALE / DIF( 1 )
589: *
590: *           1-norm-based estimate of Difl.
591: *
592:    50       CONTINUE
593:             CALL ZLACN2( MN2, WORK( MN2+1 ), WORK, DIF( 2 ), KASE,
594:      $                   ISAVE )
595:             IF( KASE.NE.0 ) THEN
596:                IF( KASE.EQ.1 ) THEN
597: *
598: *                 Solve generalized Sylvester equation
599: *
600:                   CALL ZTGSYL( 'N', IJB, N2, N1, A( I, I ), LDA, A, LDA,
601:      $                         WORK, N2, B( I, I ), LDB, B, LDB,
602:      $                         WORK( N1*N2+1 ), N2, DSCALE, DIF( 2 ),
603:      $                         WORK( N1*N2*2+1 ), LWORK-2*N1*N2, IWORK,
604:      $                         IERR )
605:                ELSE
606: *
607: *                 Solve the transposed variant.
608: *
609:                   CALL ZTGSYL( 'C', IJB, N2, N1, A( I, I ), LDA, A, LDA,
610:      $                         WORK, N2, B, LDB, B( I, I ), LDB,
611:      $                         WORK( N1*N2+1 ), N2, DSCALE, DIF( 2 ),
612:      $                         WORK( N1*N2*2+1 ), LWORK-2*N1*N2, IWORK,
613:      $                         IERR )
614:                END IF
615:                GO TO 50
616:             END IF
617:             DIF( 2 ) = DSCALE / DIF( 2 )
618:          END IF
619:       END IF
620: *
621: *     If B(K,K) is complex, make it real and positive (normalization
622: *     of the generalized Schur form) and Store the generalized
623: *     eigenvalues of reordered pair (A, B)
624: *
625:       DO 60 K = 1, N
626:          DSCALE = ABS( B( K, K ) )
627:          IF( DSCALE.GT.SAFMIN ) THEN
628:             TEMP1 = DCONJG( B( K, K ) / DSCALE )
629:             TEMP2 = B( K, K ) / DSCALE
630:             B( K, K ) = DSCALE
631:             CALL ZSCAL( N-K, TEMP1, B( K, K+1 ), LDB )
632:             CALL ZSCAL( N-K+1, TEMP1, A( K, K ), LDA )
633:             IF( WANTQ )
634:      $         CALL ZSCAL( N, TEMP2, Q( 1, K ), 1 )
635:          ELSE
636:             B( K, K ) = DCMPLX( ZERO, ZERO )
637:          END IF
638: *
639:          ALPHA( K ) = A( K, K )
640:          BETA( K ) = B( K, K )
641: *
642:    60 CONTINUE
643: *
644:    70 CONTINUE
645: *
646:       WORK( 1 ) = LWMIN
647:       IWORK( 1 ) = LIWMIN
648: *
649:       RETURN
650: *
651: *     End of ZTGSEN
652: *
653:       END
654: