    PROGRAM f02sdfe

!      F02SDF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : f02sdf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: relep, rmu
       INTEGER                         :: i, ifail, j, k, k1, k2, lda, ldb,    &
                                          lwork, ma, mb, n
       LOGICAL                         :: sym
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:,:), vec(:), work(:)
       REAL (KIND=nag_wp)              :: d(30)
       INTEGER, ALLOCATABLE            :: iwork(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          min
!      .. Executable Statements ..
       WRITE (nout,*) 'F02SDF Example Program Results'
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, ma, mb
       lda = 2*ma + 1
       ldb = 2*mb + 1
       lwork = n*(ma+2)
       ALLOCATE (a(lda,n),b(ldb,n),vec(n),work(lwork),iwork(n))
       DO i = 1, n
          k1 = ma + 1 - min(ma,i-1)
          k2 = ma + 1 + min(ma,n-i)
          READ (nin,*) (a(k,i),k=k1,k2)
       END DO
       DO i = 1, n
          k1 = mb + 1 - min(mb,i-1)
          k2 = mb + 1 + min(mb,n-i)
          READ (nin,*) (b(k,i),k=k1,k2)
       END DO
       READ (nin,*) rmu, d(1)
       sym = .FALSE.
       relep = 0.0E0_nag_wp

!      ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
       ifail = 1
       CALL f02sdf(n,ma+1,mb+1,a,lda,b,ldb,sym,relep,rmu,vec,d,iwork,work, &
          lwork,ifail)

       WRITE (nout,*)
       IF (ifail==0) THEN
          WRITE (nout,99999) 'Corrected eigenvalue = ', rmu + d(30)
          WRITE (nout,*)
          WRITE (nout,*) 'Eigenvector is'
          WRITE (nout,99998) vec(1:n)
       ELSE IF (ifail>0) THEN
          WRITE (nout,99997) 'Error in F02SDF. IFAIL =', ifail
          IF (ifail==7 .OR. ifail==9) THEN
             WRITE (nout,*)
             WRITE (nout,*) 'Successive corrections to RMU were'
             WRITE (nout,*)
             DO j = 1, 29
                IF (d(j)==0.0E0_nag_wp) GO TO 20
                WRITE (nout,99996) d(j)
             END DO
          END IF
       ELSE
          WRITE (nout,99995) ifail
       END IF
20     CONTINUE

99999  FORMAT (1X,A,F8.4)
99998  FORMAT (1X,5F9.4)
99997  FORMAT (1X,A,I5)
99996  FORMAT (1X,E20.4)
99995  FORMAT (1X,' ** F02SDF returned with IFAIL = ',I5)
    END PROGRAM f02sdfe
