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

NAG FL Interface Introduction
Example description
    Program f08qgfe

!     F08QGF Example Program Text

!     Mark 28.4 Release. NAG Copyright 2022.

!     .. Use Statements ..
      Use nag_library, Only: dgemm, dlange => f06raf, dtrsen, 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, s, sep
      Integer                          :: i, ifail, info, lda, ldc, ldq, ldt,  &
                                          liwork, lwork, m, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), c(:,:), q(:,:), t(:,:),      &
                                          wi(:), work(:), wr(:)
      Integer, Allocatable             :: iwork(:)
      Logical, Allocatable             :: select(:)
!     .. Executable Statements ..
      Write (nout,*) 'F08QGF Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldc = n
      ldq = n
      ldt = n
      liwork = (n*n)/4
      lwork = (n*n)/2
      Allocate (a(lda,n),c(ldc,n),q(ldq,n),t(ldt,n),wi(n),work(lwork),wr(n),   &
        iwork(liwork),select(n))

!     Read T, Q and the logical array SELECT from data file

      Read (nin,*)(t(i,1:n),i=1,n)
      Read (nin,*)(q(i,1:n),i=1,n)

      Read (nin,*) select(1:n)

!     Compute Q * T * Q**T to find  A
!     The NAG name equivalent of dgemm is f06yaf
      alpha = 1._nag_wp
      beta = 0._nag_wp
      Call dgemm('N','N',n,n,n,alpha,q,ldq,t,ldt,beta,c,ldc)
      Call dgemm('N','T',n,n,n,alpha,c,ldc,q,ldq,beta,a,lda)

!     Print Matrix A, as computed from Q * T * Q**T
!     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 computed from Q*T*Q^T',    &
        ifail)

      Write (nout,*)
      Flush (nout)

!     Reorder the Schur factor T and update the matrix Q to obtain TT and QT

!     The NAG name equivalent of dtrsen is f08qgf
      Call dtrsen('Both','Vectors',select,n,t,ldt,q,ldq,wr,wi,m,s,sep,work,    &
        lwork,iwork,liwork,info)

!     Compute (Q * T * Q^T) - (QT * TT * QT^T) and store in A,
!     i.e. the difference between reconstructed A using Schur and reordered
!          Schur decompositions.
      alpha = 1._nag_wp
      beta = 0._nag_wp
      Call dgemm('N','N',n,n,n,alpha,q,ldq,t,ldt,beta,c,ldc)
      alpha = -1._nag_wp
      beta = 1._nag_wp
      Call dgemm('N','T',n,n,n,alpha,c,ldc,q,ldq,beta,a,lda)

!     Find norm of difference matrix and print warning if it is too large
!     f06raf is the NAG name equivalent of the LAPACK auxiliary dlange
      norm = dlange('O',lda,n,a,lda,work)
      If (norm>x02ajf()**0.8_nag_wp) Then
        Write (nout,*) 'Norm of A - (QT * TT * QT^T) is much greater than 0.'
        Write (nout,*) 'Schur factorization has failed.'
      Else
!       Print Result
        Write (nout,99999) 'Condition number estimate',                        &
          ' of the selected cluster of eigenvalues = ', 1.0_nag_wp/s
        Write (nout,*)
        Write (nout,99999) 'Condition number estimate of the spec',            &
          'ified invariant subspace    = ', 1.0_nag_wp/sep
      End If

99999 Format (1X,A,A,1P,E10.2)
    End Program f08qgfe