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

NAG FL Interface Introduction
Example description
    Program f08pefe

!     F08PEF Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: dgemm, dhseqr, dlange => f06raf, nag_wp, x02ajf,  &
                             x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: alpha, beta, norm
      Integer                          :: i, ifail, info, ldc, ldd, ldh, ldz,  &
                                          lwork, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: c(:,:), d(:,:), h(:,:), wi(:),       &
                                          work(:), wr(:), z(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F08PEF Example Program Results'
      Flush (nout)

!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      ldc = n
      ldd = n
      ldh = n
      ldz = n
      lwork = n
      Allocate (c(ldc,n),d(ldd,n),h(ldh,n),wi(n),work(lwork),wr(n),z(ldz,n))

!     Read H from data file
      Read (nin,*)(h(i,1:n),i=1,n)

!     Copy H into D
      d(1:n,1:n) = h(1:n,1:n)

!     Print Matrix H
!     ifail: behaviour on error exit
!            =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04caf('General',' ',n,n,h,ldh,'Matrix H',ifail)

!     Calculate the eigenvalues and Schur factorization of H

!     The NAG name equivalent of dhseqr is f08pef
      Call dhseqr('Schur form','Initialize Z',n,1,n,h,ldh,wr,wi,z,ldz,work,    &
        lwork,info)

      Write (nout,*)
      If (info>0) Then
        Write (nout,*) 'Failure to converge.'
      Else

!       Compute H - Z*T*Z^T from the factorization of A and store in matrix D
!       The NAG name equivalent of dgemm is f06yaf
        alpha = 1.0_nag_wp
        beta = 0.0_nag_wp
        Call dgemm('N','N',n,n,n,alpha,z,ldz,h,ldh,beta,c,ldc)
        alpha = -1.0_nag_wp
        beta = 1.0_nag_wp
        Call dgemm('N','T',n,n,n,alpha,c,ldc,z,ldz,beta,d,ldd)

!       Find norm of matrix D and print warning if it is too large
!       f06raf is the NAG name equivalent of the LAPACK auxiliary dlange
        norm = dlange('O',ldd,n,d,ldd,work)
        If (norm>x02ajf()**0.8_nag_wp) Then
          Write (nout,*) 'Norm of H-(Z*T*Z^T) is much greater than 0.'
          Write (nout,*) 'Schur factorization has failed.'
        Else
!         Print eigenvalues
          Write (nout,*) 'Eigenvalues'
          Write (nout,99999)(' (',wr(i),',',wi(i),')',i=1,n)
        End If
      End If

99999 Format (1X,A,F8.4,A,F8.4,A)
    End Program f08pefe