*     F08XBF 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, LIWORK, LWORK
      PARAMETER        (LDA=NMAX,LDB=NMAX,LDVSL=NMAX,LDVSR=NMAX,
     +                 LIWORK=NMAX+6,LWORK=8*(NMAX+1)
     +                 +16+NMAX*NB+NMAX*NMAX/2)
*     .. Local Scalars ..
      DOUBLE PRECISION ABNORM, ANORM, BNORM, EPS, TOL
      INTEGER          I, IFAIL, INFO, J, LWKOPT, N, SDIM
*     .. Local Arrays ..
      DOUBLE PRECISION A(LDA,NMAX), ALPHAI(NMAX), ALPHAR(NMAX),
     +                 B(LDB,NMAX), BETA(NMAX), RCONDE(2), RCONDV(2),
     +                 VSL(LDVSL,NMAX), VSR(LDVSR,NMAX), WORK(LWORK)
      INTEGER          IWORK(LIWORK)
      LOGICAL          BWORK(NMAX)
*     .. External Functions ..
      DOUBLE PRECISION F06BNF, F06RAF, X02AJF
      LOGICAL          DELCTG
      EXTERNAL         F06BNF, F06RAF, X02AJF, DELCTG
*     .. External Subroutines ..
      EXTERNAL         DGGESX, X04CAF
*     .. Executable Statements ..
      WRITE (NOUT,*) 'F08XBF 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 Frobenius norms of A and B
*
         ANORM = F06RAF('Frobenius',N,N,A,LDA,WORK)
         BNORM = F06RAF('Frobenius',N,N,B,LDB,WORK)
*
*        Find the generalized Schur form
*
         CALL DGGESX('Vectors (left)','Vectors (right)','Sort',DELCTG,
     +               'Both reciprocal condition numbers',N,A,LDA,B,LDB,
     +               SDIM,ALPHAR,ALPHAI,BETA,VSL,LDVSL,VSR,LDVSR,RCONDE,
     +               RCONDV,WORK,LWORK,IWORK,LIWORK,BWORK,INFO)
*
         IF (INFO.GT.0 .AND. INFO.NE.(N+2)) THEN
            WRITE (NOUT,99999) 'Failure in DGGESX. INFO =', INFO
         ELSE
            WRITE (NOUT,99999)
     +        'Number of eigenvalues for which DELCTG is true = ', SDIM,
     +        '(dimension of deflating subspaces)'
            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)
*
*           Print out the reciprocal condition numbers
*
            WRITE (NOUT,*)
            WRITE (NOUT,99997)
     +        'Reciprocals of left and right projection norms onto',
     +        'the deflating subspaces for the selected eigenvalues',
     +        'RCONDE(1) = ', RCONDE(1), ', RCONDE(2) = ', RCONDE(2)
            WRITE (NOUT,*)
            WRITE (NOUT,99997)
     +        'Reciprocal condition numbers for the left and right',
     +        'deflating subspaces', 'RCONDV(1) = ', RCONDV(1),
     +        ', RCONDV(2) = ', RCONDV(2)
*
*           Compute the machine precision and sqrt(ANORM**2+BNORM**2)
*
            EPS = X02AJF()
            ABNORM = F06BNF(ANORM,BNORM)
            TOL = EPS*ABNORM
*
*           Print out the approximate asymptotic error bound on the
*           average absolute error of the selected eigenvalues given by
*
*              eps*norm((A, B))/PL,   where PL = RCONDE(1)
*
            WRITE (NOUT,*)
            WRITE (NOUT,99996)
     +        'Approximate asymptotic error bound for selected ',
     +        'eigenvalues    = ', TOL/RCONDE(1)
*
*           Print out an approximate asymptotic bound on the maximum
*           angular error in the computed deflating subspaces given by
*
*              eps*norm((A, B))/DIF(2),   where DIF(2) = RCONDV(2)
*
            WRITE (NOUT,99996)
     +        'Approximate asymptotic error bound for the deflating ',
     +        'subspaces = ', TOL/RCONDV(2)
*
            LWKOPT = WORK(1)
            IF (LWORK.LT.LWKOPT) THEN
               WRITE (NOUT,*)
               WRITE (NOUT,99995) '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,/1X,A)
99998 FORMAT (1X,2A,/1X,A)
99997 FORMAT (1X,A,/1X,A,/1X,2(A,1P,E8.1))
99996 FORMAT (1X,2A,1P,E8.1)
99995 FORMAT (1X,A,I5,/1X,A,I5)
      END

      LOGICAL FUNCTION DELCTG(AR,AI,B)
*     .. Scalar Arguments ..
*
*     Logical function DELCTG for use with DGGESX (F08XBF)
*
*     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
