001:       SUBROUTINE CTZRZF( M, N, 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, LDA, LWORK, M, N
010: *     ..
011: *     .. Array Arguments ..
012:       COMPLEX            A( LDA, * ), TAU( * ), WORK( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  CTZRZF reduces the M-by-N ( M<=N ) complex upper trapezoidal matrix A
019: *  to upper triangular form by means of unitary transformations.
020: *
021: *  The upper trapezoidal matrix A is factored as
022: *
023: *     A = ( R  0 ) * Z,
024: *
025: *  where Z is an N-by-N unitary matrix and R is an M-by-M upper
026: *  triangular matrix.
027: *
028: *  Arguments
029: *  =========
030: *
031: *  M       (input) INTEGER
032: *          The number of rows of the matrix A.  M >= 0.
033: *
034: *  N       (input) INTEGER
035: *          The number of columns of the matrix A.  N >= M.
036: *
037: *  A       (input/output) COMPLEX array, dimension (LDA,N)
038: *          On entry, the leading M-by-N upper trapezoidal part of the
039: *          array A must contain the matrix to be factorized.
040: *          On exit, the leading M-by-M upper triangular part of A
041: *          contains the upper triangular matrix R, and elements M+1 to
042: *          N of the first M rows of A, with the array TAU, represent the
043: *          unitary matrix Z as a product of M elementary reflectors.
044: *
045: *  LDA     (input) INTEGER
046: *          The leading dimension of the array A.  LDA >= max(1,M).
047: *
048: *  TAU     (output) COMPLEX array, dimension (M)
049: *          The scalar factors of the elementary reflectors.
050: *
051: *  WORK    (workspace/output) COMPLEX array, dimension (MAX(1,LWORK))
052: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
053: *
054: *  LWORK   (input) INTEGER
055: *          The dimension of the array WORK.  LWORK >= max(1,M).
056: *          For optimum performance LWORK >= M*NB, where NB is
057: *          the optimal blocksize.
058: *
059: *          If LWORK = -1, then a workspace query is assumed; the routine
060: *          only calculates the optimal size of the WORK array, returns
061: *          this value as the first entry of the WORK array, and no error
062: *          message related to LWORK is issued by XERBLA.
063: *
064: *  INFO    (output) INTEGER
065: *          = 0:  successful exit
066: *          < 0:  if INFO = -i, the i-th argument had an illegal value
067: *
068: *  Further Details
069: *  ===============
070: *
071: *  Based on contributions by
072: *    A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA
073: *
074: *  The factorization is obtained by Householder's method.  The kth
075: *  transformation matrix, Z( k ), which is used to introduce zeros into
076: *  the ( m - k + 1 )th row of A, is given in the form
077: *
078: *     Z( k ) = ( I     0   ),
079: *              ( 0  T( k ) )
080: *
081: *  where
082: *
083: *     T( k ) = I - tau*u( k )*u( k )',   u( k ) = (   1    ),
084: *                                                 (   0    )
085: *                                                 ( z( k ) )
086: *
087: *  tau is a scalar and z( k ) is an ( n - m ) element vector.
088: *  tau and z( k ) are chosen to annihilate the elements of the kth row
089: *  of X.
090: *
091: *  The scalar tau is returned in the kth element of TAU and the vector
092: *  u( k ) in the kth row of A, such that the elements of z( k ) are
093: *  in  a( k, m + 1 ), ..., a( k, n ). The elements of R are returned in
094: *  the upper triangular part of A.
095: *
096: *  Z is given by
097: *
098: *     Z =  Z( 1 ) * Z( 2 ) * ... * Z( m ).
099: *
100: *  =====================================================================
101: *
102: *     .. Parameters ..
103:       COMPLEX            ZERO
104:       PARAMETER          ( ZERO = ( 0.0E+0, 0.0E+0 ) )
105: *     ..
106: *     .. Local Scalars ..
107:       LOGICAL            LQUERY
108:       INTEGER            I, IB, IWS, KI, KK, LDWORK, LWKOPT, M1, MU, NB,
109:      $                   NBMIN, NX
110: *     ..
111: *     .. External Subroutines ..
112:       EXTERNAL           CLARZB, CLARZT, CLATRZ, XERBLA
113: *     ..
114: *     .. Intrinsic Functions ..
115:       INTRINSIC          MAX, MIN
116: *     ..
117: *     .. External Functions ..
118:       INTEGER            ILAENV
119:       EXTERNAL           ILAENV
120: *     ..
121: *     .. Executable Statements ..
122: *
123: *     Test the input arguments
124: *
125:       INFO = 0
126:       LQUERY = ( LWORK.EQ.-1 )
127:       IF( M.LT.0 ) THEN
128:          INFO = -1
129:       ELSE IF( N.LT.M ) THEN
130:          INFO = -2
131:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
132:          INFO = -4
133:       ELSE IF( LWORK.LT.MAX( 1, M ) .AND. .NOT.LQUERY ) THEN
134:          INFO = -7
135:       END IF
136: *
137:       IF( INFO.EQ.0 ) THEN
138:          IF( M.EQ.0 .OR. M.EQ.N ) THEN
139:             LWKOPT = 1
140:          ELSE
141: *
142: *           Determine the block size.
143: *
144:             NB = ILAENV( 1, 'CGERQF', ' ', M, N, -1, -1 )
145:             LWKOPT = M*NB
146:          END IF
147:          WORK( 1 ) = LWKOPT
148: *
149:          IF( LWORK.LT.MAX( 1, M ) .AND. .NOT.LQUERY ) THEN
150:             INFO = -7
151:          END IF
152:       END IF
153: *
154:       IF( INFO.NE.0 ) THEN
155:          CALL XERBLA( 'CTZRZF', -INFO )
156:          RETURN
157:       ELSE IF( LQUERY ) THEN
158:          RETURN
159:       END IF
160: *
161: *     Quick return if possible
162: *
163:       IF( M.EQ.0 ) THEN
164:          RETURN
165:       ELSE IF( M.EQ.N ) THEN
166:          DO 10 I = 1, N
167:             TAU( I ) = ZERO
168:    10    CONTINUE
169:          RETURN
170:       END IF
171: *
172:       NBMIN = 2
173:       NX = 1
174:       IWS = M
175:       IF( NB.GT.1 .AND. NB.LT.M ) THEN
176: *
177: *        Determine when to cross over from blocked to unblocked code.
178: *
179:          NX = MAX( 0, ILAENV( 3, 'CGERQF', ' ', M, N, -1, -1 ) )
180:          IF( NX.LT.M ) THEN
181: *
182: *           Determine if workspace is large enough for blocked code.
183: *
184:             LDWORK = M
185:             IWS = LDWORK*NB
186:             IF( LWORK.LT.IWS ) THEN
187: *
188: *              Not enough workspace to use optimal NB:  reduce NB and
189: *              determine the minimum value of NB.
190: *
191:                NB = LWORK / LDWORK
192:                NBMIN = MAX( 2, ILAENV( 2, 'CGERQF', ' ', M, N, -1,
193:      $                 -1 ) )
194:             END IF
195:          END IF
196:       END IF
197: *
198:       IF( NB.GE.NBMIN .AND. NB.LT.M .AND. NX.LT.M ) THEN
199: *
200: *        Use blocked code initially.
201: *        The last kk rows are handled by the block method.
202: *
203:          M1 = MIN( M+1, N )
204:          KI = ( ( M-NX-1 ) / NB )*NB
205:          KK = MIN( M, KI+NB )
206: *
207:          DO 20 I = M - KK + KI + 1, M - KK + 1, -NB
208:             IB = MIN( M-I+1, NB )
209: *
210: *           Compute the TZ factorization of the current block
211: *           A(i:i+ib-1,i:n)
212: *
213:             CALL CLATRZ( IB, N-I+1, N-M, A( I, I ), LDA, TAU( I ),
214:      $                   WORK )
215:             IF( I.GT.1 ) THEN
216: *
217: *              Form the triangular factor of the block reflector
218: *              H = H(i+ib-1) . . . H(i+1) H(i)
219: *
220:                CALL CLARZT( 'Backward', 'Rowwise', N-M, IB, A( I, M1 ),
221:      $                      LDA, TAU( I ), WORK, LDWORK )
222: *
223: *              Apply H to A(1:i-1,i:n) from the right
224: *
225:                CALL CLARZB( 'Right', 'No transpose', 'Backward',
226:      $                      'Rowwise', I-1, N-I+1, IB, N-M, A( I, M1 ),
227:      $                      LDA, WORK, LDWORK, A( 1, I ), LDA,
228:      $                      WORK( IB+1 ), LDWORK )
229:             END IF
230:    20    CONTINUE
231:          MU = I + NB - 1
232:       ELSE
233:          MU = M
234:       END IF
235: *
236: *     Use unblocked code to factor the last or only block
237: *
238:       IF( MU.GT.0 )
239:      $   CALL CLATRZ( MU, N, N-M, A, LDA, TAU, WORK )
240: *
241:       WORK( 1 ) = LWKOPT
242: *
243:       RETURN
244: *
245: *     End of CTZRZF
246: *
247:       END
248: