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

NAG FL Interface Introduction
Example description
    Program f08fpfe

!     F08FPF Example Program Text

!     Mark 28.4 Release. NAG Copyright 2022.

!     .. Use Statements ..
      Use nag_library, Only: nag_wp, x04daf, zheevx, zscal
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: zero = 0.0E+0_nag_wp
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: abstol, vl, vu
      Integer                          :: i, ifail, il, info, iu, k, lda, ldz, &
                                          lwork, m, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), work(:), z(:,:)
      Complex (Kind=nag_wp)            :: dummy(1)
      Real (Kind=nag_wp), Allocatable  :: rwork(:), w(:)
      Integer, Allocatable             :: iwork(:), jfail(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, cmplx, conjg, max, maxloc,      &
                                          nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08FPF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldz = n
      m = n
      Allocate (a(lda,n),z(ldz,m),rwork(7*n),w(n),iwork(5*n),jfail(n))

!     Read the lower and upper bounds of the interval to be searched.
      Read (nin,*) vl, vu

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of zheevx is f08fpf
      Call zheevx('Vectors','Values in range','Upper',n,a,lda,vl,vu,il,iu,     &
        abstol,m,w,z,ldz,dummy,lwork,rwork,iwork,jfail,info)

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

!     Read the upper triangular part of the matrix A.

      Read (nin,*)(a(i,i:n),i=1,n)

!     Set the absolute error tolerance for eigenvalues.  With ABSTOL
!     set to zero, the default value is used instead

      abstol = zero

!     Solve the Hermitian eigenvalue problem

!     The NAG name equivalent of zheevx is f08fpf
      Call zheevx('Vectors','Values in range','Upper',n,a,lda,vl,vu,il,iu,     &
        abstol,m,w,z,ldz,work,lwork,rwork,iwork,jfail,info)

      If (info>=0) Then

!       Print solution

        Write (nout,99999) 'Number of eigenvalues found =', m
        Write (nout,*)
        Write (nout,*) 'Eigenvalues'
        Write (nout,99998) w(1:m)
        Flush (nout)

!       Normalize the eigenvectors so that the element of largest absolute
!       value is real.
        Do i = 1, m
          rwork(1:n) = abs(z(1:n,i))
          k = maxloc(rwork(1:n),1)
          Call zscal(n,conjg(z(k,i))/cmplx(abs(z(k,i)),kind=nag_wp),z(1,i),1)
        End Do

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call x04daf('General',' ',n,m,z,ldz,'Selected eigenvectors',ifail)

        If (info>0) Then
          Write (nout,99999) 'INFO eigenvectors failed to converge, INFO =',   &
            info
          Write (nout,*) 'Indices of eigenvectors that did not converge'
          Write (nout,99997) jfail(1:m)
        End If
      Else
        Write (nout,99999) 'Failure in ZHEEVX. INFO =', info
      End If

99999 Format (1X,A,I5)
99998 Format (3X,(8F8.4))
99997 Format (3X,(8I8))
    End Program f08fpfe