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

NAG FL Interface Introduction
Example description
    Program f08fnfe

!     F08FNF Example Program Text

!     Mark 28.3 Release. NAG Copyright 2022.

!     .. Use Statements ..
      Use nag_library, Only: ddisna, nag_wp, x02ajf, x04daf, zheev, zscal
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: eerrbd, eps
      Integer                          :: i, ifail, info, k, lda, lwork, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), work(:)
      Complex (Kind=nag_wp)            :: dummy(1)
      Real (Kind=nag_wp), Allocatable  :: rcondz(:), rwork(:), w(:), zerrbd(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, cmplx, conjg, max, maxloc,      &
                                          nint, real
!     .. Executable Statements ..
      Write (nout,*) 'F08FNF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      Allocate (a(lda,n),rcondz(n),rwork(3*n-2),w(n),zerrbd(n))

!     Use routine workspace query to get optimal workspace.
!     The NAG name equivalent of zheev is f08fnf
      lwork = -1
      Call zheev('Vectors','Upper',n,a,lda,w,dummy,lwork,rwork,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 from data file

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

!     Solve the Hermitian eigenvalue problem
!     The NAG name equivalent of zheev is f08fnf
      Call zheev('Vectors','Upper',n,a,lda,w,work,lwork,rwork,info)

      If (info==0) Then

!       Print solution

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

        Write (nout,*)
        Flush (nout)

!       Normalize the eigenvectors so that the element of largest absolute
!       value is real.
        Do i = 1, n
          rwork(1:n) = abs(a(1:n,i))
          k = maxloc(rwork(1:n),1)
          Call zscal(n,conjg(a(k,i))/cmplx(abs(a(k,i)),kind=nag_wp),a(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,n,a,lda,'Eigenvectors',ifail)

!       Get the machine precision, EPS and compute the approximate
!       error bound for the computed eigenvalues.  Note that for
!       the 2-norm, max( abs(W(i)) ) = norm(A), and since the
!       eigenvalues are returned in descending order
!       max( abs(W(i)) ) = max( abs(W(1)), abs(W(n)))

        eps = x02ajf()
        eerrbd = eps*max(abs(w(1)),abs(w(n)))

!       Call DDISNA (F08FLF) to estimate reciprocal condition
!       numbers for the eigenvectors
        Call ddisna('Eigenvectors',n,n,w,rcondz,info)

!       Compute the error estimates for the eigenvectors

        Do i = 1, n
          zerrbd(i) = eerrbd/rcondz(i)
        End Do

!       Print the approximate error bounds for the eigenvalues
!       and vectors

        Write (nout,*)
        Write (nout,*) 'Error estimate for the eigenvalues'
        Write (nout,99998) eerrbd
        Write (nout,*)
        Write (nout,*) 'Error estimates for the eigenvectors'
        Write (nout,99998) zerrbd(1:n)
      Else
        Write (nout,99997) 'Failure in ZHEEV. INFO =', info
      End If

99999 Format (3X,(8F8.4))
99998 Format (4X,1P,6E11.1)
99997 Format (1X,A,I4)
    End Program f08fnfe