    PROGRAM nag_sym_lin_sys_ex03

      ! 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_sym_lin_sys, ONLY : nag_key_sym, nag_sym_lin_fac, &
       nag_sym_lin_sol_fac
      USE nag_write_mat, ONLY : nag_write_gen_mat, nag_write_tri_mat
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC EPSILON, KIND, SCALE
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: det_exp, i, n, nrhs
      REAL (wp) :: rcond
      COMPLEX (wp) :: det_frac
      CHARACTER (1) :: uplo
      ! .. Local Arrays ..
      INTEGER, ALLOCATABLE :: pivot(:)
      REAL (wp), ALLOCATABLE :: bwd_err(:), fwd_err(:)
      COMPLEX (wp), ALLOCATABLE :: a(:,:), a_fac(:,:), b(:,:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_sym_lin_sys_ex03'

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) n, nrhs
      READ (nag_std_in,*) uplo

      ALLOCATE (a(n,n),a_fac(n,n),b(n,nrhs),bwd_err(nrhs),fwd_err(nrhs), &
       pivot(n))                   ! Allocate storage

      a = 0.0_wp
      SELECT CASE (uplo)
      CASE ('L','l')
        READ (nag_std_in,*) (a(i,:i),i=1,n)
      CASE ('U','u')
        READ (nag_std_in,*) (a(i,i:),i=1,n)
      END SELECT
      a_fac = a

      READ (nag_std_in,*) (b(i,:),i=1,n)

      ! Carry out the Bunch-Kaufman factorization

      CALL nag_sym_lin_fac(nag_key_sym,uplo,a_fac,pivot,rcond=rcond, &
       det_frac=det_frac,det_exp=det_exp)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) 'Results of the Bunch-Kaufman factorization'
      WRITE (nag_std_out,*)

      CALL nag_write_tri_mat(uplo,a_fac,format='(F7.4)', &
       title='Details of the Bunch-Kaufman factorization')

      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 = det_frac*SCALE(1.0_wp,det_exp) &
       &='',2X,"(",ES11.3,",",ES11.3,")")') det_frac*SCALE(1.0_wp,det_exp)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'(1X,''kappa(A) (1/rcond)''/9X,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 estimates returned in fwd_err.'
      END IF

      ! Solve the system of equations

      CALL nag_sym_lin_sol_fac(nag_key_sym,uplo,a_fac,pivot,b,a=a, &
       bwd_err=bwd_err,fwd_err=fwd_err)

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

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

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

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

    END PROGRAM nag_sym_lin_sys_ex03
