    PROGRAM nag_gen_bnd_lin_sys_ex05

      ! Example Program Text for nag_sym_lin_sys
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_gen_bnd_lin_sys, ONLY : nag_gen_bnd_lin_fac, &
       nag_gen_bnd_lin_sol_fac
      USE nag_write_mat, ONLY : nag_write_gen_mat, nag_write_bnd_mat
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC EPSILON, KIND, MAX, MIN, SCALE
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: det_exp, i, j, kl, ku, n, nrhs
      REAL (wp) :: det_frac, rcond
      CHARACTER (1) :: trans
      ! .. Local Arrays ..
      INTEGER, ALLOCATABLE :: pivot(:)
      REAL (wp), ALLOCATABLE :: a(:,:), a_fac(:,:), b(:,:), bwd_err(:), &
       fwd_err(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_gen_lin_sys_ex05'

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) n, kl, ku, nrhs
      READ (nag_std_in,*) trans

      ALLOCATE (a(kl+ku+1,n),b(n,nrhs),a_fac(2*kl+ku+1,n),bwd_err(nrhs), &
       fwd_err(nrhs),pivot(n))     ! Allocate storage

      DO i = 1, n
        READ (nag_std_in,*) (a(ku+1+i-j,j),j=MAX(1,i-kl),MIN(i+ku,n))
      END DO
      READ (nag_std_in,*) (b(i,:),i=1,n)

      ! Carry out LU factorisation

      CALL nag_gen_bnd_lin_fac(ku,a,a_fac,pivot,rcond_1=rcond, &
       det_frac=det_frac,det_exp=det_exp)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) 'Result of the LU factorisation '
      WRITE (nag_std_out,*)

      CALL nag_write_bnd_mat(ku+kl,a_fac,format='(F8.4)',title='Array a_fac')

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) 'Pivotal sequence (pivot)'
      WRITE (nag_std_out,'(2X,10I4:)') pivot
      WRITE (nag_std_out,*)
      WRITE (nag_std_out, &
       '(1X,''determinant = SCALE(det_frac,det_exp) ='',2X,ES11.3)') &
       SCALE(det_frac,det_exp)
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'(1X,''kappa(A) (1/rcond)''/2X,ES11.2)') 1/rcond
      IF (rcond<EPSILON(1.0_wp)) THEN
        WRITE (nag_std_out,*)
        WRITE (nag_std_out,*) ' ** WARNING ** '
        WRITE (nag_std_out,*) 'The matrix is almost singular: the ' // &
         'solution may have no accuracy.'
        WRITE (nag_std_out,*) 'Examine the forward error bounds ' // &
         'estimate returned in fwd_err.'
      END IF

      ! Solve the system of equations

      CALL nag_gen_bnd_lin_sol_fac(ku,a_fac,pivot,b,a=a,trans=trans, &
       bwd_err=bwd_err,fwd_err=fwd_err)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) &
       'Result of the solution of the simultaneous equations'
      WRITE (nag_std_out,*)

      CALL nag_write_gen_mat(b,format='(F9.4)',int_col_labels=.TRUE., &
       title='Solutions (one per column)')

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) 'Backward error bounds'
      WRITE (nag_std_out,'(2X,4ES11.2)') bwd_err
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) 'Forward error bounds (estimates)'
      WRITE (nag_std_out,'(2X,4ES11.2)') fwd_err

      DEALLOCATE (a,a_fac,b,pivot,bwd_err,fwd_err) ! Deallocate storage

    END PROGRAM nag_gen_bnd_lin_sys_ex05
