    PROGRAM f08anfe

!      F08ANF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : dznrm2, nag_wp, zgels
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nb = 64, nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: rnorm
       INTEGER                         :: i, info, lda, lwork, m, n
!      .. Local Arrays ..
       COMPLEX (KIND=nag_wp), ALLOCATABLE :: a(:,:), b(:), work(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'F08ANF Example Program Results'
       WRITE (nout,*)
!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) m, n
       lda = m
       lwork = n + nb*m
       ALLOCATE (a(lda,n),b(m),work(lwork))

!      Read A and B from data file

       READ (nin,*) (a(i,1:n),i=1,m)
       READ (nin,*) b(1:m)

!      Solve the least squares problem min( norm2(b - Ax) ) for x
!      The NAG name equivalent of zgels is f08anf
       CALL zgels('No transpose',m,n,1,a,lda,b,m,work,lwork,info)

!      Print solution

       WRITE (nout,*) 'Least squares solution'
       WRITE (nout,99999) b(1:n)

!      Compute and print estimate of the square root of the residual
!      sum of squares

!      The NAG name equivalent of dznrm2 is f06jjf
       rnorm = dznrm2(m-n,b(n+1),1)
       WRITE (nout,*)
       WRITE (nout,*) 'Square root of the residual sum of squares'
       WRITE (nout,99998) rnorm

99999  FORMAT (4(' (',F7.4,',',F7.4,')':))
99998  FORMAT (1X,1P,E10.2)
    END PROGRAM f08anfe
