1       SUBROUTINE CDRGSX( NSIZE, NCMAX, THRESH, NIN, NOUT, A, LDA, B,
  2      $                   AI, BI, Z, Q, ALPHA, BETA, C, LDC, S, WORK,
  3      $                   LWORK, RWORK, IWORK, LIWORK, BWORK, INFO )
  4 *
  5 *  -- LAPACK test routine (version 3.1) --
  6 *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
  7 *     November 2006
  8 *
  9 *     .. Scalar Arguments ..
 10       INTEGER            INFO, LDA, LDC, LIWORK, LWORK, NCMAX, NIN,
 11      $                   NOUT, NSIZE
 12       REAL               THRESH
 13 *     ..
 14 *     .. Array Arguments ..
 15       LOGICAL            BWORK( * )
 16       INTEGER            IWORK( * )
 17       REAL               RWORK( * ), S( * )
 18       COMPLEX            A( LDA, * ), AI( LDA, * ), ALPHA( * ),
 19      $                   B( LDA, * ), BETA( * ), BI( LDA, * ),
 20      $                   C( LDC, * ), Q( LDA, * ), WORK( * ),
 21      $                   Z( LDA, * )
 22 *     ..
 23 *
 24 *  Purpose
 25 *  =======
 26 *
 27 *  CDRGSX checks the nonsymmetric generalized eigenvalue (Schur form)
 28 *  problem expert driver CGGESX.
 29 *
 30 *  CGGES factors A and B as Q*S*Z'  and Q*T*Z' , where ' means conjugate
 31 *  transpose, S and T are  upper triangular (i.e., in generalized Schur
 32 *  form), and Q and Z are unitary. It also computes the generalized
 33 *  eigenvalues (alpha(j),beta(j)), j=1,...,n.  Thus,
 34 *  w(j) = alpha(j)/beta(j) is a root of the characteristic equation
 35 *
 36 *                  det( A - w(j) B ) = 0
 37 *
 38 *  Optionally it also reorders the eigenvalues so that a selected
 39 *  cluster of eigenvalues appears in the leading diagonal block of the
 40 *  Schur forms; computes a reciprocal condition number for the average
 41 *  of the selected eigenvalues; and computes a reciprocal condition
 42 *  number for the right and left deflating subspaces corresponding to
 43 *  the selected eigenvalues.
 44 *
 45 *  When CDRGSX is called with NSIZE > 0, five (5) types of built-in
 46 *  matrix pairs are used to test the routine CGGESX.
 47 *
 48 *  When CDRGSX is called with NSIZE = 0, it reads in test matrix data
 49 *  to test CGGESX.
 50 *  (need more details on what kind of read-in data are needed).
 51 *
 52 *  For each matrix pair, the following tests will be performed and
 53 *  compared with the threshhold THRESH except for the tests (7) and (9):
 54 *
 55 *  (1)   | A - Q S Z' | / ( |A| n ulp )
 56 *
 57 *  (2)   | B - Q T Z' | / ( |B| n ulp )
 58 *
 59 *  (3)   | I - QQ' | / ( n ulp )
 60 *
 61 *  (4)   | I - ZZ' | / ( n ulp )
 62 *
 63 *  (5)   if A is in Schur form (i.e. triangular form)
 64 *
 65 *  (6)   maximum over j of D(j)  where:
 66 *
 67 *                      |alpha(j) - S(j,j)|        |beta(j) - T(j,j)|
 68 *            D(j) = ------------------------ + -----------------------
 69 *                   max(|alpha(j)|,|S(j,j)|)   max(|beta(j)|,|T(j,j)|)
 70 *
 71 *  (7)   if sorting worked and SDIM is the number of eigenvalues
 72 *        which were selected.
 73 *
 74 *  (8)   the estimated value DIF does not differ from the true values of
 75 *        Difu and Difl more than a factor 10*THRESH. If the estimate DIF
 76 *        equals zero the corresponding true values of Difu and Difl
 77 *        should be less than EPS*norm(A, B). If the true value of Difu
 78 *        and Difl equal zero, the estimate DIF should be less than
 79 *        EPS*norm(A, B).
 80 *
 81 *  (9)   If INFO = N+3 is returned by CGGESX, the reordering "failed"
 82 *        and we check that DIF = PL = PR = 0 and that the true value of
 83 *        Difu and Difl is < EPS*norm(A, B). We count the events when
 84 *        INFO=N+3.
 85 *
 86 *  For read-in test matrices, the same tests are run except that the
 87 *  exact value for DIF (and PL) is input data.  Additionally, there is
 88 *  one more test run for read-in test matrices:
 89 *
 90 *  (10)  the estimated value PL does not differ from the true value of
 91 *        PLTRU more than a factor THRESH. If the estimate PL equals
 92 *        zero the corresponding true value of PLTRU should be less than
 93 *        EPS*norm(A, B). If the true value of PLTRU equal zero, the
 94 *        estimate PL should be less than EPS*norm(A, B).
 95 *
 96 *  Note that for the built-in tests, a total of 10*NSIZE*(NSIZE-1)
 97 *  matrix pairs are generated and tested. NSIZE should be kept small.
 98 *
 99 *  SVD (routine CGESVD) is used for computing the true value of DIF_u
100 *  and DIF_l when testing the built-in test problems.
101 *
102 *  Built-in Test Matrices
103 *  ======================
104 *
105 *  All built-in test matrices are the 2 by 2 block of triangular
106 *  matrices
107 *
108 *           A = [ A11 A12 ]    and      B = [ B11 B12 ]
109 *               [     A22 ]                 [     B22 ]
110 *
111 *  where for different type of A11 and A22 are given as the following.
112 *  A12 and B12 are chosen so that the generalized Sylvester equation
113 *
114 *           A11*R - L*A22 = -A12
115 *           B11*R - L*B22 = -B12
116 *
117 *  have prescribed solution R and L.
118 *
119 *  Type 1:  A11 = J_m(1,-1) and A_22 = J_k(1-a,1).
120 *           B11 = I_m, B22 = I_k
121 *           where J_k(a,b) is the k-by-k Jordan block with ``a'' on
122 *           diagonal and ``b'' on superdiagonal.
123 *
124 *  Type 2:  A11 = (a_ij) = ( 2(.5-sin(i)) ) and
125 *           B11 = (b_ij) = ( 2(.5-sin(ij)) ) for i=1,...,m, j=i,...,m
126 *           A22 = (a_ij) = ( 2(.5-sin(i+j)) ) and
127 *           B22 = (b_ij) = ( 2(.5-sin(ij)) ) for i=m+1,...,k, j=i,...,k
128 *
129 *  Type 3:  A11, A22 and B11, B22 are chosen as for Type 2, but each
130 *           second diagonal block in A_11 and each third diagonal block
131 *           in A_22 are made as 2 by 2 blocks.
132 *
133 *  Type 4:  A11 = ( 20(.5 - sin(ij)) ) and B22 = ( 2(.5 - sin(i+j)) )
134 *              for i=1,...,m,  j=1,...,m and
135 *           A22 = ( 20(.5 - sin(i+j)) ) and B22 = ( 2(.5 - sin(ij)) )
136 *              for i=m+1,...,k,  j=m+1,...,k
137 *
138 *  Type 5:  (A,B) and have potentially close or common eigenvalues and
139 *           very large departure from block diagonality A_11 is chosen
140 *           as the m x m leading submatrix of A_1:
141 *                   |  1  b                            |
142 *                   | -b  1                            |
143 *                   |        1+d  b                    |
144 *                   |         -b 1+d                   |
145 *            A_1 =  |                  d  1            |
146 *                   |                 -1  d            |
147 *                   |                        -d  1     |
148 *                   |                        -1 -d     |
149 *                   |                               1  |
150 *           and A_22 is chosen as the k x k leading submatrix of A_2:
151 *                   | -1  b                            |
152 *                   | -b -1                            |
153 *                   |       1-d  b                     |
154 *                   |       -b  1-d                    |
155 *            A_2 =  |                 d 1+b            |
156 *                   |               -1-b d             |
157 *                   |                       -d  1+b    |
158 *                   |                      -1+b  -d    |
159 *                   |                              1-d |
160 *           and matrix B are chosen as identity matrices (see SLATM5).
161 *
162 *
163 *  Arguments
164 *  =========
165 *
166 *  NSIZE   (input) INTEGER
167 *          The maximum size of the matrices to use. NSIZE >= 0.
168 *          If NSIZE = 0, no built-in tests matrices are used, but
169 *          read-in test matrices are used to test SGGESX.
170 *
171 *  NCMAX   (input) INTEGER
172 *          Maximum allowable NMAX for generating Kroneker matrix
173 *          in call to CLAKF2
174 *
175 *  THRESH  (input) REAL
176 *          A test will count as "failed" if the "error", computed as
177 *          described above, exceeds THRESH.  Note that the error
178 *          is scaled to be O(1), so THRESH should be a reasonably
179 *          small multiple of 1, e.g., 10 or 100.  In particular,
180 *          it should not depend on the precision (single vs. double)
181 *          or the size of the matrix.  THRESH >= 0.
182 *
183 *  NIN     (input) INTEGER
184 *          The FORTRAN unit number for reading in the data file of
185 *          problems to solve.
186 *
187 *  NOUT    (input) INTEGER
188 *          The FORTRAN unit number for printing out error messages
189 *          (e.g., if a routine returns INFO not equal to 0.)
190 *
191 *  A       (workspace) COMPLEX array, dimension (LDA, NSIZE)
192 *          Used to store the matrix whose eigenvalues are to be
193 *          computed.  On exit, A contains the last matrix actually used.
194 *
195 *  LDA     (input) INTEGER
196 *          The leading dimension of A, B, AI, BI, Z and Q,
197 *          LDA >= max( 1, NSIZE ). For the read-in test,
198 *          LDA >= max( 1, N ), N is the size of the test matrices.
199 *
200 *  B       (workspace) COMPLEX array, dimension (LDA, NSIZE)
201 *          Used to store the matrix whose eigenvalues are to be
202 *          computed.  On exit, B contains the last matrix actually used.
203 *
204 *  AI      (workspace) COMPLEX array, dimension (LDA, NSIZE)
205 *          Copy of A, modified by CGGESX.
206 *
207 *  BI      (workspace) COMPLEX array, dimension (LDA, NSIZE)
208 *          Copy of B, modified by CGGESX.
209 *
210 *  Z       (workspace) COMPLEX array, dimension (LDA, NSIZE)
211 *          Z holds the left Schur vectors computed by CGGESX.
212 *
213 *  Q       (workspace) COMPLEX array, dimension (LDA, NSIZE)
214 *          Q holds the right Schur vectors computed by CGGESX.
215 *
216 *  ALPHA   (workspace) COMPLEX array, dimension (NSIZE)
217 *  BETA    (workspace) COMPLEX array, dimension (NSIZE)
218 *          On exit, ALPHA/BETA are the eigenvalues.
219 *
220 *  C       (workspace) COMPLEX array, dimension (LDC, LDC)
221 *          Store the matrix generated by subroutine CLAKF2, this is the
222 *          matrix formed by Kronecker products used for estimating
223 *          DIF.
224 *
225 *  LDC     (input) INTEGER
226 *          The leading dimension of C. LDC >= max(1, LDA*LDA/2 ).
227 *
228 *  S       (workspace) REAL array, dimension (LDC)
229 *          Singular values of C
230 *
231 *  WORK    (workspace) COMPLEX array, dimension (LWORK)
232 *
233 *  LWORK   (input) INTEGER
234 *          The dimension of the array WORK.  LWORK >= 3*NSIZE*NSIZE/2
235 *
236 *  RWORK   (workspace) REAL array,
237 *                                 dimension (5*NSIZE*NSIZE/2 - 4)
238 *
239 *  IWORK   (workspace) INTEGER array, dimension (LIWORK)
240 *
241 *  LIWORK  (input) INTEGER
242 *          The dimension of the array IWORK. LIWORK >= NSIZE + 2.
243 *
244 *  BWORK   (workspace) LOGICAL array, dimension (NSIZE)
245 *
246 *  INFO    (output) INTEGER
247 *          = 0:  successful exit
248 *          < 0:  if INFO = -i, the i-th argument had an illegal value.
249 *          > 0:  A routine returned an error code.
250 *
251 *  =====================================================================
252 *
253 *     .. Parameters ..
254       REAL               ZERO, ONE, TEN
255       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0, TEN = 1.0E+1 )
256       COMPLEX            CZERO
257       PARAMETER          ( CZERO = ( 0.0E+00.0E+0 ) )
258 *     ..
259 *     .. Local Scalars ..
260       LOGICAL            ILABAD
261       CHARACTER          SENSE
262       INTEGER            BDSPAC, I, IFUNC, J, LINFO, MAXWRK, MINWRK, MM,
263      $                   MN2, NERRS, NPTKNT, NTEST, NTESTT, PRTYPE, QBA,
264      $                   QBB
265       REAL               ABNRM, BIGNUM, DIFTRU, PLTRU, SMLNUM, TEMP1,
266      $                   TEMP2, THRSH2, ULP, ULPINV, WEIGHT
267       COMPLEX            X
268 *     ..
269 *     .. Local Arrays ..
270       REAL               DIFEST( 2 ), PL( 2 ), RESULT10 )
271 *     ..
272 *     .. External Functions ..
273       LOGICAL            CLCTSX
274       INTEGER            ILAENV
275       REAL               CLANGE, SLAMCH
276       EXTERNAL           CLCTSX, ILAENV, CLANGE, SLAMCH
277 *     ..
278 *     .. External Subroutines ..
279       EXTERNAL           ALASVM, CGESVD, CGET51, CGGESX, CLACPY, CLAKF2,
280      $                   CLASET, CLATM5, SLABAD, XERBLA
281 *     ..
282 *     .. Scalars in Common ..
283       LOGICAL            FS
284       INTEGER            K, M, MPLUSN, N
285 *     ..
286 *     .. Common blocks ..
287       COMMON             / MN / M, N, MPLUSN, K, FS
288 *     ..
289 *     .. Intrinsic Functions ..
290       INTRINSIC          ABSAIMAGMAX, REAL, SQRT
291 *     ..
292 *     .. Statement Functions ..
293       REAL               ABS1
294 *     ..
295 *     .. Statement Function definitions ..
296       ABS1( X ) = ABSREAL( X ) ) + ABSAIMAG( X ) )
297 *     ..
298 *     .. Executable Statements ..
299 *
300 *     Check for errors
301 *
302       IF( NSIZE.LT.0 ) THEN
303          INFO = -1
304       ELSE IF( THRESH.LT.ZERO ) THEN
305          INFO = -2
306       ELSE IF( NIN.LE.0 ) THEN
307          INFO = -3
308       ELSE IF( NOUT.LE.0 ) THEN
309          INFO = -4
310       ELSE IF( LDA.LT.1 .OR. LDA.LT.NSIZE ) THEN
311          INFO = -6
312       ELSE IF( LDC.LT.1 .OR. LDC.LT.NSIZE*NSIZE / 2 ) THEN
313          INFO = -15
314       ELSE IF( LIWORK.LT.NSIZE+2 ) THEN
315          INFO = -21
316       END IF
317 *
318 *     Compute workspace
319 *      (Note: Comments in the code beginning "Workspace:" describe the
320 *       minimal amount of workspace needed at that point in the code,
321 *       as well as the preferred amount for good performance.
322 *       NB refers to the optimal block size for the immediately
323 *       following subroutine, as returned by ILAENV.)
324 *
325       MINWRK = 1
326       IF( INFO.EQ.0 .AND. LWORK.GE.1 ) THEN
327          MINWRK = 3*NSIZE*NSIZE / 2
328 *
329 *        workspace for cggesx
330 *
331          MAXWRK = NSIZE*1+ILAENV( 1'CGEQRF'' ', NSIZE, 1, NSIZE,
332      $            0 ) )
333          MAXWRK = MAX( MAXWRK, NSIZE*1+ILAENV( 1'CUNGQR'' ',
334      $            NSIZE, 1, NSIZE, -1 ) ) )
335 *
336 *        workspace for cgesvd
337 *
338          BDSPAC = 3*NSIZE*NSIZE / 2
339          MAXWRK = MAX( MAXWRK, NSIZE*NSIZE*
340      $            ( 1+ILAENV( 1'CGEBRD'' ', NSIZE*NSIZE / 2,
341      $            NSIZE*NSIZE / 2-1-1 ) ) )
342          MAXWRK = MAX( MAXWRK, BDSPAC )
343 *
344          MAXWRK = MAX( MAXWRK, MINWRK )
345 *
346          WORK( 1 ) = MAXWRK
347       END IF
348 *
349       IF( LWORK.LT.MINWRK )
350      $   INFO = -18
351 *
352       IF( INFO.NE.0 ) THEN
353          CALL XERBLA( 'CDRGSX'-INFO )
354          RETURN
355       END IF
356 *
357 *     Important constants
358 *
359       ULP = SLAMCH( 'P' )
360       ULPINV = ONE / ULP
361       SMLNUM = SLAMCH( 'S' ) / ULP
362       BIGNUM = ONE / SMLNUM
363       CALL SLABAD( SMLNUM, BIGNUM )
364       THRSH2 = TEN*THRESH
365       NTESTT = 0
366       NERRS = 0
367 *
368 *     Go to the tests for read-in matrix pairs
369 *
370       IFUNC = 0
371       IF( NSIZE.EQ.0 )
372      $   GO TO 70
373 *
374 *     Test the built-in matrix pairs.
375 *     Loop over different functions (IFUNC) of CGGESX, types (PRTYPE)
376 *     of test matrices, different size (M+N)
377 *
378       PRTYPE = 0
379       QBA = 3
380       QBB = 4
381       WEIGHT = SQRT( ULP )
382 *
383       DO 60 IFUNC = 03
384          DO 50 PRTYPE = 15
385             DO 40 M = 1, NSIZE - 1
386                DO 30 N = 1, NSIZE - M
387 *
388                   WEIGHT = ONE / WEIGHT
389                   MPLUSN = M + N
390 *
391 *                 Generate test matrices
392 *
393                   FS = .TRUE.
394                   K = 0
395 *
396                   CALL CLASET( 'Full', MPLUSN, MPLUSN, CZERO, CZERO, AI,
397      $                         LDA )
398                   CALL CLASET( 'Full', MPLUSN, MPLUSN, CZERO, CZERO, BI,
399      $                         LDA )
400 *
401                   CALL CLATM5( PRTYPE, M, N, AI, LDA, AI( M+1, M+1 ),
402      $                         LDA, AI( 1, M+1 ), LDA, BI, LDA,
403      $                         BI( M+1, M+1 ), LDA, BI( 1, M+1 ), LDA,
404      $                         Q, LDA, Z, LDA, WEIGHT, QBA, QBB )
405 *
406 *                 Compute the Schur factorization and swapping the
407 *                 m-by-m (1,1)-blocks with n-by-n (2,2)-blocks.
408 *                 Swapping is accomplished via the function CLCTSX
409 *                 which is supplied below.
410 *
411                   IF( IFUNC.EQ.0 ) THEN
412                      SENSE = 'N'
413                   ELSE IF( IFUNC.EQ.1 ) THEN
414                      SENSE = 'E'
415                   ELSE IF( IFUNC.EQ.2 ) THEN
416                      SENSE = 'V'
417                   ELSE IF( IFUNC.EQ.3 ) THEN
418                      SENSE = 'B'
419                   END IF
420 *
421                   CALL CLACPY( 'Full', MPLUSN, MPLUSN, AI, LDA, A, LDA )
422                   CALL CLACPY( 'Full', MPLUSN, MPLUSN, BI, LDA, B, LDA )
423 *
424                   CALL CGGESX( 'V''V''S', CLCTSX, SENSE, MPLUSN, AI,
425      $                         LDA, BI, LDA, MM, ALPHA, BETA, Q, LDA, Z,
426      $                         LDA, PL, DIFEST, WORK, LWORK, RWORK,
427      $                         IWORK, LIWORK, BWORK, LINFO )
428 *
429                   IF( LINFO.NE.0 .AND. LINFO.NE.MPLUSN+2 ) THEN
430                      RESULT1 ) = ULPINV
431                      WRITE( NOUT, FMT = 9999 )'CGGESX', LINFO, MPLUSN,
432      $                  PRTYPE
433                      INFO = LINFO
434                      GO TO 30
435                   END IF
436 *
437 *                 Compute the norm(A, B)
438 *
439                   CALL CLACPY( 'Full', MPLUSN, MPLUSN, AI, LDA, WORK,
440      $                         MPLUSN )
441                   CALL CLACPY( 'Full', MPLUSN, MPLUSN, BI, LDA,
442      $                         WORK( MPLUSN*MPLUSN+1 ), MPLUSN )
443                   ABNRM = CLANGE( 'Fro', MPLUSN, 2*MPLUSN, WORK, MPLUSN,
444      $                    RWORK )
445 *
446 *                 Do tests (1) to (4)
447 *
448                   RESULT2 ) = ZERO
449                   CALL CGET51( 1, MPLUSN, A, LDA, AI, LDA, Q, LDA, Z,
450      $                         LDA, WORK, RWORK, RESULT1 ) )
451                   CALL CGET51( 1, MPLUSN, B, LDA, BI, LDA, Q, LDA, Z,
452      $                         LDA, WORK, RWORK, RESULT2 ) )
453                   CALL CGET51( 3, MPLUSN, B, LDA, BI, LDA, Q, LDA, Q,
454      $                         LDA, WORK, RWORK, RESULT3 ) )
455                   CALL CGET51( 3, MPLUSN, B, LDA, BI, LDA, Z, LDA, Z,
456      $                         LDA, WORK, RWORK, RESULT4 ) )
457                   NTEST = 4
458 *
459 *                 Do tests (5) and (6): check Schur form of A and
460 *                 compare eigenvalues with diagonals.
461 *
462                   TEMP1 = ZERO
463                   RESULT5 ) = ZERO
464                   RESULT6 ) = ZERO
465 *
466                   DO 10 J = 1, MPLUSN
467                      ILABAD = .FALSE.
468                      TEMP2 = ( ABS1( ALPHA( J )-AI( J, J ) ) /
469      $                       MAX( SMLNUM, ABS1( ALPHA( J ) ),
470      $                       ABS1( AI( J, J ) ) )+
471      $                       ABS1( BETA( J )-BI( J, J ) ) /
472      $                       MAX( SMLNUM, ABS1( BETA( J ) ),
473      $                       ABS1( BI( J, J ) ) ) ) / ULP
474                      IF( J.LT.MPLUSN ) THEN
475                         IF( AI( J+1, J ).NE.ZERO ) THEN
476                            ILABAD = .TRUE.
477                            RESULT5 ) = ULPINV
478                         END IF
479                      END IF
480                      IF( J.GT.1 ) THEN
481                         IF( AI( J, J-1 ).NE.ZERO ) THEN
482                            ILABAD = .TRUE.
483                            RESULT5 ) = ULPINV
484                         END IF
485                      END IF
486                      TEMP1 = MAX( TEMP1, TEMP2 )
487                      IF( ILABAD ) THEN
488                         WRITE( NOUT, FMT = 9997 )J, MPLUSN, PRTYPE
489                      END IF
490    10             CONTINUE
491                   RESULT6 ) = TEMP1
492                   NTEST = NTEST + 2
493 *
494 *                 Test (7) (if sorting worked)
495 *
496                   RESULT7 ) = ZERO
497                   IF( LINFO.EQ.MPLUSN+3 ) THEN
498                      RESULT7 ) = ULPINV
499                   ELSE IF( MM.NE.N ) THEN
500                      RESULT7 ) = ULPINV
501                   END IF
502                   NTEST = NTEST + 1
503 *
504 *                 Test (8): compare the estimated value DIF and its
505 *                 value. first, compute the exact DIF.
506 *
507                   RESULT8 ) = ZERO
508                   MN2 = MM*( MPLUSN-MM )*2
509                   IF( IFUNC.GE.2 .AND. MN2.LE.NCMAX*NCMAX ) THEN
510 *
511 *                    Note: for either following two cases, there are
512 *                    almost same number of test cases fail the test.
513 *
514                      CALL CLAKF2( MM, MPLUSN-MM, AI, LDA,
515      $                            AI( MM+1, MM+1 ), BI,
516      $                            BI( MM+1, MM+1 ), C, LDC )
517 *
518                      CALL CGESVD( 'N''N', MN2, MN2, C, LDC, S, WORK,
519      $                            1, WORK( 2 ), 1, WORK( 3 ), LWORK-2,
520      $                            RWORK, INFO )
521                      DIFTRU = S( MN2 )
522 *
523                      IF( DIFEST( 2 ).EQ.ZERO ) THEN
524                         IF( DIFTRU.GT.ABNRM*ULP )
525      $                     RESULT8 ) = ULPINV
526                      ELSE IF( DIFTRU.EQ.ZERO ) THEN
527                         IF( DIFEST( 2 ).GT.ABNRM*ULP )
528      $                     RESULT8 ) = ULPINV
529                      ELSE IF( ( DIFTRU.GT.THRSH2*DIFEST( 2 ) ) .OR.
530      $                        ( DIFTRU*THRSH2.LT.DIFEST( 2 ) ) ) THEN
531                         RESULT8 ) = MAX( DIFTRU / DIFEST( 2 ),
532      $                                DIFEST( 2 ) / DIFTRU )
533                      END IF
534                      NTEST = NTEST + 1
535                   END IF
536 *
537 *                 Test (9)
538 *
539                   RESULT9 ) = ZERO
540                   IF( LINFO.EQ.( MPLUSN+2 ) ) THEN
541                      IF( DIFTRU.GT.ABNRM*ULP )
542      $                  RESULT9 ) = ULPINV
543                      IF( ( IFUNC.GT.1 ) .AND. ( DIFEST( 2 ).NE.ZERO ) )
544      $                  RESULT9 ) = ULPINV
545                      IF( ( IFUNC.EQ.1 ) .AND. ( PL( 1 ).NE.ZERO ) )
546      $                  RESULT9 ) = ULPINV
547                      NTEST = NTEST + 1
548                   END IF
549 *
550                   NTESTT = NTESTT + NTEST
551 *
552 *                 Print out tests which fail.
553 *
554                   DO 20 J = 19
555                      IFRESULT( J ).GE.THRESH ) THEN
556 *
557 *                       If this is the first test to fail,
558 *                       print a header to the data file.
559 *
560                         IF( NERRS.EQ.0 ) THEN
561                            WRITE( NOUT, FMT = 9996 )'CGX'
562 *
563 *                          Matrix types
564 *
565                            WRITE( NOUT, FMT = 9994 )
566 *
567 *                          Tests performed
568 *
569                            WRITE( NOUT, FMT = 9993 )'unitary''''',
570      $                        'transpose', ( '''', I = 14 )
571 *
572                         END IF
573                         NERRS = NERRS + 1
574                         IFRESULT( J ).LT.10000.0 ) THEN
575                            WRITE( NOUT, FMT = 9992 )MPLUSN, PRTYPE,
576      $                        WEIGHT, M, J, RESULT( J )
577                         ELSE
578                            WRITE( NOUT, FMT = 9991 )MPLUSN, PRTYPE,
579      $                        WEIGHT, M, J, RESULT( J )
580                         END IF
581                      END IF
582    20             CONTINUE
583 *
584    30          CONTINUE
585    40       CONTINUE
586    50    CONTINUE
587    60 CONTINUE
588 *
589       GO TO 150
590 *
591    70 CONTINUE
592 *
593 *     Read in data from file to check accuracy of condition estimation
594 *     Read input data until N=0
595 *
596       NPTKNT = 0
597 *
598    80 CONTINUE
599       READ( NIN, FMT = *END = 140 )MPLUSN
600       IF( MPLUSN.EQ.0 )
601      $   GO TO 140
602       READ( NIN, FMT = *END = 140 )N
603       DO 90 I = 1, MPLUSN
604          READ( NIN, FMT = * )( AI( I, J ), J = 1, MPLUSN )
605    90 CONTINUE
606       DO 100 I = 1, MPLUSN
607          READ( NIN, FMT = * )( BI( I, J ), J = 1, MPLUSN )
608   100 CONTINUE
609       READ( NIN, FMT = * )PLTRU, DIFTRU
610 *
611       NPTKNT = NPTKNT + 1
612       FS = .TRUE.
613       K = 0
614       M = MPLUSN - N
615 *
616       CALL CLACPY( 'Full', MPLUSN, MPLUSN, AI, LDA, A, LDA )
617       CALL CLACPY( 'Full', MPLUSN, MPLUSN, BI, LDA, B, LDA )
618 *
619 *     Compute the Schur factorization while swaping the
620 *     m-by-m (1,1)-blocks with n-by-n (2,2)-blocks.
621 *
622       CALL CGGESX( 'V''V''S', CLCTSX, 'B', MPLUSN, AI, LDA, BI, LDA,
623      $             MM, ALPHA, BETA, Q, LDA, Z, LDA, PL, DIFEST, WORK,
624      $             LWORK, RWORK, IWORK, LIWORK, BWORK, LINFO )
625 *
626       IF( LINFO.NE.0 .AND. LINFO.NE.MPLUSN+2 ) THEN
627          RESULT1 ) = ULPINV
628          WRITE( NOUT, FMT = 9998 )'CGGESX', LINFO, MPLUSN, NPTKNT
629          GO TO 130
630       END IF
631 *
632 *     Compute the norm(A, B)
633 *        (should this be norm of (A,B) or (AI,BI)?)
634 *
635       CALL CLACPY( 'Full', MPLUSN, MPLUSN, AI, LDA, WORK, MPLUSN )
636       CALL CLACPY( 'Full', MPLUSN, MPLUSN, BI, LDA,
637      $             WORK( MPLUSN*MPLUSN+1 ), MPLUSN )
638       ABNRM = CLANGE( 'Fro', MPLUSN, 2*MPLUSN, WORK, MPLUSN, RWORK )
639 *
640 *     Do tests (1) to (4)
641 *
642       CALL CGET51( 1, MPLUSN, A, LDA, AI, LDA, Q, LDA, Z, LDA, WORK,
643      $             RWORK, RESULT1 ) )
644       CALL CGET51( 1, MPLUSN, B, LDA, BI, LDA, Q, LDA, Z, LDA, WORK,
645      $             RWORK, RESULT2 ) )
646       CALL CGET51( 3, MPLUSN, B, LDA, BI, LDA, Q, LDA, Q, LDA, WORK,
647      $             RWORK, RESULT3 ) )
648       CALL CGET51( 3, MPLUSN, B, LDA, BI, LDA, Z, LDA, Z, LDA, WORK,
649      $             RWORK, RESULT4 ) )
650 *
651 *     Do tests (5) and (6): check Schur form of A and compare
652 *     eigenvalues with diagonals.
653 *
654       NTEST = 6
655       TEMP1 = ZERO
656       RESULT5 ) = ZERO
657       RESULT6 ) = ZERO
658 *
659       DO 110 J = 1, MPLUSN
660          ILABAD = .FALSE.
661          TEMP2 = ( ABS1( ALPHA( J )-AI( J, J ) ) /
662      $           MAX( SMLNUM, ABS1( ALPHA( J ) ), ABS1( AI( J, J ) ) )+
663      $           ABS1( BETA( J )-BI( J, J ) ) /
664      $           MAX( SMLNUM, ABS1( BETA( J ) ), ABS1( BI( J, J ) ) ) )
665      $            / ULP
666          IF( J.LT.MPLUSN ) THEN
667             IF( AI( J+1, J ).NE.ZERO ) THEN
668                ILABAD = .TRUE.
669                RESULT5 ) = ULPINV
670             END IF
671          END IF
672          IF( J.GT.1 ) THEN
673             IF( AI( J, J-1 ).NE.ZERO ) THEN
674                ILABAD = .TRUE.
675                RESULT5 ) = ULPINV
676             END IF
677          END IF
678          TEMP1 = MAX( TEMP1, TEMP2 )
679          IF( ILABAD ) THEN
680             WRITE( NOUT, FMT = 9997 )J, MPLUSN, NPTKNT
681          END IF
682   110 CONTINUE
683       RESULT6 ) = TEMP1
684 *
685 *     Test (7) (if sorting worked)  <--------- need to be checked.
686 *
687       NTEST = 7
688       RESULT7 ) = ZERO
689       IF( LINFO.EQ.MPLUSN+3 )
690      $   RESULT7 ) = ULPINV
691 *
692 *     Test (8): compare the estimated value of DIF and its true value.
693 *
694       NTEST = 8
695       RESULT8 ) = ZERO
696       IF( DIFEST( 2 ).EQ.ZERO ) THEN
697          IF( DIFTRU.GT.ABNRM*ULP )
698      $      RESULT8 ) = ULPINV
699       ELSE IF( DIFTRU.EQ.ZERO ) THEN
700          IF( DIFEST( 2 ).GT.ABNRM*ULP )
701      $      RESULT8 ) = ULPINV
702       ELSE IF( ( DIFTRU.GT.THRSH2*DIFEST( 2 ) ) .OR.
703      $         ( DIFTRU*THRSH2.LT.DIFEST( 2 ) ) ) THEN
704          RESULT8 ) = MAX( DIFTRU / DIFEST( 2 ), DIFEST( 2 ) / DIFTRU )
705       END IF
706 *
707 *     Test (9)
708 *
709       NTEST = 9
710       RESULT9 ) = ZERO
711       IF( LINFO.EQ.( MPLUSN+2 ) ) THEN
712          IF( DIFTRU.GT.ABNRM*ULP )
713      $      RESULT9 ) = ULPINV
714          IF( ( IFUNC.GT.1 ) .AND. ( DIFEST( 2 ).NE.ZERO ) )
715      $      RESULT9 ) = ULPINV
716          IF( ( IFUNC.EQ.1 ) .AND. ( PL( 1 ).NE.ZERO ) )
717      $      RESULT9 ) = ULPINV
718       END IF
719 *
720 *     Test (10): compare the estimated value of PL and it true value.
721 *
722       NTEST = 10
723       RESULT10 ) = ZERO
724       IF( PL( 1 ).EQ.ZERO ) THEN
725          IF( PLTRU.GT.ABNRM*ULP )
726      $      RESULT10 ) = ULPINV
727       ELSE IF( PLTRU.EQ.ZERO ) THEN
728          IF( PL( 1 ).GT.ABNRM*ULP )
729      $      RESULT10 ) = ULPINV
730       ELSE IF( ( PLTRU.GT.THRESH*PL( 1 ) ) .OR.
731      $         ( PLTRU*THRESH.LT.PL( 1 ) ) ) THEN
732          RESULT10 ) = ULPINV
733       END IF
734 *
735       NTESTT = NTESTT + NTEST
736 *
737 *     Print out tests which fail.
738 *
739       DO 120 J = 1, NTEST
740          IFRESULT( J ).GE.THRESH ) THEN
741 *
742 *           If this is the first test to fail,
743 *           print a header to the data file.
744 *
745             IF( NERRS.EQ.0 ) THEN
746                WRITE( NOUT, FMT = 9996 )'CGX'
747 *
748 *              Matrix types
749 *
750                WRITE( NOUT, FMT = 9995 )
751 *
752 *              Tests performed
753 *
754                WRITE( NOUT, FMT = 9993 )'unitary''''''transpose',
755      $            ( '''', I = 14 )
756 *
757             END IF
758             NERRS = NERRS + 1
759             IFRESULT( J ).LT.10000.0 ) THEN
760                WRITE( NOUT, FMT = 9990 )NPTKNT, MPLUSN, J, RESULT( J )
761             ELSE
762                WRITE( NOUT, FMT = 9989 )NPTKNT, MPLUSN, J, RESULT( J )
763             END IF
764          END IF
765 *
766   120 CONTINUE
767 *
768   130 CONTINUE
769       GO TO 80
770   140 CONTINUE
771 *
772   150 CONTINUE
773 *
774 *     Summary
775 *
776       CALL ALASVM( 'CGX', NOUT, NERRS, NTESTT, 0 )
777 *
778       WORK( 1 ) = MAXWRK
779 *
780       RETURN
781 *
782  9999 FORMAT' CDRGSX: ', A, ' returned INFO=', I6, '.'/ 9X'N=',
783      $      I6, ', JTYPE=', I6, ')' )
784 *
785  9998 FORMAT' CDRGSX: ', A, ' returned INFO=', I6, '.'/ 9X'N=',
786      $      I6, ', Input Example #', I2, ')' )
787 *
788  9997 FORMAT' CDRGSX: S not in Schur form at eigenvalue ', I6, '.',
789      $      / 9X'N=', I6, ', JTYPE=', I6, ')' )
790 *
791  9996 FORMAT/ 1X, A3, ' -- Complex Expert Generalized Schur form',
792      $      ' problem driver' )
793 *
794  9995 FORMAT'Input Example' )
795 *
796  9994 FORMAT' Matrix types: '/
797      $      '  1:  A is a block diagonal matrix of Jordan blocks ',
798      $      'and B is the identity '/ '      matrix, ',
799      $      / '  2:  A and B are upper triangular matrices, ',
800      $      / '  3:  A and B are as type 2, but each second diagonal ',
801      $      'block in A_11 and '/
802      $      '      each third diaongal block in A_22 are 2x2 blocks,',
803      $      / '  4:  A and B are block diagonal matrices, ',
804      $      / '  5:  (A,B) has potentially close or common ',
805      $      'eigenvalues.'/ )
806 *
807  9993 FORMAT/ ' Tests performed:  (S is Schur, T is triangular, ',
808      $      'Q and Z are ', A, ','/ 19X,
809      $      ' a is alpha, b is beta, and ', A, ' means ', A, '.)',
810      $      / '  1 = | A - Q S Z', A,
811      $      ' | / ( |A| n ulp )      2 = | B - Q T Z', A,
812      $      ' | / ( |B| n ulp )'/ '  3 = | I - QQ', A,
813      $      ' | / ( n ulp )             4 = | I - ZZ', A,
814      $      ' | / ( n ulp )'/ '  5 = 1/ULP  if A is not in ',
815      $      'Schur form S'/ '  6 = difference between (alpha,beta)',
816      $      ' and diagonals of (S,T)'/
817      $      '  7 = 1/ULP  if SDIM is not the correct number of ',
818      $      'selected eigenvalues'/
819      $      '  8 = 1/ULP  if DIFEST/DIFTRU > 10*THRESH or ',
820      $      'DIFTRU/DIFEST > 10*THRESH',
821      $      / '  9 = 1/ULP  if DIFEST <> 0 or DIFTRU > ULP*norm(A,B) ',
822      $      'when reordering fails'/
823      $      ' 10 = 1/ULP  if PLEST/PLTRU > THRESH or ',
824      $      'PLTRU/PLEST > THRESH'/
825      $      '    ( Test 10 is only for input examples )'/ )
826  9992 FORMAT' Matrix order=', I2, ', type=', I2, ', a='E10.4,
827      $      ', order(A_11)=', I2, ', result ', I2, ' is ', 0P, F8.2 )
828  9991 FORMAT' Matrix order=', I2, ', type=', I2, ', a='E10.4,
829      $      ', order(A_11)=', I2, ', result ', I2, ' is ', 0P, E10.4 )
830  9990 FORMAT' Input example #', I2, ', matrix order=', I4, ',',
831      $      ' result ', I2, ' is', 0P, F8.2 )
832  9989 FORMAT' Input example #', I2, ', matrix order=', I4, ',',
833      $      ' result ', I2, ' is', 1P, E10.3 )
834 *
835 *     End of CDRGSX
836 *
837       END