    PROGRAM f07cafe

!      F07CAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dgtsv, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: info, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: b(:), d(:), dl(:), du(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F07CAF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n

       ALLOCATE (b(n),d(n),dl(n-1),du(n-1))

!      Read the tridiagonal matrix A and the right hand side B from
!      data file

       READ (nin,*) du(1:n-1)
       READ (nin,*) d(1:n)
       READ (nin,*) dl(1:n-1)
       READ (nin,*) b(1:n)

!      Solve the equations Ax = b for x

!      The NAG name equivalent of dgtsv is f07caf
       CALL dgtsv(n,1,dl,d,du,b,n,info)

       IF (info==0) THEN

!         Print solution

          WRITE (nout,*) 'Solution'
          WRITE (nout,99999) b(1:n)

       ELSE
          WRITE (nout,99998) 'The (', info, ',', info, ')', &
             ' element of the factor U is zero'
       END IF

99999  FORMAT ((1X,7F11.4))
99998  FORMAT (1X,A,I3,A,I3,A,A)
    END PROGRAM f07cafe
