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

NAG FL Interface Introduction
Example description
    Program f08ngfe

!     F08NGF Example Program Text

!     Mark 27.2 Release. NAG Copyright 2021.

!     .. Use Statements ..
      Use nag_library, Only: blas_damax_val, dgehrd, dhsein, dhseqr, dnrm2,    &
                             dormhr, nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: zero = 0.0_nag_wp
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Complex (Kind=nag_wp)            :: eig, eig1
      Real (Kind=nag_wp)               :: r, thresh
      Integer                          :: i, ifail, info, j, k, l, lda, ldc,   &
                                          ldh, ldvl, ldz, lwork, m, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), c(:,:), h(:,:), tau(:),      &
                                          vl(:,:), wi(:), work(:), wr(:),      &
                                          z(:,:)
      Integer, Allocatable             :: ifaill(:), ifailr(:)
      Logical, Allocatable             :: select(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: aimag, cmplx, conjg, maxloc, real,   &
                                          sqrt, sum
!     .. Executable Statements ..
      Write (nout,*) 'F08NGF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      ldz = 1
      lda = n
      ldc = n
      ldh = n
      ldvl = n
      lwork = 64*n
      Allocate (a(lda,n),c(ldc,n),h(ldh,n),tau(n),vl(ldvl,n),wi(n),            &
        work(lwork),wr(n),z(ldz,1),ifaill(n),ifailr(n),select(n))

!     Read A from data file

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

      Read (nin,*) thresh

!     Reduce A to upper Hessenberg form H = (Q**T)*A*Q
!     The NAG name equivalent of dgehrd is f08nef
      Call dgehrd(n,1,n,a,lda,tau,work,lwork,info)

!     Copy A to H
      h(1:n,1:n) = a(1:n,1:n)

!     Calculate the eigenvalues of H (same as A)
!     The NAG name equivalent of dhseqr is f08pef
      Call dhseqr('Eigenvalues','No vectors',n,1,n,h,ldh,wr,wi,z,ldz,work,     &
        lwork,info)

      Write (nout,*)
      If (info>0) Then
        Write (nout,*) 'Failure to converge.'
      Else
        Write (nout,*) 'Eigenvalues'
        Write (nout,99999)(' (',wr(i),',',wi(i),')',i=1,n)

        Do i = 1, n
          select(i) = wr(i) < thresh
        End Do

!       Calculate the eigenvectors of H (as specified by SELECT),
!       storing the result in C
!       The NAG name equivalent of dhsein is f08pkf
        Call dhsein('Right','QR','No initial vectors',select,n,a,lda,wr,wi,vl, &
          ldvl,c,ldc,n,m,work,ifaill,ifailr,info)

!       Calculate the eigenvectors of A = Q * (eigenvectors of H)
!       The NAG name equivalent of dormhr is f08ngf
        Call dormhr('Left','No transpose',n,m,1,n,a,lda,tau,c,ldc,work,lwork,  &
          info)

!       Print eigenvectors

        Write (nout,*)
        Flush (nout)

!       Normalize selected eigenvectors
        j = 0
        k = 1
        Do While (k<=n)
          If (select(k)) Then
            j = j + 1
            If (wi(k)==0.0_nag_wp) Then
!             Normalize real eigenvector by making largest positive
              Call blas_damax_val(n,c(1,j),1,l,r)
              r = dnrm2(n,c(1,j),1)
              c(1:n,j) = c(1:n,j)/r
              If (c(l,j)<zero) Then
                c(1:n,j) = -c(1:n,j)
              End If
            Else
!             Normalize complex eigenvectors making largest element real
              work(1:n) = c(1:n,j)**2 + c(1:n,j+1)**2
              l = maxloc(work(1:n),1)
              eig1 = cmplx(c(l,j),c(l,j+1),kind=nag_wp)
              eig1 = conjg(eig1)/sqrt(work(l)*sum(work(1:n)))
              Do i = 1, n
                eig = cmplx(c(i,j),c(i,j+1),kind=nag_wp)
                eig = eig*eig1
                c(i,j) = real(eig)
                c(i,j+1) = aimag(eig)
              End Do
              c(l,j+1) = 0.0_nag_wp
              j = j + 1
              k = k + 1
            End If
          End If
          k = k + 1
        End Do

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call x04caf('General',' ',n,m,c,ldc,'Contents of array C',ifail)

      End If

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