NAG Library Manual, Mark 30.2
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
    Program f08xqfe

!     F08XQF Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: f08xnz, m01daf, m01edf, nag_wp, x02ajf, x04daf,   &
                             x04dbf, zgemm, zgges3, zlange => f06uaf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Complex (Kind=nag_wp)            :: alph, bet
      Real (Kind=nag_wp)               :: normd, norme
      Integer                          :: i, ifail, info, lda, ldb, ldc, ldd,  &
                                          lde, ldvsl, ldvsr, lwork, n, sdim
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), alpha(:), b(:,:), beta(:), &
                                          c(:,:), d(:,:), e(:,:), vsl(:,:),    &
                                          vsr(:,:), work(:)
      Complex (Kind=nag_wp)            :: wdum(1)
      Real (Kind=nag_wp), Allocatable  :: rwork(:)
      Integer, Allocatable             :: irank(:)
      Logical, Allocatable             :: bwork(:)
      Character (1)                    :: clabs(1), rlabs(1)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, all, cmplx, max, nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08XQF Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldb = n
      ldc = n
      ldd = n
      lde = n
      ldvsl = n
      ldvsr = n
      Allocate (a(lda,n),alpha(n),b(ldb,n),beta(n),c(ldc,n),d(ldd,n),e(lde,n), &
        vsl(ldvsl,n),vsr(ldvsr,n),rwork(8*n),bwork(n))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of zgges3 is f08xqf
      Call zgges3('Vectors (left)','Vectors (right)','No sort',f08xnz,n,a,lda, &
        b,ldb,sdim,alpha,beta,vsl,ldvsl,vsr,ldvsr,wdum,lwork,rwork,bwork,info)

!     Make sure that there is enough workspace for block size nb.
      lwork = max((nb+1)*n,nint(real(wdum(1))))
      Allocate (work(lwork))

!     Read in the matrices A and B
      Read (nin,*)(a(i,1:n),i=1,n)
      Read (nin,*)(b(i,1:n),i=1,n)

!     Copy A and B into D and E respectively
      d(1:n,1:n) = a(1:n,1:n)
      e(1:n,1:n) = b(1:n,1:n)

!     Print matrices A and B
!     ifail: behaviour on error exit
!            =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04dbf('General',' ',n,n,a,lda,'Bracketed','F8.4','Matrix A',       &
        'Integer',rlabs,'Integer',clabs,80,0,ifail)
      Write (nout,*)
      Flush (nout)

      ifail = 0
      Call x04dbf('General',' ',n,n,b,ldb,'Bracketed','F8.4','Matrix B',       &
        'Integer',rlabs,'Integer',clabs,80,0,ifail)
      Write (nout,*)
      Flush (nout)

!     Find the generalized Schur form
!     The NAG name equivalent of zgges3 is f08xqf
      Call zgges3('Vectors (left)','Vectors (right)','No sort',f08xnz,n,a,lda, &
        b,ldb,sdim,alpha,beta,vsl,ldvsl,vsr,ldvsr,work,lwork,rwork,bwork,info)

      If (info>0) Then
        Write (nout,99999) 'Failure in ZGGES3. INFO =', info
      Else

!       Compute A - Q*S*Z^H from the factorization of (A,B) and store in
!       matrix D
!       The NAG name equivalent of zgemm is f06zaf
        alph = cmplx(1,kind=nag_wp)
        bet = cmplx(0,kind=nag_wp)
        Call zgemm('N','N',n,n,n,alph,vsl,ldvsl,a,lda,bet,c,ldc)
        alph = cmplx(-1,kind=nag_wp)
        bet = cmplx(1,kind=nag_wp)
        Call zgemm('N','C',n,n,n,alph,c,ldc,vsr,ldvsr,bet,d,ldd)

!       Compute B - Q*T*Z^H from the factorization of (A,B) and store in
!       matrix E
        alph = cmplx(1,kind=nag_wp)
        bet = cmplx(0,kind=nag_wp)
        Call zgemm('N','N',n,n,n,alph,vsl,ldvsl,b,ldb,bet,c,ldc)
        alph = cmplx(-1,kind=nag_wp)
        bet = cmplx(1,kind=nag_wp)
        Call zgemm('N','C',n,n,n,alph,c,ldc,vsr,ldvsr,bet,e,lde)

!       Find norms of matrices D and E and warn if either is too large
!       f06uaf is the NAG name equivalent of the LAPACK auxiliary zlange
        normd = zlange('O',ldd,n,d,ldd,rwork)
        norme = zlange('O',lde,n,e,lde,rwork)
        If (normd>x02ajf()**0.75_nag_wp .Or. norme>x02ajf()**0.75_nag_wp) Then
          Write (nout,*)                                                       &
            'Norm of A-(Q*S*Z^H) or norm of B-(Q*T*Z^H) is much greater than 0.'
          Write (nout,*) 'Schur factorization has failed.'
        Else
!         Print generalized eigenvalues
          Write (nout,*) 'Generalized Eigenvalues'

          If (all(abs(beta(1:n))>x02ajf())) Then
            alpha(1:n) = alpha(1:n)/beta(1:n)
!           Reorder eigenvalues by descending absolute value
            rwork(1:n) = abs(alpha(1:n))
            Allocate (irank(n))
            ifail = 0
            Call m01daf(rwork,1,n,'Descending',irank,ifail)
            Call m01edf(alpha,1,n,irank,ifail)
            ifail = 0
            Call x04daf('Gen',' ',1,n,alpha,1,'Eigenvalues:',ifail)
            Write (nout,*)
            Flush (nout)
          Else
            Do i = 1, n
              If (beta(i)/=0.0_nag_wp) Then
                Write (nout,99998) i, alpha(i)/beta(i)
              Else
                Write (nout,99997) i
              End If
            End Do
          End If
        End If
      End If

99999 Format (1X,A,I4)
99998 Format (1X,I2,1X,'(',1P,E11.4,',',E11.4,')')
99997 Format (1X,I4,'Eigenvalue is infinite')
    End Program f08xqfe