001:       SUBROUTINE STZRZF( 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:       REAL               A( LDA, * ), TAU( * ), WORK( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  STZRZF reduces the M-by-N ( M<=N ) real upper trapezoidal matrix A
019: *  to upper triangular form by means of orthogonal 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 orthogonal 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) REAL 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: *          orthogonal 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) REAL array, dimension (M)
049: *          The scalar factors of the elementary reflectors.
050: *
051: *  WORK    (workspace/output) REAL 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:       REAL               ZERO
104:       PARAMETER          ( ZERO = 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           SLARZB, SLARZT, SLATRZ, 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:       END IF
134: *
135:       IF( INFO.EQ.0 ) THEN
136:          IF( M.EQ.0 .OR. M.EQ.N ) THEN
137:             LWKOPT = 1
138:          ELSE
139: *
140: *           Determine the block size.
141: *
142:             NB = ILAENV( 1, 'SGERQF', ' ', M, N, -1, -1 )
143:             LWKOPT = M*NB
144:          END IF
145:          WORK( 1 ) = LWKOPT
146: *
147:          IF( LWORK.LT.MAX( 1, M ) .AND. .NOT.LQUERY ) THEN
148:             INFO = -7
149:          END IF
150:       END IF
151: *
152:       IF( INFO.NE.0 ) THEN
153:          CALL XERBLA( 'STZRZF', -INFO )
154:          RETURN
155:       ELSE IF( LQUERY ) THEN
156:          RETURN
157:       END IF
158: *
159: *     Quick return if possible
160: *
161:       IF( M.EQ.0 ) THEN
162:          RETURN
163:       ELSE IF( M.EQ.N ) THEN
164:          DO 10 I = 1, N
165:             TAU( I ) = ZERO
166:    10    CONTINUE
167:          RETURN
168:       END IF
169: *
170:       NBMIN = 2
171:       NX = 1
172:       IWS = M
173:       IF( NB.GT.1 .AND. NB.LT.M ) THEN
174: *
175: *        Determine when to cross over from blocked to unblocked code.
176: *
177:          NX = MAX( 0, ILAENV( 3, 'SGERQF', ' ', M, N, -1, -1 ) )
178:          IF( NX.LT.M ) THEN
179: *
180: *           Determine if workspace is large enough for blocked code.
181: *
182:             LDWORK = M
183:             IWS = LDWORK*NB
184:             IF( LWORK.LT.IWS ) THEN
185: *
186: *              Not enough workspace to use optimal NB:  reduce NB and
187: *              determine the minimum value of NB.
188: *
189:                NB = LWORK / LDWORK
190:                NBMIN = MAX( 2, ILAENV( 2, 'SGERQF', ' ', M, N, -1,
191:      $                 -1 ) )
192:             END IF
193:          END IF
194:       END IF
195: *
196:       IF( NB.GE.NBMIN .AND. NB.LT.M .AND. NX.LT.M ) THEN
197: *
198: *        Use blocked code initially.
199: *        The last kk rows are handled by the block method.
200: *
201:          M1 = MIN( M+1, N )
202:          KI = ( ( M-NX-1 ) / NB )*NB
203:          KK = MIN( M, KI+NB )
204: *
205:          DO 20 I = M - KK + KI + 1, M - KK + 1, -NB
206:             IB = MIN( M-I+1, NB )
207: *
208: *           Compute the TZ factorization of the current block
209: *           A(i:i+ib-1,i:n)
210: *
211:             CALL SLATRZ( IB, N-I+1, N-M, A( I, I ), LDA, TAU( I ),
212:      $                   WORK )
213:             IF( I.GT.1 ) THEN
214: *
215: *              Form the triangular factor of the block reflector
216: *              H = H(i+ib-1) . . . H(i+1) H(i)
217: *
218:                CALL SLARZT( 'Backward', 'Rowwise', N-M, IB, A( I, M1 ),
219:      $                      LDA, TAU( I ), WORK, LDWORK )
220: *
221: *              Apply H to A(1:i-1,i:n) from the right
222: *
223:                CALL SLARZB( 'Right', 'No transpose', 'Backward',
224:      $                      'Rowwise', I-1, N-I+1, IB, N-M, A( I, M1 ),
225:      $                      LDA, WORK, LDWORK, A( 1, I ), LDA,
226:      $                      WORK( IB+1 ), LDWORK )
227:             END IF
228:    20    CONTINUE
229:          MU = I + NB - 1
230:       ELSE
231:          MU = M
232:       END IF
233: *
234: *     Use unblocked code to factor the last or only block
235: *
236:       IF( MU.GT.0 )
237:      $   CALL SLATRZ( MU, N, N-M, A, LDA, TAU, WORK )
238: *
239:       WORK( 1 ) = LWKOPT
240: *
241:       RETURN
242: *
243: *     End of STZRZF
244: *
245:       END
246: