*     F08XAF Example Program Text
*     Mark 21 Release. NAG Copyright 2004.
*     .. Parameters ..
      INTEGER          NIN, NOUT
      PARAMETER        (NIN=5,NOUT=6)
      INTEGER          NB, NMAX
      PARAMETER        (NB=64,NMAX=10)
      INTEGER          LDA, LDB, LDVSL, LDVSR, LWORK
      PARAMETER        (LDA=NMAX,LDB=NMAX,LDVSL=NMAX,LDVSR=NMAX,
     +                 LWORK=8*NMAX+16+NMAX*NB)
*     .. Local Scalars ..
      INTEGER          I, IFAIL, INFO, J, LWKOPT, N, SDIM
*     .. Local Arrays ..
      DOUBLE PRECISION A(LDA,NMAX), ALPHAI(NMAX), ALPHAR(NMAX),
     +                 B(LDB,NMAX), BETA(NMAX), VSL(LDVSL,NMAX),
     +                 VSR(LDVSR,NMAX), WORK(LWORK)
      LOGICAL          BWORK(NMAX)
*     .. External Functions ..
      LOGICAL          DELCTG
      EXTERNAL         DELCTG
*     .. External Subroutines ..
      EXTERNAL         DGGES, X04CAF
*     .. Executable Statements ..
      WRITE (NOUT,*) 'F08XAF Example Program Results'
      WRITE (NOUT,*)
*     Skip heading in data file
      READ (NIN,*)
      READ (NIN,*) N
      IF (N.LE.NMAX) THEN
*
*        Read in the matrices A and B
*
         READ (NIN,*) ((A(I,J),J=1,N),I=1,N)
         READ (NIN,*) ((B(I,J),J=1,N),I=1,N)
*
*        Find the generalized Schur form
*
         CALL DGGES('Vectors (left)','Vectors (right)','Sort',DELCTG,N,
     +              A,LDA,B,LDB,SDIM,ALPHAR,ALPHAI,BETA,VSL,LDVSL,VSR,
     +              LDVSR,WORK,LWORK,BWORK,INFO)
*
         IF (INFO.GT.0 .AND. INFO.NE.(N+2)) THEN
            WRITE (NOUT,99999) 'Failure in DGGES. INFO =', INFO
         ELSE
            WRITE (NOUT,99999)
     +        'Number of eigenvalues for which DELCTG is true = ', SDIM
            WRITE (NOUT,*)
            IF (INFO.EQ.(N+2)) THEN
               WRITE (NOUT,99998) '***Note that rounding errors mean ',
     +           'that leading eigenvalues in the generalized',
     +           'Schur form no longer satisfy DELCTG = .TRUE.'
               WRITE (NOUT,*)
            END IF
*
*           Print out the factors of the generalized Schur factorization
*
            IFAIL = 0
            CALL X04CAF('General',' ',N,N,A,LDA,
     +                  'Generalized Schur matrix S',IFAIL)
*
            WRITE (NOUT,*)
            CALL X04CAF('General',' ',N,N,B,LDB,
     +                  'Generalized Schur matrix T',IFAIL)
*
            WRITE (NOUT,*)
            CALL X04CAF('General',' ',N,N,VSL,LDVSL,
     +                  'Matrix of left generalized Schur vectors',
     +                  IFAIL)
*
            WRITE (NOUT,*)
            CALL X04CAF('General',' ',N,N,VSR,LDVSR,
     +                  'Matrix of right generalized Schur vectors',
     +                  IFAIL)
*
            LWKOPT = WORK(1)
            IF (LWORK.LT.LWKOPT) THEN
               WRITE (NOUT,*)
               WRITE (NOUT,99997) 'Optimum workspace required = ',
     +           LWKOPT, 'Workspace provided         = ', LWORK
            END IF
         END IF
      ELSE
         WRITE (NOUT,*)
         WRITE (NOUT,*) 'NMAX too small'
      END IF
      STOP
*
99999 FORMAT (1X,A,I4)
99998 FORMAT (1X,2A,/1X,A)
99997 FORMAT (1X,A,I5,/1X,A,I5)
      END

      LOGICAL FUNCTION DELCTG(AR,AI,B)
*     .. Scalar Arguments ..
*
*     Logical function DELCTG for use with DGGES (F08XAF)
*
*     Returns the value .TRUE. if the imaginary part of the eigenvalue
*     (AR + AI*i)/B is zero, i.e. the eigenvalue is real
*
      DOUBLE PRECISION        AI, AR, B
*     .. Local Scalars ..
      LOGICAL                 D
*     .. Executable Statements ..
      IF (AI.EQ.0.0D0) THEN
         D = .TRUE.
      ELSE
         D = .FALSE.
      END IF
*
      DELCTG = D
*
      RETURN
      END
