Program f08zafe

!     F08ZAF Example Program Text

!     Mark 26.1 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: dgglse, dnrm2, nag_wp
!     .. 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, ldb, lwork, m, n, p
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), b(:,:), c(:), d(:), work(:), &
                                          x(:)
!     .. Executable Statements ..
      Write (nout,*) 'F08ZAF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n, p
      lda = m
      ldb = p
      lwork = p + n + nb*(m+n)
      Allocate (a(lda,n),b(ldb,n),c(m),d(p),work(lwork),x(n))

!     Read A, B, C and D from data file

      Read (nin,*)(a(i,1:n),i=1,m)
      Read (nin,*)(b(i,1:n),i=1,p)
      Read (nin,*) c(1:m)
      Read (nin,*) d(1:p)

!     Solve the equality-constrained least squares problem

!     minimize ||c - A*x|| (in the 2-norm) subject to B*x = D

!     The NAG name equivalent of dgglse is f08zaf
      Call dgglse(m,n,p,a,lda,b,ldb,c,d,x,work,lwork,info)

!     Print least squares solution

      Write (nout,*) 'Constrained least squares solution'
      Write (nout,99999) x(1:n)

!     Compute the square root of the residual sum of squares

!     The NAG name equivalent of dnrm2 is f06ejf
      rnorm = dnrm2(m-n+p,c(n-p+1),1)
      Write (nout,*)
      Write (nout,*) 'Square root of the residual sum of squares'
      Write (nout,99998) rnorm

99999 Format (1X,7F11.4)
99998 Format (3X,1P,E11.2)
    End Program f08zafe