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

NAG FL Interface Introduction
Example description
    Program f08psfe

!     F08PSF Example Program Text

!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: nag_wp, x02ajf, x04dbf, zgemm, zhseqr,            &
                             zlange => f06uaf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Complex (Kind=nag_wp)            :: alpha, beta
      Real (Kind=nag_wp)               :: norm
      Integer                          :: i, ifail, info, ldc, ldd, ldh, ldz,  &
                                          lwork, n
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: c(:,:), d(:,:), h(:,:), w(:),      &
                                          work(:), z(:,:)
      Real (Kind=nag_wp)               :: rwork(1)
      Character (1)                    :: clabs(1), rlabs(1)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: cmplx
!     .. Executable Statements ..
      Write (nout,*) 'F08PSF Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      ldc = n
      ldd = n
      ldh = n
      ldz = n
      lwork = n
      Allocate (c(ldc,n),d(ldd,n),h(ldh,n),w(n),work(lwork),z(ldz,n))

!     Read H from data file

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

!     Store H in D
      d(1:ldd,1:n) = h(1:ldh,1:n)

!     Print matrix H
!     ifail: behaviour on error exit
!            =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04dbf('General',' ',n,n,h,ldh,'Bracketed','F7.4','Matrix H',       &
        'Integer',rlabs,'Integer',clabs,80,0,ifail)

!     Calculate the eigenvalues and Schur factorization of H

!     The NAG name equivalent of zhseqr is f08psf
      Call zhseqr('Schur form','Initialize Z',n,1,n,h,ldh,w,z,ldz,work,lwork,  &
        info)

      Write (nout,*)
      If (info>0) Then
        Write (nout,*) 'Failure to converge.'
      Else

!       Compute A - Z*T*Z^H from Schur factorization of A, and store in matrix
!       D
!       The NAG name equivalent of zgemm is f06zaf
        alpha = cmplx(1,kind=nag_wp)
        beta = cmplx(0,kind=nag_wp)
        Call zgemm('N','N',n,n,n,alpha,z,ldz,h,ldh,beta,c,ldc)
        alpha = cmplx(-1,kind=nag_wp)
        beta = cmplx(1,kind=nag_wp)
        Call zgemm('N','C',n,n,n,alpha,c,ldc,z,ldz,beta,d,ldd)

!       Find norm of matrix D and print warning if it is too large
!       f06uaf is the NAG name equivalent of the LAPACK auxiliary zlange
        norm = zlange('O',ldd,n,d,ldd,rwork)

        If (norm>x02ajf()**0.5_nag_wp) Then
          Write (nout,*) 'Norm of A-(Z*T*Z^H) is much greater than 0.'
          Write (nout,*) 'Schur factorization has failed.'
        Else
!         Print eigenvalues
          Write (nout,*) 'Eigenvalues'
          Write (nout,99999)(w(i),i=1,n)
        End If

      End If

99999 Format ((3X,4(' (',F7.4,',',F7.4,')',:)))
    End Program f08psfe