Program f08ykfe
! F08YKF Example Program Text
! Mark 25 Release. NAG Copyright 2014.
! .. Use Statements ..
Use nag_library, Only: dgeqrf, dggbak, dggbal, dgghrd, dhgeqz, dorgqr, &
dormqr, dtgevc, f06qff, f06qhf, nag_wp, x04caf
! .. Implicit None Statement ..
Implicit None
! .. Parameters ..
Real (Kind=nag_wp), Parameter :: one = 1.0E0_nag_wp
Real (Kind=nag_wp), Parameter :: zero = 0.0E0_nag_wp
Integer, Parameter :: nin = 5, nout = 6
! .. Local Scalars ..
Integer :: i, icols, ifail, ihi, ilo, info, &
irows, jwork, lda, ldb, ldvl, ldvr, &
lwork, m, n
Logical :: ileft, iright
Character (1) :: compq, compz, howmny, job, side
! .. Local Arrays ..
Real (Kind=nag_wp), Allocatable :: a(:,:), alphai(:), alphar(:), &
b(:,:), beta(:), lscale(:), &
rscale(:), tau(:), vl(:,:), vr(:,:), &
work(:)
Logical, Allocatable :: select(:)
! .. Intrinsic Procedures ..
Intrinsic :: nint
! .. Executable Statements ..
Write (nout,*) 'F08YKF Example Program Results'
Flush (nout)
! ileft is TRUE if left eigenvectors are required
! iright is TRUE if right eigenvectors are required
ileft = .True.
iright = .True.
! Skip heading in data file
Read (nin,*)
Read (nin,*) n
lda = n
ldb = n
ldvl = n
ldvr = n
lwork = 6*n
Allocate (a(lda,n),alphai(n),alphar(n),b(ldb,n),beta(n),lscale(n), &
rscale(n),tau(n),vl(ldvl,ldvl),vr(ldvr,ldvr),work(lwork),select(n))
! READ matrix A from data file
Read (nin,*)(a(i,1:n),i=1,n)
! READ matrix B from data file
Read (nin,*)(b(i,1:n),i=1,n)
! Balance matrix pair (A,B)
job = 'B'
! The NAG name equivalent of dggbal is f08whf
Call dggbal(job,n,a,lda,b,ldb,ilo,ihi,lscale,rscale,work,info)
! Matrix A after balancing
! 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 after balancing',ifail)
Write (nout,*)
Flush (nout)
! Matrix B after balancing
ifail = 0
Call x04caf('General',' ',n,n,b,ldb,'Matrix B after balancing',ifail)
Write (nout,*)
Flush (nout)
! Reduce B to triangular form using QR
irows = ihi + 1 - ilo
icols = n + 1 - ilo
! The NAG name equivalent of dgeqrf is f08aef
Call dgeqrf(irows,icols,b(ilo,ilo),ldb,tau,work,lwork,info)
! Apply the orthogonal transformation to matrix A
! The NAG name equivalent of dormqr is f08agf
Call dormqr('L','T',irows,icols,irows,b(ilo,ilo),ldb,tau,a(ilo,ilo),lda, &
work,lwork,info)
! Initialize VL (if left eigenvectors are required)
If (ileft) Then
Call f06qhf('General',n,n,zero,one,vl,ldvl)
Call f06qff('Lower',irows-1,irows-1,b(ilo+1,ilo),ldb,vl(ilo+1,ilo), &
ldvl)
! The NAG name equivalent of dorgqr is f08aff
Call dorgqr(irows,irows,irows,vl(ilo,ilo),ldvl,tau,work,lwork,info)
End If
! Initialize VR (if right eigenvectors are required)
If (iright) Call f06qhf('General',n,n,zero,one,vr,ldvr)
! Compute the generalized Hessenberg form of (A,B)
compq = 'V'
compz = 'V'
! The NAG name equivalent of dgghrd is f08wef
Call dgghrd(compq,compz,n,ilo,ihi,a,lda,b,ldb,vl,ldvl,vr,ldvr,info)
! Matrix A in generalized Hessenberg form
ifail = 0
Call x04caf('General',' ',n,n,a,lda,'Matrix A in Hessenberg form',ifail)
Write (nout,*)
Flush (nout)
! Matrix B in generalized Hessenberg form
ifail = 0
Call x04caf('General',' ',n,n,b,ldb,'Matrix B in Hessenberg form',ifail)
! Routine DHGEQZ
! Workspace query: jwork = -1
jwork = -1
job = 'S'
! The NAG name equivalent of dhgeqz is f08xef
Call dhgeqz(job,compq,compz,n,ilo,ihi,a,lda,b,ldb,alphar,alphai,beta,vl, &
ldvl,vr,ldvr,work,jwork,info)
Write (nout,*)
Write (nout,99999) nint(work(1))
Write (nout,99998) lwork
Write (nout,*)
Write (nout,99997)
Write (nout,99996)
! Compute the generalized Schur form
! if the workspace lwork is adequate
! The Schur form also gives parameters
! required to compute generalized eigenvalues
If (nint(work(1))<=lwork) Then
! The NAG name equivalent of dhgeqz is f08xef
Call dhgeqz(job,compq,compz,n,ilo,ihi,a,lda,b,ldb,alphar,alphai,beta, &
vl,ldvl,vr,ldvr,work,lwork,info)
! Print the generalized eigenvalues
Do i = 1, n
If (beta(i)/=0.0E0_nag_wp) Then
Write (nout,99995) i, '(', alphar(i)/beta(i), ',', &
alphai(i)/beta(i), ')'
Else
Write (nout,99996) i
End If
End Do
Write (nout,*)
Flush (nout)
! Compute left and right generalized eigenvectors
! of the balanced matrix
howmny = 'B'
If (ileft .And. iright) Then
side = 'B'
Else If (ileft) Then
side = 'L'
Else If (iright) Then
side = 'R'
End If
! The NAG name equivalent of dtgevc is f08ykf
Call dtgevc(side,howmny,select,n,a,lda,b,ldb,vl,ldvl,vr,ldvr,n,m,work, &
info)
If (iright) Then
! Compute right eigenvectors of the original matrix
job = 'B'
side = 'R'
! The NAG name equivalent of dggbak is f08wjf
Call dggbak(job,side,n,ilo,ihi,lscale,rscale,n,vr,ldvr,info)
! Normalize the right eigenvectors
Do i = 1, n
vr(1:n,i) = vr(1:n,i)/vr(1,i)
End Do
! Print the right eigenvectors
ifail = 0
Call x04caf('General',' ',n,n,vr,ldvr, &
'Right eigenvectors ',ifail)
Write (nout,*)
Flush (nout)
End If
! Compute left eigenvectors of the original matrix
If (ileft) Then
job = 'B'
side = 'L'
! The NAG name equivalent of dggbak is f08wjf
Call dggbak(job,side,n,ilo,ihi,lscale,rscale,n,vl,ldvl,info)
! Normalize the left eigenvectors
Do i = 1, n
vl(1:n,i) = vl(1:n,i)/vl(1,i)
End Do
! Print the left eigenvectors
ifail = 0
Call x04caf('General',' ',n,n,vl,ldvl,'Left eigenvectors',ifail)
End If
Else
Write (nout,99994)
End If
99999 Format (1X,'Minimal required LWORK = ',I6)
99998 Format (1X,'Actual value of LWORK = ',I6)
99997 Format (1X,'Generalized eigenvalues')
99996 Format (1X,I4,5X,'Infinite eigenvalue')
99995 Format (1X,I4,5X,A,F7.3,A,F7.3,A)
99994 Format (1X,'Insufficient workspace for array WORK'/' in F08XEF/', &
'DHGEQZ')
End Program f08ykfe