Program f08befe
! F08BEF Example Program Text
! Mark 29.2 Release. NAG Copyright 2023.
! .. Use Statements ..
Use nag_library, Only: dgeqpf, dormqr, dtrsv, nag_wp, x04caf
! .. Implicit None Statement ..
Implicit None
! .. Parameters ..
Real (Kind=nag_wp), Parameter :: zero = 0.0E0_nag_wp
Integer, Parameter :: nin = 5, nout = 6
! .. Local Scalars ..
Real (Kind=nag_wp) :: tol
Integer :: i, ifail, info, k, lda, ldb, ldx, &
lwork, m, n, nrhs
! .. Local Arrays ..
Real (Kind=nag_wp), Allocatable :: a(:,:), b(:,:), tau(:), work(:), &
x(:,:)
Integer, Allocatable :: jpvt(:)
! .. Intrinsic Procedures ..
Intrinsic :: abs
! .. Executable Statements ..
Write (nout,*) 'F08BEF Example Program Results'
! Skip heading in data file
Read (nin,*)
Read (nin,*) m, n, nrhs
lda = m
ldb = m
ldx = m
lwork = 64*n
Allocate (a(lda,n),b(ldb,nrhs),tau(n),work(lwork),x(ldx,nrhs),jpvt(n))
! 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)
! Initialize JPVT to be zero so that all columns are free
jpvt(1:n) = 0
! Compute the QR factorization of A
! The NAG name equivalent of dgeqpf is f08bef
Call dgeqpf(m,n,a,lda,jpvt,tau,work,info)
! Choose TOL to reflect the relative accuracy of the input data
tol = 0.01E0_nag_wp
! Determine which columns of R to use
loop: Do k = 1, n
If (abs(a(k,k))<=tol*abs(a(1,1))) Then
Exit loop
End If
End Do loop
! Compute C = (Q**T)*B, storing the result in B
k = k - 1
! The NAG name equivalent of dormqr is f08agf
Call dormqr('Left','Transpose',m,nrhs,n,a,lda,tau,b,ldb,work,lwork,info)
! Compute least squares solution by back-substitution in R*B = C
Do i = 1, nrhs
! The NAG name equivalent of dtrsv is f06pjf
Call dtrsv('Upper','No transpose','Non-Unit',k,a,lda,b(1,i),1)
! Set the unused elements of the I-th solution vector to zero
b(k+1:n,i) = zero
End Do
! Unscramble the least squares solution stored in B
Do i = 1, n
x(jpvt(i),1:nrhs) = b(i,1:nrhs)
End Do
! Print least squares solution
Write (nout,*)
Flush (nout)
! ifail: behaviour on error exit
! =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
ifail = 0
Call x04caf('General',' ',n,nrhs,x,ldx,'Least squares solution',ifail)
End Program f08befe