```001:       SUBROUTINE ZUNGLQ( M, N, K, A, LDA, TAU, WORK, LWORK, INFO )
002: *
003: *  -- LAPACK routine (version 3.2) --
004: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
005: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       INTEGER            INFO, K, LDA, LWORK, M, N
010: *     ..
011: *     .. Array Arguments ..
012:       COMPLEX*16         A( LDA, * ), TAU( * ), WORK( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  ZUNGLQ generates an M-by-N complex matrix Q with orthonormal rows,
019: *  which is defined as the first M rows of a product of K elementary
020: *  reflectors of order N
021: *
022: *        Q  =  H(k)' . . . H(2)' H(1)'
023: *
024: *  as returned by ZGELQF.
025: *
026: *  Arguments
027: *  =========
028: *
029: *  M       (input) INTEGER
030: *          The number of rows of the matrix Q. M >= 0.
031: *
032: *  N       (input) INTEGER
033: *          The number of columns of the matrix Q. N >= M.
034: *
035: *  K       (input) INTEGER
036: *          The number of elementary reflectors whose product defines the
037: *          matrix Q. M >= K >= 0.
038: *
039: *  A       (input/output) COMPLEX*16 array, dimension (LDA,N)
040: *          On entry, the i-th row must contain the vector which defines
041: *          the elementary reflector H(i), for i = 1,2,...,k, as returned
042: *          by ZGELQF in the first k rows of its array argument A.
043: *          On exit, the M-by-N matrix Q.
044: *
045: *  LDA     (input) INTEGER
046: *          The first dimension of the array A. LDA >= max(1,M).
047: *
048: *  TAU     (input) COMPLEX*16 array, dimension (K)
049: *          TAU(i) must contain the scalar factor of the elementary
050: *          reflector H(i), as returned by ZGELQF.
051: *
052: *  WORK    (workspace/output) COMPLEX*16 array, dimension (MAX(1,LWORK))
053: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
054: *
055: *  LWORK   (input) INTEGER
056: *          The dimension of the array WORK. LWORK >= max(1,M).
057: *          For optimum performance LWORK >= M*NB, where NB is
058: *          the optimal blocksize.
059: *
060: *          If LWORK = -1, then a workspace query is assumed; the routine
061: *          only calculates the optimal size of the WORK array, returns
062: *          this value as the first entry of the WORK array, and no error
063: *          message related to LWORK is issued by XERBLA.
064: *
065: *  INFO    (output) INTEGER
066: *          = 0:  successful exit;
067: *          < 0:  if INFO = -i, the i-th argument has an illegal value
068: *
069: *  =====================================================================
070: *
071: *     .. Parameters ..
072:       COMPLEX*16         ZERO
073:       PARAMETER          ( ZERO = ( 0.0D+0, 0.0D+0 ) )
074: *     ..
075: *     .. Local Scalars ..
076:       LOGICAL            LQUERY
077:       INTEGER            I, IB, IINFO, IWS, J, KI, KK, L, LDWORK,
078:      \$                   LWKOPT, NB, NBMIN, NX
079: *     ..
080: *     .. External Subroutines ..
081:       EXTERNAL           XERBLA, ZLARFB, ZLARFT, ZUNGL2
082: *     ..
083: *     .. Intrinsic Functions ..
084:       INTRINSIC          MAX, MIN
085: *     ..
086: *     .. External Functions ..
087:       INTEGER            ILAENV
088:       EXTERNAL           ILAENV
089: *     ..
090: *     .. Executable Statements ..
091: *
092: *     Test the input arguments
093: *
094:       INFO = 0
095:       NB = ILAENV( 1, 'ZUNGLQ', ' ', M, N, K, -1 )
096:       LWKOPT = MAX( 1, M )*NB
097:       WORK( 1 ) = LWKOPT
098:       LQUERY = ( LWORK.EQ.-1 )
099:       IF( M.LT.0 ) THEN
100:          INFO = -1
101:       ELSE IF( N.LT.M ) THEN
102:          INFO = -2
103:       ELSE IF( K.LT.0 .OR. K.GT.M ) THEN
104:          INFO = -3
105:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
106:          INFO = -5
107:       ELSE IF( LWORK.LT.MAX( 1, M ) .AND. .NOT.LQUERY ) THEN
108:          INFO = -8
109:       END IF
110:       IF( INFO.NE.0 ) THEN
111:          CALL XERBLA( 'ZUNGLQ', -INFO )
112:          RETURN
113:       ELSE IF( LQUERY ) THEN
114:          RETURN
115:       END IF
116: *
117: *     Quick return if possible
118: *
119:       IF( M.LE.0 ) THEN
120:          WORK( 1 ) = 1
121:          RETURN
122:       END IF
123: *
124:       NBMIN = 2
125:       NX = 0
126:       IWS = M
127:       IF( NB.GT.1 .AND. NB.LT.K ) THEN
128: *
129: *        Determine when to cross over from blocked to unblocked code.
130: *
131:          NX = MAX( 0, ILAENV( 3, 'ZUNGLQ', ' ', M, N, K, -1 ) )
132:          IF( NX.LT.K ) THEN
133: *
134: *           Determine if workspace is large enough for blocked code.
135: *
136:             LDWORK = M
137:             IWS = LDWORK*NB
138:             IF( LWORK.LT.IWS ) THEN
139: *
140: *              Not enough workspace to use optimal NB:  reduce NB and
141: *              determine the minimum value of NB.
142: *
143:                NB = LWORK / LDWORK
144:                NBMIN = MAX( 2, ILAENV( 2, 'ZUNGLQ', ' ', M, N, K, -1 ) )
145:             END IF
146:          END IF
147:       END IF
148: *
149:       IF( NB.GE.NBMIN .AND. NB.LT.K .AND. NX.LT.K ) THEN
150: *
151: *        Use blocked code after the last block.
152: *        The first kk rows are handled by the block method.
153: *
154:          KI = ( ( K-NX-1 ) / NB )*NB
155:          KK = MIN( K, KI+NB )
156: *
157: *        Set A(kk+1:m,1:kk) to zero.
158: *
159:          DO 20 J = 1, KK
160:             DO 10 I = KK + 1, M
161:                A( I, J ) = ZERO
162:    10       CONTINUE
163:    20    CONTINUE
164:       ELSE
165:          KK = 0
166:       END IF
167: *
168: *     Use unblocked code for the last or only block.
169: *
170:       IF( KK.LT.M )
171:      \$   CALL ZUNGL2( M-KK, N-KK, K-KK, A( KK+1, KK+1 ), LDA,
172:      \$                TAU( KK+1 ), WORK, IINFO )
173: *
174:       IF( KK.GT.0 ) THEN
175: *
176: *        Use blocked code
177: *
178:          DO 50 I = KI + 1, 1, -NB
179:             IB = MIN( NB, K-I+1 )
180:             IF( I+IB.LE.M ) THEN
181: *
182: *              Form the triangular factor of the block reflector
183: *              H = H(i) H(i+1) . . . H(i+ib-1)
184: *
185:                CALL ZLARFT( 'Forward', 'Rowwise', N-I+1, IB, A( I, I ),
186:      \$                      LDA, TAU( I ), WORK, LDWORK )
187: *
188: *              Apply H' to A(i+ib:m,i:n) from the right
189: *
190:                CALL ZLARFB( 'Right', 'Conjugate transpose', 'Forward',
191:      \$                      'Rowwise', M-I-IB+1, N-I+1, IB, A( I, I ),
192:      \$                      LDA, WORK, LDWORK, A( I+IB, I ), LDA,
193:      \$                      WORK( IB+1 ), LDWORK )
194:             END IF
195: *
196: *           Apply H' to columns i:n of current block
197: *
198:             CALL ZUNGL2( IB, N-I+1, IB, A( I, I ), LDA, TAU( I ), WORK,
199:      \$                   IINFO )
200: *
201: *           Set columns 1:i-1 of current block to zero
202: *
203:             DO 40 J = 1, I - 1
204:                DO 30 L = I, I + IB - 1
205:                   A( L, J ) = ZERO
206:    30          CONTINUE
207:    40       CONTINUE
208:    50    CONTINUE
209:       END IF
210: *
211:       WORK( 1 ) = IWS
212:       RETURN
213: *
214: *     End of ZUNGLQ
215: *
216:       END
217: ```