    PROGRAM f04ccfe

!      F04CCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f04ccf, nag_wp, x04dbf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: errbnd, rcond
       INTEGER                         :: i, ierr, ifail, ldb, n, nrhs
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: b(:,:), d(:), dl(:), du(:), du2(:)
       INTEGER, ALLOCATABLE            :: ipiv(:)
       CHARACTER (1)                   :: clabs(1), rlabs(1)
!      .. Executable Statements ..
       WRITE (nout,*) 'F04CCF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, nrhs
       ldb = n
       ALLOCATE (b(ldb,nrhs),d(n),dl(n-1),du(n-1),du2(n-2),ipiv(n))
!      Read A and B from data file

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

!      Solve the equations AX = B for X

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 1
       CALL f04ccf(n,nrhs,dl,d,du,du2,ipiv,b,ldb,rcond,errbnd,ifail)

       IF (ifail==0) THEN
!         Print solution, estimate of condition number and approximate
!         error bound

          ierr = 0
          CALL x04dbf('General',' ',n,nrhs,b,ldb,'Bracketed',' ','Solution', &
             'Integer',rlabs,'Integer',clabs,80,0,ierr)

          WRITE (nout,*)
          WRITE (nout,*) 'Estimate of condition number'
          WRITE (nout,99999) 1.0E0_nag_wp/rcond
          WRITE (nout,*)
          WRITE (nout,*) 'Estimate of error bound for computed solutions'
          WRITE (nout,99999) errbnd
       ELSE IF (ifail==n+1) THEN
!         Matrix A is numerically singular.  Print estimate of
!         reciprocal of condition number and solution
          WRITE (nout,*)
          WRITE (nout,*) 'Estimate of reciprocal of condition number'
          WRITE (nout,99999) rcond
          WRITE (nout,*)
          FLUSH (nout)

          ierr = 0
          CALL x04dbf('General',' ',n,nrhs,b,ldb,'Bracketed',' ','Solution', &
             'Integer',rlabs,'Integer',clabs,80,0,ierr)

       ELSE IF (ifail>0 .AND. ifail<=n) THEN
!         The upper triangular matrix U is exactly singular.  Print
!         details of factorization
          WRITE (nout,*) 'Details of factorization'
          WRITE (nout,*)
          WRITE (nout,*) ' Second super-diagonal of U'
          WRITE (nout,99998) du2(1:n-2)
          WRITE (nout,*)
          WRITE (nout,*) ' First super-diagonal of U'
          WRITE (nout,99998) du(1:n-1)
          WRITE (nout,*)
          WRITE (nout,*) ' Main diagonal of U'
          WRITE (nout,99998) d(1:n)
          WRITE (nout,*)
          WRITE (nout,*) ' Multipliers'
          WRITE (nout,99998) dl(1:n-1)
          WRITE (nout,*)
          WRITE (nout,*) ' Vector of interchanges'
          WRITE (nout,99997) ipiv(1:n)
       ELSE
          WRITE (nout,99996) ifail
       END IF

99999  FORMAT (8X,1P,E9.1)
99998  FORMAT (4(1X,'(',F7.4,',',F7.4,')':))
99997  FORMAT (1X,8I9)
99996  FORMAT (1X,' ** F04CCF returned with IFAIL = ',I5)
    END PROGRAM f04ccfe
