    PROGRAM nag_mat_inv_ex21

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

      ! .. Use Statements ..
      USE nag_mat_inv, ONLY : nag_sym_mat_inv_fac
      USE nag_examples_io, ONLY : nag_std_out, nag_std_in
      USE nag_write_mat, ONLY : nag_write_tri_mat
      USE nag_sym_lin_sys, ONLY : nag_key_sym, nag_sym_lin_fac
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, n
      CHARACTER (1) :: uplo
      ! .. Local Arrays ..
      INTEGER, ALLOCATABLE :: pivot(:)
      REAL (wp), ALLOCATABLE :: a(:,:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_mat_inv_ex21'
      WRITE (nag_std_out,*)

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

      ALLOCATE (a(n,n),pivot(n))   ! Allocate storage

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

      CALL nag_sym_lin_fac(nag_key_sym,uplo,a,pivot)

      CALL nag_write_tri_mat(uplo,a,format='F10.4',title='Factorized matrix')

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) 'Pivotal sequence (pivot)'
      WRITE (nag_std_out,'(2X,10I4:)') pivot
      WRITE (nag_std_out,*)

      CALL nag_sym_mat_inv_fac(nag_key_sym,uplo,a,pivot)

      CALL nag_write_tri_mat(uplo,a,format='F10.4',title= &
       'The inverse - using the factorized matrix')

      DEALLOCATE (a,pivot)         ! Deallocate storage

    END PROGRAM nag_mat_inv_ex21
