!   C05PCA Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE c05pcae_mod

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

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: factor = 100.0_nag_wp
       INTEGER, PARAMETER                  :: maxfev = 1000, mode = 2, n = 9,  &
                                              nout = 6, nprint = 0
       INTEGER, PARAMETER                  :: nr = n*(n+1)/2
       INTEGER, PARAMETER                  :: ldfjac = n
    CONTAINS
!dec$ attributes offload : mic :: fcn
       SUBROUTINE fcn(n,x,fvec,fjac,ldfjac,iflag,iuser,ruser)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Scalar Arguments ..
          INTEGER, INTENT (INOUT)             :: iflag
          INTEGER, INTENT (IN)                :: ldfjac, n
!         .. Array Arguments ..
          REAL (KIND=nag_wp), INTENT (INOUT)  :: fjac(ldfjac,n), fvec(n),      &
                                                 ruser(*)
          REAL (KIND=nag_wp), INTENT (IN)     :: x(n)
          INTEGER, INTENT (INOUT)             :: iuser(*)
!         .. Local Scalars ..
          INTEGER                             :: k
!         .. Executable Statements ..
          IF (iflag==0) THEN

!            Insert print statements here when NPRINT is positive.

             CONTINUE
          ELSE IF (iflag/=2) THEN
             fvec(1:n) = (ruser(2)+ruser(3)*x(1:n))*x(1:n) - ruser(5)
             fvec(2:n) = fvec(2:n) + ruser(1)*x(1:(n-1))
             fvec(1:(n-1)) = fvec(1:(n-1)) + ruser(4)*x(2:n)
          ELSE
             fjac(1:n,1:n) = 0.0E0_nag_wp

             fjac(1,1) = ruser(2) + 2.0_nag_wp*ruser(3)*x(1)
             fjac(1,2) = ruser(4)
             DO k = 2, n - 1
                fjac(k,k) = ruser(2) + 2.0_nag_wp*ruser(3)*x(k)
                fjac(k,k-1) = ruser(1)
                fjac(k,k+1) = ruser(4)
             END DO
             fjac(n,n-1) = ruser(1)
             fjac(n,n) = ruser(2) + 2.0_nag_wp*ruser(3)*x(n)

          END IF

          RETURN

!dec$ attributes offload : mic :: fcn
       END SUBROUTINE fcn
    END MODULE c05pcae_mod
    PROGRAM c05pcae

!      C05PCA Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : c05pca, dnrm2, nag_wp, x02ajf
       USE c05pcae_mod, ONLY : factor, fcn, ldfjac, maxfev, mode, n, nout,     &
                               nprint, nr
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: fnorm, xtol
       INTEGER                             :: ifail, j, lr, nfev, njev
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: diag(:), fjac(:,:), fvec(:),     &
                                              qtf(:), r(:), x(:)
       REAL (KIND=nag_wp)                  :: ruser(5), w(1,1)
       INTEGER                             :: iuser(1)
!      .. Intrinsic Functions ..
       INTRINSIC                              real, sqrt
!      .. Executable Statements ..
       WRITE (nout,*) 'C05PCA Example Program Results'

       ALLOCATE (diag(n),fjac(ldfjac,n),fvec(n),qtf(n),r(nr),x(n))

!      Store the coefficients describing the system

       ruser(1:5) = real((/-1,3,-2,-2,-1/),kind=nag_wp)

!      The following starting values provide a rough solution.

       x(1:n) = -1.0E0_nag_wp

       xtol = sqrt(x02ajf())
       diag(1:n) = 1.0_nag_wp

       ifail = -1
       CALL c05pca(fcn,n,x,fvec,fjac,ldfjac,xtol,maxfev,diag,mode,factor, &
          nprint,nfev,njev,r,lr,qtf,w,iuser,ruser,ifail)

       SELECT CASE (ifail)
       CASE (0)
!         The NAG name equivalent of dnrm2 is f06ejf
          fnorm = dnrm2(n,fvec,1)
          WRITE (nout,*)
          WRITE (nout,99999) 'Final 2-norm of the residuals =', fnorm
          WRITE (nout,*)
          WRITE (nout,99998) 'Number of function evaluations =', nfev
          WRITE (nout,*)
          WRITE (nout,99998) 'Number of Jacobian evaluations =', njev
          WRITE (nout,*)
          WRITE (nout,*) 'Final approximate solution'
          WRITE (nout,*)
          WRITE (nout,99997) (x(j),j=1,n)
       CASE (2:)
          WRITE (nout,*)
          WRITE (nout,*) 'Approximate solution:'
          WRITE (nout,*)
          WRITE (nout,99997) (x(j),j=1,n)
       END SELECT

99999  FORMAT (1X,A,E12.4)
99998  FORMAT (1X,A,I10)
99997  FORMAT (1X,3F12.4)
    END PROGRAM c05pcae
