*     F04BBF Example Program Text
*     Mark 21 Release. NAG Copyright 2004.
*     .. Parameters ..
      INTEGER          NIN, NOUT
      PARAMETER        (NIN=5,NOUT=6)
      INTEGER          NMAX, KLMAX, KUMAX, NRHSMX
      PARAMETER        (NMAX=8,KLMAX=4,KUMAX=4,NRHSMX=2)
      INTEGER          LDAB, LDB
      PARAMETER        (LDAB=2*KLMAX+KUMAX+1,LDB=NMAX)
*     .. Local Scalars ..
      DOUBLE PRECISION ERRBND, RCOND
      INTEGER          I, IERR, IFAIL, J, K, KL, KU, N, NRHS
*     .. Local Arrays ..
      DOUBLE PRECISION AB(LDAB,NMAX), B(LDB,NRHSMX)
      INTEGER          IPIV(NMAX)
*     .. External Subroutines ..
      EXTERNAL         F04BBF, X04CAF, X04CEF
*     .. Intrinsic Functions ..
      INTRINSIC        MAX, MIN
*     .. Executable Statements ..
      WRITE (NOUT,*) 'F04BBF Example Program Results'
      WRITE (NOUT,*)
*     Skip heading in data file
      READ (NIN,*)
      READ (NIN,*) N, KL, KU, NRHS
      IF (N.LE.NMAX .AND. KL.LE.KLMAX .AND. KU.LE.KUMAX .AND. NRHS.LE.
     +    NRHSMX) THEN
*
*        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,J),J=1,NRHS),I=1,N)
*
*        Solve the equations AX = B for X
*
         IFAIL = 1
         CALL F04BBF(N,KL,KU,NRHS,AB,LDAB,IPIV,B,LDB,RCOND,ERRBND,IFAIL)
*
         IF (IFAIL.EQ.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.0D0/RCOND
            WRITE (NOUT,*)
            WRITE (NOUT,*)
     +        'Estimate of error bound for computed solutions'
            WRITE (NOUT,99999) ERRBND
         ELSE IF (IFAIL.EQ.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,*)
            IERR = 0
            CALL X04CAF('General',' ',N,NRHS,B,LDB,'Solution',IERR)
*
         ELSE IF (IFAIL.GT.0 .AND. IFAIL.LE.N) THEN
*
*           The upper triangular matrix U is exactly singular.  Print
*           details of factorization
*
            WRITE (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(I),I=1,N)
         ELSE
            WRITE (NOUT,99997) IFAIL
         END IF
      ELSE
         WRITE (NOUT,*)
     +     'One or more of NMAX, KLMAX, KUMAX or NRHSMX is too small'
      END IF
*
99999 FORMAT (6X,1P,E9.1)
99998 FORMAT ((1X,7I11))
99997 FORMAT (1X,' ** F04BBF returned with IFAIL = ',I5)
      END
