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

NAG FL Interface Introduction
Example description
    Program f02jqfe

!     F02JQF Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: f02jqf, m01def, m01edf, nag_wp, x04caf, x04daf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: tol = 0.0E0_nag_wp
      Real (Kind=nag_wp), Parameter    :: zero = 0.0E+0_nag_wp
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: t0, t1
      Integer                          :: i, ifail, iwarn, j, lda, ldb, ldc,   &
                                          ldvl, ldvr, n, scal, sense, tdvl,    &
                                          tdvr
      Character (1)                    :: jobvl, jobvr
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), alpha(:), b(:,:), beta(:), &
                                          c(:,:), cvr(:), e(:), vl(:,:),       &
                                          vr(:,:)
      Real (Kind=nag_wp), Allocatable  :: bevl(:), bevr(:), ea(:,:), s(:)
      Integer, Allocatable             :: irank(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, all, maxval, real
!     .. Executable Statements ..
      Write (nout,*) 'F02JQF Example Program Results'
      Flush (nout)
!     Skip heading in data file and read in n, scal, sense, jobVL and jobVR
      Read (nin,*)
      Read (nin,*) n, scal, sense
      Read (nin,*) jobvl, jobvr

      lda = n
      ldb = n
      ldc = n
      ldvl = n
      ldvr = n
      tdvl = 2*n
      tdvr = 2*n
      Allocate (a(lda,n),b(ldb,n),c(ldc,n),alpha(2*n),beta(2*n),e(2*n),        &
        vl(ldvl,tdvl),vr(ldvr,tdvr),s(2*n),bevr(2*n),bevl(2*n),cvr(n),         &
        ea(2*n,2),irank(2*n))

!     Read in the matrices A, B and C

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

!     Solve the quadratic eigenvalue problem
      ifail = -1
      Call f02jqf(scal,jobvl,jobvr,sense,tol,n,a,lda,b,ldb,c,ldc,alpha,beta,   &
        vl,ldvl,vr,ldvr,s,bevl,bevr,iwarn,ifail)
      If (iwarn/=0) Then
        Write (nout,*)
        Write (nout,99999) 'Warning from f02jqf. IWARN =', iwarn
      End If

      Write (nout,*)
      If (ifail/=0) Then
        Write (nout,99999) 'Failure in f02jqf. IFAIL =', ifail
        Go To 100
      End If
      Flush (nout)

      If (all(real(beta(1:2*n))>zero)) Then
        e(1:2*n) = alpha(1:2*n)/beta(1:2*n)
!       Sort eigenvalues by absolute value and then by real part.
!       Add 1000.0 to tie differences of small orders of epsilon.
        ea(1:2*n,1) = 1000.0_nag_wp + abs(e(1:2*n))
        ea(1:2*n,2) = real(e(1:2*n))
        ifail = 0
        Call m01def(ea,2*n,1,2*n,1,2,'Descending',irank,ifail)
        Call m01edf(e,1,2*n,irank,ifail)

!       Print Eigenvalues
        ifail = 0
        Call x04daf('General',' ',1,2*n,e,1,'Eigenvalues:',ifail)

        If (jobvr=='V' .Or. jobvr=='v') Then
!         Sort right eigenvectors using irank
          Do j = 1, n
            e(1:2*n) = vr(j,1:2*n)
            Call m01edf(e,1,2*n,irank,ifail)
            vr(j,1:2*n) = e(1:2*n)
          End Do
        End If
        If (jobvl=='V' .Or. jobvl=='v') Then
!         Sort left eigenvectors using irank
          Do j = 1, n
            e(1:2*n) = vl(j,1:2*n)
            Call m01edf(e,1,2*n,irank,ifail)
            vl(j,1:2*n) = e(1:2*n)
          End Do
        End If
      Else
!       Some eigenvalues are infinite
!       Print alpha and beta
        ifail = 0
        Call x04daf('General',' ',1,2*n,alpha,1,'Alpha:',ifail)
        ifail = 0
        Call x04daf('General',' ',1,2*n,beta,1,'Beta:',ifail)

      End If
      If (jobvr=='V' .Or. jobvr=='v') Then
!       Print Right Eigenvectors
        Write (nout,*)
        Flush (nout)
        ifail = 0
        Call x04daf('G',' ',n,2*n,vr,n,'Right Eigenvectors (columns):',ifail)
      End If
      If (jobvl=='V' .Or. jobvl=='v') Then
!       Print Left Eigenvectors
        Write (nout,*)
        Flush (nout)
        ifail = 0
        Call x04daf('G',' ',n,2*n,vl,n,'Left Eigenvectors (columns):',ifail)
      End If

      If (sense==1 .Or. sense>4) Then
        Write (nout,*)
        Flush (nout)
!       Print Eigenvalues
        ifail = 0
        Call x04caf('G',' ',1,2*n,s,1,'Eigenvalue Condition numbers:',ifail)
      End If

      If (sense==3 .Or. sense==4 .Or. sense>5) Then
        t0 = maxval(bevr)
        Write (nout,*)
        Write (nout,99998)                                                     &
          'Max backward error for eigenvalues and right eigenvectors', t0
      End If

      If (sense==2 .Or. sense==4 .Or. sense==5 .Or. sense==7) Then
        t1 = maxval(bevl)
        Write (nout,*)
        Write (nout,99998)                                                     &
          'Max backward error for eigenvalues and left eigenvectors ', t1
      End If

100   Continue

99999 Format (1X,3(A,I4))
99998 Format (1X,A,1P,E11.1)
    End Program f02jqfe