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

NAG FL Interface Introduction
Example description
    Program f08sbfe

!     F08SBF Example Program Text

!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: blas_damax_val, dsygvx, nag_wp, x04caf
!     .. 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 ..
      Real (Kind=nag_wp)               :: abstol, r, vl, vu
      Integer                          :: i, ifail, il, info, iu, k, lda, ldb, &
                                          ldz, lwork, m, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), b(:,:), w(:), work(:),       &
                                          z(:,:)
      Real (Kind=nag_wp)               :: dummy(1)
      Integer, Allocatable             :: iwork(:), jfail(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max, nint
!     .. Executable Statements ..
      Write (nout,*) 'F08SBF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      lda = n
      ldb = n
      ldz = n
      m = n
      Allocate (a(lda,n),b(ldb,n),w(n),z(ldz,m),iwork(5*n),jfail(n))

!     Read the lower and upper bounds of the interval to be searched.
      Read (nin,*) vl, vu

!     Use routine workspace query to get optimal workspace.
      lwork = -1
!     The NAG name equivalent of dsygvx is f08sbf
      Call dsygvx(1,'Vectors','Values in range','Upper',n,a,lda,b,ldb,vl,vu,   &
        il,iu,abstol,m,w,z,ldz,dummy,lwork,iwork,jfail,info)

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

!     Read the upper triangular parts of the matrices A and B

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

!     Set the absolute error tolerance for eigenvalues.  With ABSTOL
!     set to zero, the default value is used instead

      abstol = zero

!     Solve the generalized symmetric eigenvalue problem
!     A*x = lambda*B*x (ITYPE = 1)

!     The NAG name equivalent of dsygvx is f08sbf
      Call dsygvx(1,'Vectors','Values in range','Upper',n,a,lda,b,ldb,vl,vu,   &
        il,iu,abstol,m,w,z,ldz,work,lwork,iwork,jfail,info)

      If (info>=0 .And. info<=n) Then

!       Print solution

        Write (nout,99999) 'Number of eigenvalues found =', m
        Write (nout,*)
        Write (nout,*) 'Eigenvalues'
        Write (nout,99998) w(1:m)
        Flush (nout)

!       Normalize the eigenvectors, largest positive
        Do i = 1, m
          Call blas_damax_val(n,z(1,i),1,k,r)
          If (z(k,i)<zero) Then
            z(1:n,i) = -z(1:n,i)
          End If
        End Do

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call x04caf('General',' ',n,m,z,ldz,'Selected eigenvectors',ifail)

        If (info>0) Then
          Write (nout,99999) 'INFO eigenvectors failed to converge, INFO =',   &
            info
          Write (nout,*) 'Indices of eigenvectors that did not converge'
          Write (nout,99997) jfail(1:m)
        End If
      Else If (info>n .And. info<=2*n) Then
        i = info - n
        Write (nout,99996) 'The leading minor of order ', i,                   &
          ' of B is not positive definite'
      Else
        Write (nout,99999) 'Failure in DSYGVX. INFO =', info
      End If

99999 Format (1X,A,I5)
99998 Format (3X,(8F8.4))
99997 Format (3X,(8I8))
99996 Format (1X,A,I4,A)
    End Program f08sbfe