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

NAG FL Interface Introduction
Example description
    Program f08gpfe

!     F08GPF Example Program Text

!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: dznrm2, nag_wp, x04daf, zhpevx
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: zero = 0.0E+0_nag_wp
      Integer, Parameter               :: nin = 5, nout = 6
      Character (1), Parameter         :: uplo = 'U'
!     .. Local Scalars ..
      Complex (Kind=nag_wp)            :: scal
      Real (Kind=nag_wp)               :: abstol, vl, vu
      Integer                          :: i, ifail, il, info, iu, j, k, ldz,   &
                                          m, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: ap(:), work(:), z(:,:)
      Real (Kind=nag_wp), Allocatable  :: rwork(:), w(:)
      Integer, Allocatable             :: iwork(:), jfail(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, conjg, maxloc
!     .. Executable Statements ..
      Write (nout,*) 'F08GPF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      ldz = n
      m = n
      Allocate (ap((n*(n+1))/2),work(2*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,
!     and read the upper or lower triangular part of the matrix A
!     from data file

      Read (nin,*) vl, vu
      If (uplo=='U') Then
        Read (nin,*)((ap(i+(j*(j-1))/2),j=i,n),i=1,n)
      Else If (uplo=='L') Then
        Read (nin,*)((ap(i+((2*n-j)*(j-1))/2),j=1,i),i=1,n)
      End If

!     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 zhpevx is f08gpf
      Call zhpevx('Vectors','Values in range',uplo,n,ap,vl,vu,il,iu,abstol,m,  &
        w,z,ldz,work,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, largest element real
        Do i = 1, m
          rwork(1:n) = abs(z(1:n,i))
          k = maxloc(rwork(1:n),1)
          scal = conjg(z(k,i))/abs(z(k,i))/dznrm2(n,z(1,i),1)
          z(1:n,i) = z(1:n,i)*scal
        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 ZHPEVX. INFO =', info
      End If

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