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

NAG FL Interface Introduction
Example description
    Program f08xefe

!     F08XEF Example Program Text

!     Mark 27.2 Release. NAG Copyright 2021.

!     .. Use Statements ..
      Use nag_library, Only: dgeqrf, dggbal, dgghd3, dhgeqz, dormqr, nag_wp,   &
                             x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: i, ifail, ihi, ilo, info, irows,     &
                                          jwork, lda, ldb, ldq, ldz, lwork, n
      Character (1)                    :: compq, compz, job
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), alphai(:), alphar(:),        &
                                          b(:,:), beta(:), lscale(:), q(:,:),  &
                                          rscale(:), tau(:), work(:), z(:,:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: nint
!     .. Executable Statements ..
      Write (nout,*) 'F08XEF Example Program Results'
      Flush (nout)

!     Skip heading in data file

      Read (nin,*)
      Read (nin,*) n
      ldq = 1
      ldz = 1
      lda = n
      ldb = n
      lwork = 6*n
      Allocate (alphai(n),alphar(n),beta(n),a(lda,n),lscale(n),q(ldq,ldq),     &
        rscale(n),b(ldb,n),tau(n),work(lwork),z(ldz,ldz))

!     READ matrix A from data file
      Read (nin,*)(a(i,1:n),i=1,n)

!     READ matrix B from data file
      Read (nin,*)(b(i,1:n),i=1,n)

!     Balance matrix pair (A,B)
      job = 'B'

!     The NAG name equivalent of dggbal is f08whf
      Call dggbal(job,n,a,lda,b,ldb,ilo,ihi,lscale,rscale,work,info)

!     Matrix A after balancing

!     ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04caf('General',' ',n,n,a,lda,'Matrix A after balancing',ifail)

      Write (nout,*)
      Flush (nout)

!     Matrix B after balancing

      ifail = 0
      Call x04caf('General',' ',n,n,b,ldb,'Matrix B after balancing',ifail)

      Write (nout,*)
      Flush (nout)

!     Reduce B to triangular form using QR
      irows = ihi + 1 - ilo

!     The NAG name equivalent of dgeqrf is f08aef
      Call dgeqrf(irows,irows,b(ilo,ilo),ldb,tau,work,lwork,info)

!     Apply the orthogonal transformation to matrix A
!     The NAG name equivalent of dormqr is f08agf
      Call dormqr('L','T',irows,irows,irows,b(ilo,ilo),ldb,tau,a(ilo,ilo),lda, &
        work,lwork,info)

!     Compute the generalized Hessenberg form of (A,B) -> (H,T)
      compq = 'N'
      compz = 'N'

!     The NAG name equivalent of dgghd3 is f08wff
      Call dgghd3(compq,compz,irows,1,irows,a(ilo,ilo),lda,b(ilo,ilo),ldb,q,   &
        ldq,z,ldz,work,lwork,info)

!     Matrix A (H) in generalized Hessenberg form.

      ifail = 0
      Call x04caf('General',' ',n,n,a,lda,'Matrix A in Hessenberg form',ifail)

      Write (nout,*)
      Flush (nout)

!     Matrix B (T) in generalized Hessenberg form.

      ifail = 0
      Call x04caf('General',' ',n,n,b,ldb,'Matrix B is triangular',ifail)

!     Routine DHGEQZ
!     Workspace query: jwork = -1

      jwork = -1
      job = 'E'

!     The NAG name equivalent of dhgeqz is f08xef
      Call dhgeqz(job,compq,compz,n,ilo,ihi,a,lda,b,ldb,alphar,alphai,beta,q,  &
        ldq,z,ldz,work,jwork,info)

      Write (nout,*)
      Write (nout,99999) nint(work(1))
      Write (nout,99998) lwork
      Write (nout,*)

!     Compute the generalized Schur form
!     if the workspace lwork is adequate

      If (nint(work(1))<=lwork) Then

!       The NAG name equivalent of dhgeqz is f08xef
        Call dhgeqz(job,compq,compz,n,ilo,ihi,a,lda,b,ldb,alphar,alphai,beta,  &
          q,ldq,z,ldz,work,lwork,info)

!       Print the generalized eigenvalues

        Write (nout,99997)

        Do i = 1, n
          If (beta(i)/=0.0E0_nag_wp) Then
            Write (nout,99996) i, '(', alphar(i)/beta(i), ',',                 &
              alphai(i)/beta(i), ')'
          Else
            Write (nout,99994) i
          End If
        End Do
      Else
        Write (nout,99995)
      End If

99999 Format (1X,'Minimal required LWORK = ',I6)
99998 Format (1X,'Actual value of  LWORK = ',I6)
99997 Format (1X,'Generalized eigenvalues')
99996 Format (1X,I4,5X,A,F7.3,A,F7.3,A)
99995 Format (1X,'Insufficient workspace allocated for call to F08XEF/DHGEQZ')
99994 Format (1X,I4,'Eigenvalue is infinite')
    End Program f08xefe