Program f08hcfe

!     F08HCF Example Program Text

!     Mark 26.1 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: dsbevd, nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: i, ifail, info, j, kd, ldab, ldz,    &
                                          liwork, lwork, n
      Character (1)                    :: job, uplo
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: ab(:,:), w(:), work(:), z(:,:)
      Integer, Allocatable             :: iwork(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max, min
!     .. Executable Statements ..
      Write (nout,*) 'F08HCF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n, kd
      ldab = kd + 1
      ldz = n
      liwork = 5*n + 3
      lwork = 2*n*n + 5*n + 1
      Allocate (ab(ldab,n),w(n),work(lwork),z(ldz,n),iwork(liwork))

!     Read A from data file

      Read (nin,*) uplo
      If (uplo=='U') Then
        Do i = 1, n
          Read (nin,*)(ab(kd+1+i-j,j),j=i,min(n,i+kd))
        End Do
      Else If (uplo=='L') Then
        Do i = 1, n
          Read (nin,*)(ab(1+i-j,j),j=max(1,i-kd),i)
        End Do
      End If

      Read (nin,*) job

!     Calculate all the eigenvalues and eigenvectors of A
!     The NAG name equivalent of dsbevd is f08hcf
      Call dsbevd(job,uplo,n,kd,ab,ldab,w,z,ldz,work,lwork,iwork,liwork,info)

      Write (nout,*)
      If (info>0) Then
        Write (nout,*) 'Failure to converge.'
      Else

!       Print eigenvalues and eigenvectors

        Write (nout,*) 'Eigenvalues'
        Write (nout,99999) w(1:n)
        Write (nout,*)
        Flush (nout)

!       Standardize the eigenvectors so that first elements are non-negative.
        Do i = 1, n
          If (z(1,i)<0.0_nag_wp) Then
            z(1:n,i) = -z(1:n,i)
          End If
        End Do

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call x04caf('General',' ',n,n,z,ldz,'Eigenvectors',ifail)

      End If

99999 Format (3X,(8F8.4))
    End Program f08hcfe