!   F01EMF Example Program Text

!   Mark 25 Release. NAG Copyright 2014.

    Module f01emfe_mod

!     F01EMF Example Program Module:
!            Parameters and User-defined Routines

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                               :: fexp2
    Contains
      Subroutine fexp2(m,iflag,nz,z,fz,iuser,ruser)

!       .. Parameters ..
        Complex (Kind=nag_wp), Parameter     ::                                &
                                              two = (2.0E0_nag_wp,0.0E0_nag_wp)
!       .. Scalar Arguments ..
        Integer, Intent (Inout)              :: iflag
        Integer, Intent (In)                 :: m, nz
!       .. Array Arguments ..
        Complex (Kind=nag_wp), Intent (Out)  :: fz(nz)
        Complex (Kind=nag_wp), Intent (In)   :: z(nz)
        Real (Kind=nag_wp), Intent (Inout)   :: ruser(*)
        Integer, Intent (Inout)              :: iuser(*)
!       .. Intrinsic Procedures ..
        Intrinsic                            :: exp
!       .. Executable Statements ..
        Continue
        fz(1:nz) = (two**m)*exp(two*z(1:nz))
!       Set iflag nonzero to terminate execution for any reason.
        iflag = 0
        Return
      End Subroutine fexp2
    End Module f01emfe_mod
    Program f01emfe

!     F01EMF Example Main Program

!     .. Use Statements ..
      Use nag_library, Only: f01emf, nag_wp, x04caf
      Use f01emfe_mod, Only: fexp2
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter                   :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)                   :: imnorm
      Integer                              :: i, ifail, iflag, lda, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable      :: a(:,:)
      Real (Kind=nag_wp)                   :: ruser(1)
      Integer                              :: iuser(1)
!     .. Executable Statements ..
      Write (nout,*) 'F01EMF Example Program Results'
      Write (nout,*)
      Flush (nout)

!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n

      lda = n
      Allocate (a(lda,n))

!     Read A from data file
      Read (nin,*)(a(i,1:n),i=1,n)

!     Find f( A )
      ifail = 0
      Call f01emf(n,a,lda,fexp2,iuser,ruser,iflag,imnorm,ifail)

!     Print solution
      ifail = 0
      Call x04caf('G','N',n,n,a,lda,'F(A) = EXP(2A)',ifail)

!     Print the norm of the imaginary part to check it is small
      Write (nout,*)
      Write (nout,Fmt='(1X,A,F6.2)') 'Imnorm =', imnorm
    End Program f01emfe