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

NAG FL Interface Introduction
Example description
    Program f08nffe

!     F08NFF Example Program Text

!     Mark 27.3 Release. NAG Copyright 2021.

!     .. Use Statements ..
      Use nag_library, Only: dgehrd, dgemm, dhseqr, dlange => f06raf, dorghr,  &
                             nag_wp, x02ajf, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: alpha, beta, norm
      Integer                          :: i, ifail, info, lda, ldc, ldd, ldz,  &
                                          lwork, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), c(:,:), d(:,:), tau(:),      &
                                          wi(:), work(:), wr(:), z(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F08NFF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldz = n
      ldc = n
      ldd = n
      lwork = 64*(n-1)
      Allocate (a(lda,n),c(ldc,n),d(ldd,n),tau(n),wi(n),work(lwork),wr(n),     &
        z(ldz,n))

!     Read A from data file
      Read (nin,*)(a(i,1:n),i=1,n)

!     Copy A into D.
      d(1:n,1:n) = a(1:n,1:n)

      Write (nout,*)
      Flush (nout)

!     Print Matrix A
!     ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call x04caf('General',' ',n,n,a,lda,'Matrix A',ifail)

      Write (nout,*)
      Flush (nout)

!     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 into Z
      z(1:n,1:n) = a(1:n,1:n)

!     Form Q explicitly, storing the result in Z
!     The NAG name equivalent of dorghr is f08nff
      Call dorghr(n,1,n,z,ldz,tau,work,lwork,info)

!     Calculate the Schur factorization of H = Y*T*(Y**T) and form
!     Q*Y explicitly, storing the result in Z

!     Note that A = Z*T*(Z**T), where Z = Q*Y
!     The NAG name equivalent of dhseqr is f08pef
      Call dhseqr('Schur form','Vectors',n,1,n,a,lda,wr,wi,z,ldz,work,lwork,   &
        info)

!     Compute A - Z*T*Z^T from the factorization of A and store in matrix D.
!     The NAG name equivalent of dgemm is f06yaf.
      alpha = 1.0_nag_wp
      beta = 0.0_nag_wp
      Call dgemm('N','N',n,n,n,alpha,z,ldz,a,lda,beta,c,ldc)
      alpha = -1.0_nag_wp
      beta = 1.0_nag_wp
      Call dgemm('N','T',n,n,n,alpha,c,ldc,z,ldz,beta,d,ldd)

!     Find norm of difference matrix D and warn if it is too large;
!     f06raf is the NAG name equivalent of the LAPACK auxiliary dlange
      norm = dlange('O',ldd,n,d,ldd,work)
      If (norm>x02ajf()**0.8_nag_wp) Then
        Write (nout,*) 'Norm of A-(Z*T*Z^T) is much greater than 0.'
        Write (nout,*) 'Schur factorization has failed.'
      Else
!       Print eigenvalues.
        Write (nout,*) 'Eigenvalues'
        Write (nout,99999)(' (',wr(i),',',wi(i),')',i=1,n)
      End If

99999 Format (1X,A,F8.4,A,F8.4,A)

    End Program f08nffe