Program f08zbfe

!     F08ZBF Example Program Text

!     Mark 26 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: dggglm, 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(:,:), d(:), work(:), x(:), &
                                          y(:)
!     .. Executable Statements ..
      Write (nout,*) 'F08ZBF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n, p
      lda = m
      ldb = m
      lwork = n + m + nb*(m+p)
      Allocate (a(lda,n),b(ldb,p),d(m),work(lwork),x(n),y(p))

!     Read A, B and D from data file

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

!     Solve the weighted least squares problem

!     minimize ||inv(B)*(d - A*x)|| (in the 2-norm)

!     The NAG name equivalent of dggglm is f08zbf
      Call dggglm(m,n,p,a,lda,b,ldb,d,x,y,work,lwork,info)

!     Print least squares solution, x

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

!     Print residual vector y = inv(B)*(d - A*x)

      Write (nout,*)
      Write (nout,*) 'Residual vector'
      Write (nout,99998) y(1:p)

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

!     The NAG name equivalent of dnrm2 is f06ejf
      rnorm = dnrm2(p,y,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,7E11.2)
    End Program f08zbfe