001:       SUBROUTINE CTGSEN( 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 CLACN2 in place of CLACON, 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:       REAL               PL, PR
016: *     ..
017: *     .. Array Arguments ..
018:       LOGICAL            SELECT( * )
019:       INTEGER            IWORK( * )
020:       REAL               DIF( * )
021:       COMPLEX            A( LDA, * ), ALPHA( * ), B( LDB, * ),
022:      $                   BETA( * ), Q( LDQ, * ), WORK( * ), Z( LDZ, * )
023: *     ..
024: *
025: *  Purpose
026: *  =======
027: *
028: *  CTGSEN 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: *  CTGSEN 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 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 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 array, dimension (N)
104: *  BETA    (output) COMPLEX 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 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 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) REAL
139: *  PR      (output) REAL
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) REAL 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 CLACN2.
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 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: *  CTGSEN 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 CLATDF), then the parameter
293: *  IDIFJB (see below) should be changed from 3 to 4 (routine CLATDF
294: *  (IJOB = 2 will be used)). See CTGSYL 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:       REAL               ZERO, ONE
329:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+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:       REAL               DSCALE, DSUM, RDSCAL, SAFMIN
336:       COMPLEX            TEMP1, TEMP2
337: *     ..
338: *     .. Local Arrays ..
339:       INTEGER            ISAVE( 3 )
340: *     ..
341: *     .. External Subroutines ..
342:       REAL               SLAMCH 
343:       EXTERNAL           CLACN2, CLACPY, CLASSQ, CSCAL, CTGEXC, CTGSYL,
344:      $                   SLAMCH, XERBLA
345: *     ..
346: *     .. Intrinsic Functions ..
347:       INTRINSIC          ABS, CMPLX, CONJG, MAX, SQRT
348: *     ..
349: *     .. Executable Statements ..
350: *
351: *     Decode and test the input parameters
352: *
353:       INFO = 0
354:       LQUERY = ( LWORK.EQ.-1 .OR. LIWORK.EQ.-1 )
355: *
356:       IF( IJOB.LT.0 .OR. IJOB.GT.5 ) THEN
357:          INFO = -1
358:       ELSE IF( N.LT.0 ) THEN
359:          INFO = -5
360:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
361:          INFO = -7
362:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
363:          INFO = -9
364:       ELSE IF( LDQ.LT.1 .OR. ( WANTQ .AND. LDQ.LT.N ) ) THEN
365:          INFO = -13
366:       ELSE IF( LDZ.LT.1 .OR. ( WANTZ .AND. LDZ.LT.N ) ) THEN
367:          INFO = -15
368:       END IF
369: *
370:       IF( INFO.NE.0 ) THEN
371:          CALL XERBLA( 'CTGSEN', -INFO )
372:          RETURN
373:       END IF
374: *
375:       IERR = 0
376: *
377:       WANTP = IJOB.EQ.1 .OR. IJOB.GE.4
378:       WANTD1 = IJOB.EQ.2 .OR. IJOB.EQ.4
379:       WANTD2 = IJOB.EQ.3 .OR. IJOB.EQ.5
380:       WANTD = WANTD1 .OR. WANTD2
381: *
382: *     Set M to the dimension of the specified pair of deflating
383: *     subspaces.
384: *
385:       M = 0
386:       DO 10 K = 1, N
387:          ALPHA( K ) = A( K, K )
388:          BETA( K ) = B( K, K )
389:          IF( K.LT.N ) THEN
390:             IF( SELECT( K ) )
391:      $         M = M + 1
392:          ELSE
393:             IF( SELECT( N ) )
394:      $         M = M + 1
395:          END IF
396:    10 CONTINUE
397: *
398:       IF( IJOB.EQ.1 .OR. IJOB.EQ.2 .OR. IJOB.EQ.4 ) THEN
399:          LWMIN = MAX( 1, 2*M*(N-M) )
400:          LIWMIN = MAX( 1, N+2 )
401:       ELSE IF( IJOB.EQ.3 .OR. IJOB.EQ.5 ) THEN
402:          LWMIN = MAX( 1, 4*M*(N-M) )
403:          LIWMIN = MAX( 1, 2*M*(N-M), N+2 )
404:       ELSE
405:          LWMIN = 1
406:          LIWMIN = 1
407:       END IF
408: *
409:       WORK( 1 ) = LWMIN
410:       IWORK( 1 ) = LIWMIN
411: *
412:       IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
413:          INFO = -21
414:       ELSE IF( LIWORK.LT.LIWMIN .AND. .NOT.LQUERY ) THEN
415:          INFO = -23
416:       END IF
417: *
418:       IF( INFO.NE.0 ) THEN
419:          CALL XERBLA( 'CTGSEN', -INFO )
420:          RETURN
421:       ELSE IF( LQUERY ) THEN
422:          RETURN
423:       END IF
424: *
425: *     Quick return if possible.
426: *
427:       IF( M.EQ.N .OR. M.EQ.0 ) THEN
428:          IF( WANTP ) THEN
429:             PL = ONE
430:             PR = ONE
431:          END IF
432:          IF( WANTD ) THEN
433:             DSCALE = ZERO
434:             DSUM = ONE
435:             DO 20 I = 1, N
436:                CALL CLASSQ( N, A( 1, I ), 1, DSCALE, DSUM )
437:                CALL CLASSQ( N, B( 1, I ), 1, DSCALE, DSUM )
438:    20       CONTINUE
439:             DIF( 1 ) = DSCALE*SQRT( DSUM )
440:             DIF( 2 ) = DIF( 1 )
441:          END IF
442:          GO TO 70
443:       END IF
444: *
445: *     Get machine constant
446: *
447:       SAFMIN = SLAMCH( 'S' )
448: *
449: *     Collect the selected blocks at the top-left corner of (A, B).
450: *
451:       KS = 0
452:       DO 30 K = 1, N
453:          SWAP = SELECT( K )
454:          IF( SWAP ) THEN
455:             KS = KS + 1
456: *
457: *           Swap the K-th block to position KS. Compute unitary Q
458: *           and Z that will swap adjacent diagonal blocks in (A, B).
459: *
460:             IF( K.NE.KS )
461:      $         CALL CTGEXC( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z,
462:      $                      LDZ, K, KS, IERR )
463: *
464:             IF( IERR.GT.0 ) THEN
465: *
466: *              Swap is rejected: exit.
467: *
468:                INFO = 1
469:                IF( WANTP ) THEN
470:                   PL = ZERO
471:                   PR = ZERO
472:                END IF
473:                IF( WANTD ) THEN
474:                   DIF( 1 ) = ZERO
475:                   DIF( 2 ) = ZERO
476:                END IF
477:                GO TO 70
478:             END IF
479:          END IF
480:    30 CONTINUE
481:       IF( WANTP ) THEN
482: *
483: *        Solve generalized Sylvester equation for R and L:
484: *                   A11 * R - L * A22 = A12
485: *                   B11 * R - L * B22 = B12
486: *
487:          N1 = M
488:          N2 = N - M
489:          I = N1 + 1
490:          CALL CLACPY( 'Full', N1, N2, A( 1, I ), LDA, WORK, N1 )
491:          CALL CLACPY( 'Full', N1, N2, B( 1, I ), LDB, WORK( N1*N2+1 ),
492:      $                N1 )
493:          IJB = 0
494:          CALL CTGSYL( 'N', IJB, N1, N2, A, LDA, A( I, I ), LDA, WORK,
495:      $                N1, B, LDB, B( I, I ), LDB, WORK( N1*N2+1 ), N1,
496:      $                DSCALE, DIF( 1 ), WORK( N1*N2*2+1 ),
497:      $                LWORK-2*N1*N2, IWORK, IERR )
498: *
499: *        Estimate the reciprocal of norms of "projections" onto
500: *        left and right eigenspaces
501: *
502:          RDSCAL = ZERO
503:          DSUM = ONE
504:          CALL CLASSQ( N1*N2, WORK, 1, RDSCAL, DSUM )
505:          PL = RDSCAL*SQRT( DSUM )
506:          IF( PL.EQ.ZERO ) THEN
507:             PL = ONE
508:          ELSE
509:             PL = DSCALE / ( SQRT( DSCALE*DSCALE / PL+PL )*SQRT( PL ) )
510:          END IF
511:          RDSCAL = ZERO
512:          DSUM = ONE
513:          CALL CLASSQ( N1*N2, WORK( N1*N2+1 ), 1, RDSCAL, DSUM )
514:          PR = RDSCAL*SQRT( DSUM )
515:          IF( PR.EQ.ZERO ) THEN
516:             PR = ONE
517:          ELSE
518:             PR = DSCALE / ( SQRT( DSCALE*DSCALE / PR+PR )*SQRT( PR ) )
519:          END IF
520:       END IF
521:       IF( WANTD ) THEN
522: *
523: *        Compute estimates Difu and Difl.
524: *
525:          IF( WANTD1 ) THEN
526:             N1 = M
527:             N2 = N - M
528:             I = N1 + 1
529:             IJB = IDIFJB
530: *
531: *           Frobenius norm-based Difu estimate.
532: *
533:             CALL CTGSYL( 'N', IJB, N1, N2, A, LDA, A( I, I ), LDA, WORK,
534:      $                   N1, B, LDB, B( I, I ), LDB, WORK( N1*N2+1 ),
535:      $                   N1, DSCALE, DIF( 1 ), WORK( N1*N2*2+1 ),
536:      $                   LWORK-2*N1*N2, IWORK, IERR )
537: *
538: *           Frobenius norm-based Difl estimate.
539: *
540:             CALL CTGSYL( 'N', IJB, N2, N1, A( I, I ), LDA, A, LDA, WORK,
541:      $                   N2, B( I, I ), LDB, B, LDB, WORK( N1*N2+1 ),
542:      $                   N2, DSCALE, DIF( 2 ), WORK( N1*N2*2+1 ),
543:      $                   LWORK-2*N1*N2, IWORK, IERR )
544:          ELSE
545: *
546: *           Compute 1-norm-based estimates of Difu and Difl using
547: *           reversed communication with CLACN2. In each step a
548: *           generalized Sylvester equation or a transposed variant
549: *           is solved.
550: *
551:             KASE = 0
552:             N1 = M
553:             N2 = N - M
554:             I = N1 + 1
555:             IJB = 0
556:             MN2 = 2*N1*N2
557: *
558: *           1-norm-based estimate of Difu.
559: *
560:    40       CONTINUE
561:             CALL CLACN2( MN2, WORK( MN2+1 ), WORK, DIF( 1 ), KASE,
562:      $                   ISAVE )
563:             IF( KASE.NE.0 ) THEN
564:                IF( KASE.EQ.1 ) THEN
565: *
566: *                 Solve generalized Sylvester equation
567: *
568:                   CALL CTGSYL( 'N', IJB, N1, N2, A, LDA, A( I, I ), LDA,
569:      $                         WORK, N1, B, LDB, B( I, I ), LDB,
570:      $                         WORK( N1*N2+1 ), N1, DSCALE, DIF( 1 ),
571:      $                         WORK( N1*N2*2+1 ), LWORK-2*N1*N2, IWORK,
572:      $                         IERR )
573:                ELSE
574: *
575: *                 Solve the transposed variant.
576: *
577:                   CALL CTGSYL( 'C', IJB, N1, N2, A, LDA, A( I, I ), LDA,
578:      $                         WORK, N1, B, LDB, B( I, I ), LDB,
579:      $                         WORK( N1*N2+1 ), N1, DSCALE, DIF( 1 ),
580:      $                         WORK( N1*N2*2+1 ), LWORK-2*N1*N2, IWORK,
581:      $                         IERR )
582:                END IF
583:                GO TO 40
584:             END IF
585:             DIF( 1 ) = DSCALE / DIF( 1 )
586: *
587: *           1-norm-based estimate of Difl.
588: *
589:    50       CONTINUE
590:             CALL CLACN2( MN2, WORK( MN2+1 ), WORK, DIF( 2 ), KASE,
591:      $                   ISAVE )
592:             IF( KASE.NE.0 ) THEN
593:                IF( KASE.EQ.1 ) THEN
594: *
595: *                 Solve generalized Sylvester equation
596: *
597:                   CALL CTGSYL( 'N', IJB, N2, N1, A( I, I ), LDA, A, LDA,
598:      $                         WORK, N2, B( I, I ), LDB, B, LDB,
599:      $                         WORK( N1*N2+1 ), N2, DSCALE, DIF( 2 ),
600:      $                         WORK( N1*N2*2+1 ), LWORK-2*N1*N2, IWORK,
601:      $                         IERR )
602:                ELSE
603: *
604: *                 Solve the transposed variant.
605: *
606:                   CALL CTGSYL( 'C', IJB, N2, N1, A( I, I ), LDA, A, LDA,
607:      $                         WORK, N2, B, LDB, B( I, I ), LDB,
608:      $                         WORK( N1*N2+1 ), N2, DSCALE, DIF( 2 ),
609:      $                         WORK( N1*N2*2+1 ), LWORK-2*N1*N2, IWORK,
610:      $                         IERR )
611:                END IF
612:                GO TO 50
613:             END IF
614:             DIF( 2 ) = DSCALE / DIF( 2 )
615:          END IF
616:       END IF
617: *
618: *     If B(K,K) is complex, make it real and positive (normalization
619: *     of the generalized Schur form) and Store the generalized 
620: *     eigenvalues of reordered pair (A, B)
621: *
622:       DO 60 K = 1, N
623:          DSCALE = ABS( B( K, K ) )
624:          IF( DSCALE.GT.SAFMIN ) THEN
625:             TEMP1 = CONJG( B( K, K ) / DSCALE )
626:             TEMP2 = B( K, K ) / DSCALE
627:             B( K, K ) = DSCALE
628:             CALL CSCAL( N-K, TEMP1, B( K, K+1 ), LDB )
629:             CALL CSCAL( N-K+1, TEMP1, A( K, K ), LDA )
630:             IF( WANTQ )
631:      $         CALL CSCAL( N, TEMP2, Q( 1, K ), 1 )
632:          ELSE
633:             B( K, K ) = CMPLX( ZERO, ZERO )
634:          END IF
635: *
636:          ALPHA( K ) = A( K, K )
637:          BETA( K ) = B( K, K )
638: *
639:    60 CONTINUE
640: *
641:    70 CONTINUE
642: *
643:       WORK( 1 ) = LWMIN
644:       IWORK( 1 ) = LIWMIN
645: *
646:       RETURN
647: *
648: *     End of CTGSEN
649: *
650:       END
651: