    PROGRAM f08tcfe

!      F08TCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dspgvd, dtpcon, f06rdf, nag_wp, x02ajf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
       CHARACTER (1), PARAMETER        :: uplo = 'U'
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: anorm, bnorm, eps, rcond, rcondb, t1
       INTEGER                         :: aplen, i, info, j, liwork, lwork, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: ap(:), bp(:), eerbnd(:), w(:), work(:)
       REAL (KIND=nag_wp)              :: dummy(1,1)
       INTEGER                         :: idum(1)
       INTEGER, ALLOCATABLE            :: iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          abs, max, nint
!      .. Executable Statements ..
       WRITE (nout,*) 'F08TCF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       aplen = (n*(n+1))/2
       ALLOCATE (ap(aplen),bp(aplen),eerbnd(n),w(n))

!      Use routine workspace query to get optimal workspace.
       lwork = -1
       liwork = -1
!      The NAG name equivalent of dspgvd is f08tcf
       CALL dspgvd(2,'No vectors',uplo,n,ap,bp,w,dummy,n,dummy,lwork,idum, &
          liwork,info)

!      Make sure that there is at least minimum workspace.
       lwork = max(3*n,nint(dummy(1,1)))
       liwork = max(n,idum(1))
       ALLOCATE (work(lwork),iwork(liwork))

!      Read the upper or lower triangular parts of the matrices A and
!      B from data file

       IF (uplo=='U') THEN
          READ (nin,*) ((ap(i+(j*(j-1))/2),j=i,n),i=1,n)
          READ (nin,*) ((bp(i+(j*(j-1))/2),j=i,n),i=1,n)
       ELSE IF (uplo=='L') THEN
          READ (nin,*) ((ap(i+((2*n-j)*(j-1))/2),j=1,i),i=1,n)
          READ (nin,*) ((bp(i+((2*n-j)*(j-1))/2),j=1,i),i=1,n)
       END IF

!      Compute the one-norms of the symmetric matrices A and B

       anorm = f06rdf('One norm',uplo,n,ap,work)
       bnorm = f06rdf('One norm',uplo,n,bp,work)

!      Solve the generalized symmetric eigenvalue problem
!      A*B*x = lambda*x (itype = 2)

!      In the following call the 9th argument is set to n rather
!      than 1 to avoid an incorrect error message in some vendor
!      versions of LAPACK.
!      The NAG name equivalent of dspgvd is f08tcf
       CALL dspgvd(2,'No vectors',uplo,n,ap,bp,w,dummy,n,work,lwork,iwork, &
          liwork,info)

       IF (info==0) THEN

!         Print solution

          WRITE (nout,*) 'Eigenvalues'
          WRITE (nout,99999) w(1:n)

!         Call DTPCON (F07UGF) to estimate the reciprocal condition
!         number of the Cholesky factor of B.  Note that:
!         cond(B) = 1/rcond**2.  DTPCON requires WORK and IWORK to be
!         of length at least 3*n and n respectively

          CALL dtpcon('One norm',uplo,'Non-unit',n,bp,rcond,work,iwork,info)

!         Print the reciprocal condition number of B

          rcondb = rcond**2
          WRITE (nout,*)
          WRITE (nout,*) 'Estimate of reciprocal condition number for B'
          WRITE (nout,99998) rcondb

!         Get the machine precision, eps, and if rcondb is not less
!         than eps**2, compute error estimates for the eigenvalues

          eps = x02ajf()
          IF (rcond>=eps) THEN
             t1 = anorm*bnorm
             DO i = 1, n
                eerbnd(i) = eps*(t1+abs(w(i))/rcondb)
             END DO

!            Print the approximate error bounds for the eigenvalues

             WRITE (nout,*)
             WRITE (nout,*) 'Error estimates for the eigenvalues'
             WRITE (nout,99998) eerbnd(1:n)
          ELSE
             WRITE (nout,*)
             WRITE (nout,*) 'B is very ill-conditioned, error ', &
                'estimates have not been computed'
          END IF
       ELSE IF (info>n .AND. info<=2*n) THEN
          i = info - n
          WRITE (nout,99997) 'The leading minor of order ', i, &
             ' of B is not positive definite'
       ELSE
          WRITE (nout,99996) 'Failure in DSPGVD. INFO =', info
       END IF

99999  FORMAT (3X,(6F11.4))
99998  FORMAT (4X,1P,6E11.1)
99997  FORMAT (1X,A,I4,A)
99996  FORMAT (1X,A,I4)
    END PROGRAM f08tcfe
