    PROGRAM e02gafe

!      E02GAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : e02gaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6, nplus2 = 5
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: resid, t, toler
       INTEGER                         :: i, ifail, irank, iter, lda, m
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:), x(:)
       INTEGER, ALLOCATABLE            :: iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          exp
!      .. Executable Statements ..
       WRITE (nout,*) 'E02GAF Example Program Results'

!      Skip heading in data file
       READ (nin,*)

       READ (nin,*) m
       lda = m + 2
       ALLOCATE (a(lda,nplus2),iwork(m),b(m),x(nplus2))

       DO i = 1, m
          READ (nin,*) t, b(i)
          a(i,1) = exp(t)
          a(i,2) = exp(-t)
       END DO

       a(1:m,3) = 1.0E0_nag_wp
       toler = 0.0E0_nag_wp

       ifail = -1
       CALL e02gaf(m,a,lda,b,nplus2,toler,x,resid,irank,iter,iwork,ifail)

       SELECT CASE (ifail)
       CASE (0,1)
          WRITE (nout,*)
          WRITE (nout,99999) 'Resid = ', resid, '  Rank = ', irank, &
             '  Iterations = ', iter, '  IFAIL =', ifail
          WRITE (nout,*)
          WRITE (nout,*) 'Solution'
          WRITE (nout,99998) x(1:(nplus2-2))
       END SELECT

99999  FORMAT (1X,A,E10.2,A,I5,A,I5,A,I5)
99998  FORMAT (1X,6F10.4)
    END PROGRAM e02gafe
