NAG Library Manual, Mark 28.5
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
    Program f08bnfe

!     F08BNF Example Program Text

!     Mark 28.5 Release. NAG Copyright 2022.

!     .. Use Statements ..
      Use nag_library, Only: nag_wp, zgelsy
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nb = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: rcond
      Integer                          :: i, info, lda, ldb, lwork, m, n, nrhs,&
                                          rank
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), b(:), work(:)
      Real (Kind=nag_wp), Allocatable  :: rwork(:)
      Integer, Allocatable             :: jpvt(:)
!     .. Executable Statements ..
      Write (nout,*) 'F08BNF Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n
      lda = max(1,m)
      ldb = max(1,m,n)
      lwork = nb*(n+1)
      Allocate (a(lda,n),b(ldb),work(lwork),rwork(2*n),jpvt(n))

!     Read A and B from data file

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

!     Initialize JPVT to be zero so that all columns are free

      jpvt(1:n) = 0

!     Choose RCOND to reflect the relative accuracy of the input data

      rcond = 0.01_nag_wp

!     Solve the least squares problem min( norm2(b - Ax) ) for the x
!     of minimum norm.

      nrhs = 1

!     The NAG name equivalent of zgelsy is f08bnf
      Call zgelsy(m,n,nrhs,a,lda,b,ldb,jpvt,rcond,rank,work,lwork,rwork,info)

!     Print solution

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

!     Print the effective rank of A

      Write (nout,*)
      Write (nout,*) 'Tolerance used to estimate the rank of A'
      Write (nout,99998) rcond
      Write (nout,*) 'Estimated rank of A'
      Write (nout,99997) rank

99999 Format (4(' (',F7.4,',',F7.4,')',:))
99998 Format (1X,1P,E10.2)
99997 Format (1X,I6)
    End Program f08bnfe