Program f04bbfe

!     F04BBF Example Program Text

!     Mark 25 Release. NAG Copyright 2014.

!     .. Use Statements ..
      Use nag_library, Only: f04bbf, nag_wp, x04caf, x04cef
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: errbnd, rcond
      Integer                          :: i, ierr, ifail, j, k, kl, ku, ldab,  &
                                          ldb, n, nrhs
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: ab(:,:), b(:,:)
      Integer, Allocatable             :: ipiv(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max, min
!     .. Executable Statements ..
      Write (nout,*) 'F04BBF Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n, kl, ku, nrhs
      ldab = 2*kl + ku + 1
      ldb = n
      Allocate (ab(ldab,n),b(ldb,nrhs),ipiv(n))
!     Read A and B from data file
      k = kl + ku + 1
      Read (nin,*)((ab(k+i-j,j),j=max(i-kl,1),min(i+ku,n)),i=1,n)
      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 f04bbf(n,kl,ku,nrhs,ab,ldab,ipiv,b,ldb,rcond,errbnd,ifail)

      If (ifail==0) Then

!       Print solution, estimate of condition number and approximate
!       error bound

        ierr = 0
        Call x04caf('General',' ',n,nrhs,b,ldb,'Solution',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 x04caf('General',' ',n,nrhs,b,ldb,'Solution',ierr)

      Else If (ifail>0 .And. ifail<=n) Then

!       The upper triangular matrix U is exactly singular.  Print
!       details of factorization
        Write (nout,*)
        Flush (nout)

        ierr = 0
        Call x04cef(n,n,kl,kl+ku,ab,ldab,'Details of factorization',ierr)

!       Print pivot indices
        Write (nout,*)
        Write (nout,*) 'Pivot indices'
        Write (nout,99998) ipiv(1:n)
      Else
        Write (nout,99997) ifail
      End If

99999 Format (6X,1P,E9.1)
99998 Format ((1X,7I11))
99997 Format (1X,' ** F04BBF returned with IFAIL = ',I5)
    End Program f04bbfe