* F08BVF Example Program Text * Mark 21 Release. NAG Copyright 2004. * .. Parameters .. INTEGER NIN, NOUT PARAMETER (NIN=5,NOUT=6) INTEGER MMAX, NB, NMAX, NRHSMX PARAMETER (MMAX=8,NB=64,NMAX=8,NRHSMX=2) INTEGER LDA, LDB, LWORK PARAMETER (LDA=MMAX,LDB=MMAX,LWORK=(NMAX+1)*NB) COMPLEX *16 ONE, ZERO PARAMETER (ONE=(1.0D0,0.0D0),ZERO=(0.0D0,0.0D0)) * .. Local Scalars .. DOUBLE PRECISION TOL INTEGER I, IFAIL, INFO, J, K, M, N, NRHS * .. Local Arrays .. COMPLEX *16 A(LDA,NMAX), B(LDB,NRHSMX), TAU(NMAX), + WORK(LWORK) DOUBLE PRECISION RNORM(NMAX), RWORK(2*NMAX) INTEGER JPVT(NMAX) CHARACTER CLABS(1), RLABS(1) * .. External Functions .. DOUBLE PRECISION DZNRM2 EXTERNAL DZNRM2 * .. External Subroutines .. EXTERNAL F06DBF, F06THF, X04DBF, ZCOPY, ZGEQP3, ZTRSM, + ZTZRZF, ZUNMQR, ZUNMRZ * .. Intrinsic Functions .. INTRINSIC ABS * .. Executable Statements .. WRITE (NOUT,*) 'F08BVF Example Program Results' WRITE (NOUT,*) * Skip heading in data file READ (NIN,*) READ (NIN,*) M, N, NRHS IF (M.LE.MMAX .AND. N.LE.NMAX .AND. M.GE.N .AND. NRHS.LE.NRHSMX) + THEN * * Read A and B from data file * READ (NIN,*) ((A(I,J),J=1,N),I=1,M) READ (NIN,*) ((B(I,J),J=1,NRHS),I=1,M) * * Initialize JPVT to be zero so that all columns are free * CALL F06DBF(N,0,JPVT,1) * * Compute the QR factorization of A with column pivoting as * A = Q*(R11 R12)*(P**T) * ( 0 R22) * CALL ZGEQP3(M,N,A,LDA,JPVT,TAU,WORK,LWORK,RWORK,INFO) * * Compute C = (C1) = (Q**H)*B, storing the result in B * (C2) * CALL ZUNMQR('Left','Conjugate transpose',M,NRHS,N,A,LDA,TAU,B, + LDB,WORK,LWORK,INFO) * * Choose TOL to reflect the relative accuracy of the input data * TOL = 0.01D0 * * Determine and print the rank, K, of R relative to TOL * DO 20 K = 1, N IF (ABS(A(K,K)).LE.TOL*ABS(A(1,1))) GO TO 40 20 CONTINUE 40 K = K - 1 * WRITE (NOUT,*) 'Tolerance used to estimate the rank of A' WRITE (NOUT,99999) TOL WRITE (NOUT,*) 'Estimated rank of A' WRITE (NOUT,99998) K WRITE (NOUT,*) * * Compute the RZ factorization of the K by K part of R as * (R1 R2) = (T 0)*Z * CALL ZTZRZF(K,N,A,LDA,TAU,WORK,LWORK,INFO) * * Compute least-squares solutions of triangular problems by * back substitution in T*Y1 = C1, storing the result in B * CALL ZTRSM('Left','Upper','No transpose','Non-Unit',K,NRHS,ONE, + A,LDA,B,LDB) * * Compute estimates of the square roots of the residual sums of * squares (2-norm of each of the columns of C2) * DO 60 J = 1, NRHS RNORM(J) = DZNRM2(M-K,B(K+1,J),1) 60 CONTINUE * * Set the remaining elements of the solutions to zero (to give * the minimum-norm solutions), Y2 = 0 * CALL F06THF('General',N-K,NRHS,ZERO,ZERO,B(K+1,1),LDB) * * Form W = (Z**H)*Y * CALL ZUNMRZ('Left','Conjugate transpose',N,NRHS,K,N-K,A,LDA, + TAU,B,LDB,WORK,LWORK,INFO) * * Permute the least-squares solutions stored in B to give X = P*W * DO 100 J = 1, NRHS DO 80 I = 1, N WORK(JPVT(I)) = B(I,J) 80 CONTINUE CALL ZCOPY(N,WORK,1,B(1,J),1) 100 CONTINUE * * Print least-squares solutions * IFAIL = 0 CALL X04DBF('General',' ',N,NRHS,B,LDB,'Bracketed','F7.4', + 'Least-squares solution(s)','Integer',RLABS, + 'Integer',CLABS,80,0,IFAIL) * * Print the square roots of the residual sums of squares * WRITE (NOUT,*) WRITE (NOUT,*) + 'Square root(s) of the residual sum(s) of squares' WRITE (NOUT,99999) (RNORM(J),J=1,NRHS) ELSE WRITE (NOUT,*) + 'One or more of MMAX, NMAX and NRHSMX is too small, ', + 'and/or M.LT.N' END IF * 99999 FORMAT (3X,1P,7E11.2) 99998 FORMAT (1X,I6) END