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

NAG FL Interface Introduction
Example description
    Program f08wafe

!     F08WAF Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: dggev, dnrm2, m01def, m01eaf, nag_wp, x02ajf,     &
                             x02amf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: zero = 0.0_nag_wp
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Complex (Kind=nag_wp)            :: eig
      Real (Kind=nag_wp)               :: d, scal_i, scal_r, small
      Integer                          :: i, ifail, info, j, k, lda, ldb,      &
                                          ldvr, lwork, n
      Logical                          :: pair
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), alphai(:), alphar(:),        &
                                          b(:,:), beta(:), vr(:,:), vr_row(:), &
                                          work(:)
      Real (Kind=nag_wp)               :: dummy(1,1)
      Integer, Allocatable             :: irank(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, all, cmplx, max, maxloc, nint,  &
                                          sqrt
!     .. Executable Statements ..
      Write (nout,*) 'F08WAF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldb = n
      ldvr = n
      Allocate (a(lda,n),alphai(n),alphar(n),b(ldb,n),beta(n),vr(ldvr,n),      &
        irank(n))

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of dggev is f08waf
      Call dggev('No left vectors','Vectors (right)',n,a,lda,b,ldb,alphar,     &
        alphai,beta,dummy,1,vr,ldvr,dummy,lwork,info)

!     Make sure that there is enough workspace for block size nb.
      lwork = max((nb+7)*n,nint(dummy(1,1)))
      Allocate (work(lwork))

!     Read in the matrices A and B

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

!     Solve the generalized eigenvalue problem

!     The NAG name equivalent of dggev is f08waf
      Call dggev('No left vectors','Vectors (right)',n,a,lda,b,ldb,alphar,     &
        alphai,beta,dummy,1,vr,ldvr,work,lwork,info)

      If (info>0) Then
        Write (nout,*)
        Write (nout,99999) 'Failure in DGGEV. INFO =', info
      Else
!       If beta(:) > eps, Order eigenvalues by ascending real parts
!       and then by ascending imaginary parts
        If (all(abs(beta(1:n))>x02ajf())) Then
!         Make sure real parts of conjugate pair are exactly equal
          j = 0
          Do While (j<n)
            j = j + 1
            If (alphai(j)/=zero) Then
              alphar(j+1) = alphar(j)
              alphai(j+1) = -alphai(j)
              beta(j+1) = beta(j)
              j = j + 1
            End If
          End Do
          work(1:n) = alphar(1:n)/beta(1:n)
          work(n+1:2*n) = alphai(1:n)/beta(1:n)
          ifail = 0
          Call m01def(work,n,1,n,1,2,'Ascending',irank,ifail)
          Call m01eaf(alphar,1,n,irank,ifail)
          Call m01eaf(alphai,1,n,irank,ifail)
          Call m01eaf(beta,1,n,irank,ifail)
!         Order the eigenvectors in the same way
          Allocate (vr_row(n))
          Do j = 1, n
            vr_row(1:n) = vr(j,1:n)
            Call m01eaf(vr_row,1,n,irank,ifail)
            vr(j,1:n) = vr_row(1:n)
          End Do
          Deallocate (vr_row)
        End If
        small = x02amf()
        pair = .False.
        Do j = 1, n
          Write (nout,*)
          If ((abs(alphar(j))+abs(alphai(j)))*small>=abs(beta(j))) Then
            Write (nout,99998) 'Eigenvalue(', j, ')',                          &
              ' is numerically infinite or undetermined', 'ALPHAR(', j,        &
              ') = ', alphar(j), ', ALPHAI(', j, ') = ', alphai(j), ', BETA(', &
              j, ') = ', beta(j)
          Else
            If (alphai(j)==zero) Then
              Write (nout,99997) 'Eigenvalue(', j, ') = ', alphar(j)/beta(j)
            Else
              eig = cmplx(alphar(j),alphai(j),kind=nag_wp)/                    &
                cmplx(beta(j),kind=nag_wp)
              Write (nout,99996) 'Eigenvalue(', j, ') = ', eig
            End If
          End If
          Write (nout,*)
          Write (nout,99995) 'Eigenvector(', j, ')'
          If (alphai(j)==zero) Then
!           Let largest element be positive
            work(1:n) = abs(vr(1:n,j))
            k = maxloc(work(1:n),1)
            If (vr(k,j)<zero) Then
              vr(1:n,j) = -vr(1:n,j)
            End If
            d = dnrm2(n,vr(1,j),1)
            vr(1:n,j) = vr(1:n,j)/d
            Write (nout,99994)(vr(i,j),i=1,n)
          Else
            If (pair) Then
              Write (nout,99993)(vr(i,j-1),-vr(i,j),i=1,n)
            Else
!             Let largest element be real (and positive).
              work(1:n) = vr(1:n,j)**2 + vr(1:n,j+1)**2
              work(1:n) = sqrt(work(1:n))
              d = dnrm2(n,work,1)
              k = maxloc(work(1:n),1)
              scal_r = (vr(k,j)/work(k))/d
              scal_i = (-vr(k,j+1)/work(k))/d
              work(1:n) = vr(1:n,j)
              vr(1:n,j) = scal_r*work(1:n) - scal_i*vr(1:n,j+1)
              vr(1:n,j+1) = scal_r*vr(1:n,j+1) + scal_i*work(1:n)
              Write (nout,99993)(vr(i,j),vr(i,j+1),i=1,n)
            End If
            pair = .Not. pair
          End If
        End Do

      End If

99999 Format (1X,A,I4)
99998 Format (1X,A,I2,2A,/,1X,2(A,I2,A,F11.3,3X),A,I2,A,F11.3)
99997 Format (1X,A,I2,A,F11.3)
99996 Format (1X,A,I2,A,'(',F11.3,',',F11.3,')')
99995 Format (1X,A,I2,A)
99994 Format (1X,F11.5)
99993 Format (1X,'(',F11.5,',',F11.5,')')
    End Program f08wafe