1       SUBROUTINE ZQRT02( M, N, K, A, AF, Q, R, LDA, TAU, WORK, LWORK,
  2      $                   RWORK, RESULT )
  3 *
  4 *  -- LAPACK test routine (version 3.1) --
  5 *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
  6 *     November 2006
  7 *
  8 *     .. Scalar Arguments ..
  9       INTEGER            K, LDA, LWORK, M, N
 10 *     ..
 11 *     .. Array Arguments ..
 12       DOUBLE PRECISION   RESULT* ), RWORK( * )
 13       COMPLEX*16         A( LDA, * ), AF( LDA, * ), Q( LDA, * ),
 14      $                   R( LDA, * ), TAU( * ), WORK( LWORK )
 15 *     ..
 16 *
 17 *  Purpose
 18 *  =======
 19 *
 20 *  ZQRT02 tests ZUNGQR, which generates an m-by-n matrix Q with
 21 *  orthonornmal columns that is defined as the product of k elementary
 22 *  reflectors.
 23 *
 24 *  Given the QR factorization of an m-by-n matrix A, ZQRT02 generates
 25 *  the orthogonal matrix Q defined by the factorization of the first k
 26 *  columns of A; it compares R(1:n,1:k) with Q(1:m,1:n)'*A(1:m,1:k),
 27 *  and checks that the columns of Q are orthonormal.
 28 *
 29 *  Arguments
 30 *  =========
 31 *
 32 *  M       (input) INTEGER
 33 *          The number of rows of the matrix Q to be generated.  M >= 0.
 34 *
 35 *  N       (input) INTEGER
 36 *          The number of columns of the matrix Q to be generated.
 37 *          M >= N >= 0.
 38 *
 39 *  K       (input) INTEGER
 40 *          The number of elementary reflectors whose product defines the
 41 *          matrix Q. N >= K >= 0.
 42 *
 43 *  A       (input) COMPLEX*16 array, dimension (LDA,N)
 44 *          The m-by-n matrix A which was factorized by ZQRT01.
 45 *
 46 *  AF      (input) COMPLEX*16 array, dimension (LDA,N)
 47 *          Details of the QR factorization of A, as returned by ZGEQRF.
 48 *          See ZGEQRF for further details.
 49 *
 50 *  Q       (workspace) COMPLEX*16 array, dimension (LDA,N)
 51 *
 52 *  R       (workspace) COMPLEX*16 array, dimension (LDA,N)
 53 *
 54 *  LDA     (input) INTEGER
 55 *          The leading dimension of the arrays A, AF, Q and R. LDA >= M.
 56 *
 57 *  TAU     (input) COMPLEX*16 array, dimension (N)
 58 *          The scalar factors of the elementary reflectors corresponding
 59 *          to the QR factorization in AF.
 60 *
 61 *  WORK    (workspace) COMPLEX*16 array, dimension (LWORK)
 62 *
 63 *  LWORK   (input) INTEGER
 64 *          The dimension of the array WORK.
 65 *
 66 *  RWORK   (workspace) DOUBLE PRECISION array, dimension (M)
 67 *
 68 *  RESULT  (output) DOUBLE PRECISION array, dimension (2)
 69 *          The test ratios:
 70 *          RESULT(1) = norm( R - Q'*A ) / ( M * norm(A) * EPS )
 71 *          RESULT(2) = norm( I - Q'*Q ) / ( M * EPS )
 72 *
 73 *  =====================================================================
 74 *
 75 *     .. Parameters ..
 76       DOUBLE PRECISION   ZERO, ONE
 77       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+0 )
 78       COMPLEX*16         ROGUE
 79       PARAMETER          ( ROGUE = ( -1.0D+10-1.0D+10 ) )
 80 *     ..
 81 *     .. Local Scalars ..
 82       INTEGER            INFO
 83       DOUBLE PRECISION   ANORM, EPS, RESID
 84 *     ..
 85 *     .. External Functions ..
 86       DOUBLE PRECISION   DLAMCH, ZLANGE, ZLANSY
 87       EXTERNAL           DLAMCH, ZLANGE, ZLANSY
 88 *     ..
 89 *     .. External Subroutines ..
 90       EXTERNAL           ZGEMM, ZHERK, ZLACPY, ZLASET, ZUNGQR
 91 *     ..
 92 *     .. Intrinsic Functions ..
 93       INTRINSIC          DBLEDCMPLXMAX
 94 *     ..
 95 *     .. Scalars in Common ..
 96       CHARACTER*32       SRNAMT
 97 *     ..
 98 *     .. Common blocks ..
 99       COMMON             / SRNAMC / SRNAMT
100 *     ..
101 *     .. Executable Statements ..
102 *
103       EPS = DLAMCH( 'Epsilon' )
104 *
105 *     Copy the first k columns of the factorization to the array Q
106 *
107       CALL ZLASET( 'Full', M, N, ROGUE, ROGUE, Q, LDA )
108       CALL ZLACPY( 'Lower', M-1, K, AF( 21 ), LDA, Q( 21 ), LDA )
109 *
110 *     Generate the first n columns of the matrix Q
111 *
112       SRNAMT = 'ZUNGQR'
113       CALL ZUNGQR( M, N, K, Q, LDA, TAU, WORK, LWORK, INFO )
114 *
115 *     Copy R(1:n,1:k)
116 *
117       CALL ZLASET( 'Full', N, K, DCMPLX( ZERO ), DCMPLX( ZERO ), R,
118      $             LDA )
119       CALL ZLACPY( 'Upper', N, K, AF, LDA, R, LDA )
120 *
121 *     Compute R(1:n,1:k) - Q(1:m,1:n)' * A(1:m,1:k)
122 *
123       CALL ZGEMM( 'Conjugate transpose''No transpose', N, K, M,
124      $            DCMPLX-ONE ), Q, LDA, A, LDA, DCMPLX( ONE ), R,
125      $            LDA )
126 *
127 *     Compute norm( R - Q'*A ) / ( M * norm(A) * EPS ) .
128 *
129       ANORM = ZLANGE( '1', M, K, A, LDA, RWORK )
130       RESID = ZLANGE( '1', N, K, R, LDA, RWORK )
131       IF( ANORM.GT.ZERO ) THEN
132          RESULT1 ) = ( ( RESID / DBLEMAX1, M ) ) ) / ANORM ) / EPS
133       ELSE
134          RESULT1 ) = ZERO
135       END IF
136 *
137 *     Compute I - Q'*Q
138 *
139       CALL ZLASET( 'Full', N, N, DCMPLX( ZERO ), DCMPLX( ONE ), R, LDA )
140       CALL ZHERK( 'Upper''Conjugate transpose', N, M, -ONE, Q, LDA,
141      $            ONE, R, LDA )
142 *
143 *     Compute norm( I - Q'*Q ) / ( M * EPS ) .
144 *
145       RESID = ZLANSY( '1''Upper', N, R, LDA, RWORK )
146 *
147       RESULT2 ) = ( RESID / DBLEMAX1, M ) ) ) / EPS
148 *
149       RETURN
150 *
151 *     End of ZQRT02
152 *
153       END