Example description
    Program f08jvfe

!     F08JVF Example Program Text

!     Mark 27.1 Release. NAG Copyright 2020.

!     .. Use Statements ..
      Use nag_library, Only: dznrm2, nag_wp, x04dbf, zhbtrd, zstedc
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
      Character (1), Parameter         :: uplo = 'U'
!     .. Local Scalars ..
      Complex (Kind=nag_wp)            :: scal
      Integer                          :: i, ifail, info, j, k, kd, ldab, ldz, &
                                          lgn, liwork, lrwork, lwork, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: ab(:,:), work(:), z(:,:)
      Complex (Kind=nag_wp)            :: cdum(1)
      Real (Kind=nag_wp), Allocatable  :: d(:), e(:), rwork(:)
      Real (Kind=nag_wp)               :: rdum(1)
      Integer                          :: idum(1)
      Integer, Allocatable             :: iwork(:)
      Character (1)                    :: clabs(1), rlabs(1)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, ceiling, conjg, log, max,       &
                                          maxloc, min, nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08JVF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n, kd
      ldab = kd + 1
      ldz = n
      lgn = ceiling(log(real(n,kind=nag_wp))/log(2.0_nag_wp))
      Allocate (ab(ldab,n),z(ldz,n),d(n),e(n-1))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
      lrwork = -1
      liwork = -1
!     The NAG name equivalent of zstedc is f08jvf
      Call zstedc('V',n,d,e,z,ldz,cdum,lwork,rdum,lrwork,idum,liwork,info)

!     Make sure that there is enough workspace.
      lwork = max(n*n,nint(real(cdum(1))))
      lrwork = max(1+3*n+2*n*lgn+4*n*n,nint(rdum(1)))
      liwork = max(6+6*n+5*n*lgn,idum(1))
      Allocate (work(lwork),rwork(lrwork),iwork(liwork))

!     Read the upper or lower triangular part of the band matrix A
!     from data file

      If (uplo=='U') Then
        Do i = 1, n
          Read (nin,*)(ab(kd+1+i-j,j),j=i,min(n,i+kd))
        End Do
      Else If (uplo=='L') Then
        Do i = 1, n
          Read (nin,*)(ab(1+i-j,j),j=max(1,i-kd),i)
        End Do
      End If

!     Reduce A to tridiagonal form T = (Z**T)*A*Z, and form Z

!     The NAG name equivalent of zhbtrd is f08hsf
      Call zhbtrd('V',uplo,n,kd,ab,ldab,d,e,z,ldz,work,info)

!     Calculate all the eigenvalues and eigenvectors of A,
!     from T and Z

!     The NAG name equivalent of zstedc is f08jvf
      Call zstedc('V',n,d,e,z,ldz,work,lwork,rwork,lrwork,iwork,liwork,info)

      If (info==0) Then

!       Print eigenvalues and eigenvectors

        Write (nout,*) 'Eigenvalues'
        Write (nout,99999) d(1:n)

        Write (nout,*)
        Flush (nout)

!       Normalize the eigenvectors, largest element real
        Do i = 1, n
          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 x04dbf('General',' ',n,n,z,ldz,'Bracketed','F7.4','Eigenvectors', &
          'Integer',rlabs,'Integer',clabs,80,0,ifail)

      Else
        Write (nout,99998) 'Failure in ZSTEDC. INFO = ', info
      End If

99999 Format (4X,F8.4,3(10X,F8.4))
99998 Format (1X,A,I10)
    End Program f08jvfe