Program f08jafe

!     F08JAF Example Program Text

!     Mark 26.1 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: ddisna, dstev, nag_wp, x02ajf, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: eerrbd, eps
      Integer                          :: i, ifail, info, ldz, n
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: d(:), e(:), rcondz(:), work(:),      &
                                          z(:,:), zerrbd(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, max
!     .. Executable Statements ..
      Write (nout,*) 'F08JAF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n
      ldz = n
      Allocate (d(n),e(n-1),rcondz(n),work(2*n-2),z(ldz,n),zerrbd(n))

!     Read the diagonal and off-diagonal elements of the matrix A
!     from data file

      Read (nin,*) d(1:n)
      Read (nin,*) e(1:n-1)

!     Solve the symmetric tridiagonal eigenvalue problem
!     The NAG name equivalent of dstev is f08jaf
      Call dstev('Vectors',n,d,e,z,ldz,work,info)

      If (info==0) Then

!       Print solution

        Write (nout,*) 'Eigenvalues'
        Write (nout,99999) d(1:n)
        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)

!       Get the machine precision, EPS and compute the approximate
!       error bound for the computed eigenvalues.  Note that for
!       the 2-norm, max( abs(D(i)) ) = norm(A), and since the
!       eigenvalues are returned in ascending order
!       max( abs(D(i)) ) = max( abs(D(1)), abs(D(n)))

        eps = x02ajf()
        eerrbd = eps*max(abs(d(1)),abs(d(n)))

!       Call DDISNA (F08FLF) to estimate reciprocal condition
!       numbers for the eigenvectors
        Call ddisna('Eigenvectors',n,n,d,rcondz,info)

!       Compute the error estimates for the eigenvectors

        Do i = 1, n
          zerrbd(i) = eerrbd/rcondz(i)
        End Do

!       Print the approximate error bounds for the eigenvalues
!       and vectors

        Write (nout,*)
        Write (nout,*) 'Error estimate for the eigenvalues'
        Write (nout,99998) eerrbd
        Write (nout,*)
        Write (nout,*) 'Error estimates for the eigenvectors'
        Write (nout,99998) zerrbd(1:n)
      Else
        Write (nout,99997) 'Failure in DSTEV. INFO =', info
      End If

99999 Format (3X,(8F8.4))
99998 Format (4X,1P,6E11.1)
99997 Format (1X,A,I4)
    End Program f08jafe