    PROGRAM f08yvfe

!      F08YVF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp, x04dbf, ztgsyl
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: dif, scale
       INTEGER                         :: i, ifail, ijob, info, lda, ldb, ldc, &
                                          ldd, lde, ldf, lwork, m, n
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), c(:,:), d(:,:),   &
                                             e(:,:), f(:,:), work(:)
       INTEGER, ALLOCATABLE            :: iwork(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08YVF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n
       lda = m
       ldb = n
       ldc = m
       ldd = m
       lde = n
       ldf = m
       lwork = 1
       ALLOCATE (a(lda,m),b(ldb,n),c(ldc,n),d(ldd,m),e(lde,n),f(ldf,n), &
          work(lwork),iwork(m+n+2))

!      Read A, B, D, E, C and F from data file

       READ (nin,*) (a(i,1:m),i=1,m)
       READ (nin,*) (b(i,1:n),i=1,n)
       READ (nin,*) (d(i,1:m),i=1,m)
       READ (nin,*) (e(i,1:n),i=1,n)
       READ (nin,*) (c(i,1:n),i=1,m)
       READ (nin,*) (f(i,1:n),i=1,m)

!      Solve the Sylvester equations
!          A*R - L*B = scale*C and D*R - L*E = scale*F
!      for R and L.

       ijob = 0

!      The NAG name equivalent of ztgsyl is f08yvf
       CALL ztgsyl('No transpose',ijob,m,n,a,lda,b,ldb,c,ldc,d,ldd,e,lde,f, &
          ldf,scale,dif,work,lwork,iwork,info)

       IF (info>=1) THEN
          WRITE (nout,99999)
          WRITE (nout,*)
          FLUSH (nout)
       END IF

!      Print the solution matrices R and L

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL x04dbf('General',' ',m,n,c,ldc,'Bracketed','F7.4', &
          'Solution matrix R','Integer',rlabs,'Integer',clabs,80,0,ifail)

       WRITE (nout,*)
       FLUSH (nout)

       ifail = 0
       CALL x04dbf('General',' ',m,n,f,ldf,'Bracketed','F7.4', &
          'Solution matrix L','Integer',rlabs,'Integer',clabs,80,0,ifail)

       WRITE (nout,*)
       WRITE (nout,99998) 'SCALE = ', scale

99999  FORMAT (/' (A,D) and (B,E) have common or very close eigenval', &
          'ues.'/' Perturbed values were used to solve the equations')
99998  FORMAT (1X,A,1P,E10.2)
    END PROGRAM f08yvfe
