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

NAG FL Interface Introduction
Example description
    Program f08apfe

!     F08APF Example Program Text

!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: dznrm2, nag_wp, x04dbf, zgemqrt, zgeqrt, ztrtrs
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nbmax = 64, nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: i, ifail, info, j, lda, ldb, ldt,    &
                                          lwork, m, n, nb, nrhs
!     .. Local Arrays ..
      Complex (Kind=nag_wp), Allocatable :: a(:,:), b(:,:), t(:,:), work(:)
      Real (Kind=nag_wp), Allocatable  :: rnorm(:)
      Character (1)                    :: clabs(1), rlabs(1)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max, min
!     .. Executable Statements ..
      Write (nout,*) 'F08APF Example Program Results'
      Write (nout,*)
      Flush (nout)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, n, nrhs
      lda = m
      ldb = m
      nb = min(m,n,nbmax)
      ldt = nb
      lwork = nb*max(m,n)
      Allocate (a(lda,n),b(ldb,nrhs),t(ldt,min(m,n)),work(lwork),rnorm(nrhs))

!     Read A and B from data file

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

!     Compute the QR factorization of A
!     The NAG name equivalent of zgeqrf is f08apf
      Call zgeqrt(m,n,nb,a,lda,t,ldt,work,info)

!     Compute C = (C1) = (Q**H)*B, storing the result in B
!                  (C2)
!     The NAG name equivalent of zgemqrt is f08aqf
      Call zgemqrt('Left','Conjugate transpose',m,nrhs,n,nb,a,lda,t,ldt,b,ldb, &
        work,info)

!     Compute least squares solutions by back-substitution in
!     R*X = C1
!     The NAG name equivalent of ztrtrs is f07tsf
      Call ztrtrs('Upper','No transpose','Non-Unit',n,nrhs,a,lda,b,ldb,info)

      If (info>0) Then
        Write (nout,*) 'The upper triangular factor, R, of A is singular, '
        Write (nout,*) 'the least squares solution could not be computed'
      Else

!       Print least squares solutions

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call x04dbf('General',' ',n,nrhs,b,ldb,'Bracketed','F7.4',             &
          'Least squares solution(s)','Integer',rlabs,'Integer',clabs,80,0,    &
          ifail)

!       Compute and print estimates of the square roots of the residual
!       sums of squares
!       The NAG name equivalent of dznrm2 is f06jjf
        Do j = 1, nrhs
          rnorm(j) = dznrm2(m-n,b(n+1,j),1)
        End Do

        Write (nout,*)
        Write (nout,*) 'Square root(s) of the residual sum(s) of squares'
        Write (nout,99999) rnorm(1:nrhs)
      End If

99999 Format (3X,1P,7E11.2)
    End Program f08apfe