    PROGRAM f04lefe

!      F04LEF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f01lef, f04lef, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: lambda, tol
       INTEGER                         :: ifail, job, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), c(:), d(:), y(:), z(:)
       INTEGER, ALLOCATABLE            :: ipiv(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F04LEF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n
       ALLOCATE (a(n),b(n),c(n),d(n),y(n),z(n),ipiv(n))
       READ (nin,*) a(1:n)
       IF (n>1) THEN
          READ (nin,*) b(2:n)
          READ (nin,*) c(2:n)
       END IF
       tol = 5.0E-5_nag_wp
       lambda = 0.0E0_nag_wp

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 0
       CALL f01lef(n,a,lambda,b,c,tol,d,ipiv,ifail)

       IF (ipiv(n)/=0) THEN
          WRITE (nout,*) 'Matrix is singular or nearly singular'
          WRITE (nout,99999) 'Diagonal element', ipiv(n), 'is small'
       ELSE
          READ (nin,*) y(1:n)
          z(1:n) = y(1:n)
          job = 1

          ifail = 0
          CALL f04lef(job,n,a,b,c,d,ipiv,y,tol,ifail)

          WRITE (nout,*)
          WRITE (nout,*) 'Solution vector for T*X = Y'
          WRITE (nout,99998) y(1:n)
          job = 2

          ifail = 0
          CALL f04lef(job,n,a,b,c,d,ipiv,z,tol,ifail)

          WRITE (nout,*)
          WRITE (nout,*) 'Solution vector for transpose(T)*X = Y'
          WRITE (nout,99998) z(1:n)
       END IF

99999  FORMAT (1X,A,I4,A)
99998  FORMAT (1X,5F9.3)
    END PROGRAM f04lefe
