001:       SUBROUTINE DGEQP3( M, N, A, LDA, JPVT, TAU, WORK, LWORK, INFO )
002: *
003: *  -- LAPACK routine (version 3.2) --
004: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
005: *     November 2006
006: *
007: *     .. Scalar Arguments ..
008:       INTEGER            INFO, LDA, LWORK, M, N
009: *     ..
010: *     .. Array Arguments ..
011:       INTEGER            JPVT( * )
012:       DOUBLE PRECISION   A( LDA, * ), TAU( * ), WORK( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  DGEQP3 computes a QR factorization with column pivoting of a
019: *  matrix A:  A*P = Q*R  using Level 3 BLAS.
020: *
021: *  Arguments
022: *  =========
023: *
024: *  M       (input) INTEGER
025: *          The number of rows of the matrix A. M >= 0.
026: *
027: *  N       (input) INTEGER
028: *          The number of columns of the matrix A.  N >= 0.
029: *
030: *  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)
031: *          On entry, the M-by-N matrix A.
032: *          On exit, the upper triangle of the array contains the
033: *          min(M,N)-by-N upper trapezoidal matrix R; the elements below
034: *          the diagonal, together with the array TAU, represent the
035: *          orthogonal matrix Q as a product of min(M,N) elementary
036: *          reflectors.
037: *
038: *  LDA     (input) INTEGER
039: *          The leading dimension of the array A. LDA >= max(1,M).
040: *
041: *  JPVT    (input/output) INTEGER array, dimension (N)
042: *          On entry, if JPVT(J).ne.0, the J-th column of A is permuted
043: *          to the front of A*P (a leading column); if JPVT(J)=0,
044: *          the J-th column of A is a free column.
045: *          On exit, if JPVT(J)=K, then the J-th column of A*P was the
046: *          the K-th column of A.
047: *
048: *  TAU     (output) DOUBLE PRECISION array, dimension (min(M,N))
049: *          The scalar factors of the elementary reflectors.
050: *
051: *  WORK    (workspace/output) DOUBLE PRECISION 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 >= 3*N+1.
056: *          For optimal performance LWORK >= 2*N+( N+1 )*NB, where NB
057: *          is 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: *  The matrix Q is represented as a product of elementary reflectors
072: *
073: *     Q = H(1) H(2) . . . H(k), where k = min(m,n).
074: *
075: *  Each H(i) has the form
076: *
077: *     H(i) = I - tau * v * v'
078: *
079: *  where tau is a real/complex scalar, and v is a real/complex vector
080: *  with v(1:i-1) = 0 and v(i) = 1; v(i+1:m) is stored on exit in
081: *  A(i+1:m,i), and tau in TAU(i).
082: *
083: *  Based on contributions by
084: *    G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain
085: *    X. Sun, Computer Science Dept., Duke University, USA
086: *
087: *  =====================================================================
088: *
089: *     .. Parameters ..
090:       INTEGER            INB, INBMIN, IXOVER
091:       PARAMETER          ( INB = 1, INBMIN = 2, IXOVER = 3 )
092: *     ..
093: *     .. Local Scalars ..
094:       LOGICAL            LQUERY
095:       INTEGER            FJB, IWS, J, JB, LWKOPT, MINMN, MINWS, NA, NB,
096:      $                   NBMIN, NFXD, NX, SM, SMINMN, SN, TOPBMN
097: *     ..
098: *     .. External Subroutines ..
099:       EXTERNAL           DGEQRF, DLAQP2, DLAQPS, DORMQR, DSWAP, XERBLA
100: *     ..
101: *     .. External Functions ..
102:       INTEGER            ILAENV
103:       DOUBLE PRECISION   DNRM2
104:       EXTERNAL           ILAENV, DNRM2
105: *     ..
106: *     .. Intrinsic Functions ..
107:       INTRINSIC          INT, MAX, MIN
108: *     ..
109: *     .. Executable Statements ..
110: *
111: *     Test input arguments
112: *     ====================
113: *
114:       INFO = 0
115:       LQUERY = ( LWORK.EQ.-1 )
116:       IF( M.LT.0 ) THEN
117:          INFO = -1
118:       ELSE IF( N.LT.0 ) THEN
119:          INFO = -2
120:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
121:          INFO = -4
122:       END IF
123: *
124:       IF( INFO.EQ.0 ) THEN
125:          MINMN = MIN( M, N )
126:          IF( MINMN.EQ.0 ) THEN
127:             IWS = 1
128:             LWKOPT = 1
129:          ELSE
130:             IWS = 3*N + 1
131:             NB = ILAENV( INB, 'DGEQRF', ' ', M, N, -1, -1 )
132:             LWKOPT = 2*N + ( N + 1 )*NB
133:          END IF
134:          WORK( 1 ) = LWKOPT
135: *
136:          IF( ( LWORK.LT.IWS ) .AND. .NOT.LQUERY ) THEN
137:             INFO = -8
138:          END IF
139:       END IF
140: *
141:       IF( INFO.NE.0 ) THEN
142:          CALL XERBLA( 'DGEQP3', -INFO )
143:          RETURN
144:       ELSE IF( LQUERY ) THEN
145:          RETURN
146:       END IF
147: *
148: *     Quick return if possible.
149: *
150:       IF( MINMN.EQ.0 ) THEN
151:          RETURN
152:       END IF
153: *
154: *     Move initial columns up front.
155: *
156:       NFXD = 1
157:       DO 10 J = 1, N
158:          IF( JPVT( J ).NE.0 ) THEN
159:             IF( J.NE.NFXD ) THEN
160:                CALL DSWAP( M, A( 1, J ), 1, A( 1, NFXD ), 1 )
161:                JPVT( J ) = JPVT( NFXD )
162:                JPVT( NFXD ) = J
163:             ELSE
164:                JPVT( J ) = J
165:             END IF
166:             NFXD = NFXD + 1
167:          ELSE
168:             JPVT( J ) = J
169:          END IF
170:    10 CONTINUE
171:       NFXD = NFXD - 1
172: *
173: *     Factorize fixed columns
174: *     =======================
175: *
176: *     Compute the QR factorization of fixed columns and update
177: *     remaining columns.
178: *
179:       IF( NFXD.GT.0 ) THEN
180:          NA = MIN( M, NFXD )
181: *CC      CALL DGEQR2( M, NA, A, LDA, TAU, WORK, INFO )
182:          CALL DGEQRF( M, NA, A, LDA, TAU, WORK, LWORK, INFO )
183:          IWS = MAX( IWS, INT( WORK( 1 ) ) )
184:          IF( NA.LT.N ) THEN
185: *CC         CALL DORM2R( 'Left', 'Transpose', M, N-NA, NA, A, LDA,
186: *CC  $                   TAU, A( 1, NA+1 ), LDA, WORK, INFO )
187:             CALL DORMQR( 'Left', 'Transpose', M, N-NA, NA, A, LDA, TAU,
188:      $                   A( 1, NA+1 ), LDA, WORK, LWORK, INFO )
189:             IWS = MAX( IWS, INT( WORK( 1 ) ) )
190:          END IF
191:       END IF
192: *
193: *     Factorize free columns
194: *     ======================
195: *
196:       IF( NFXD.LT.MINMN ) THEN
197: *
198:          SM = M - NFXD
199:          SN = N - NFXD
200:          SMINMN = MINMN - NFXD
201: *
202: *        Determine the block size.
203: *
204:          NB = ILAENV( INB, 'DGEQRF', ' ', SM, SN, -1, -1 )
205:          NBMIN = 2
206:          NX = 0
207: *
208:          IF( ( NB.GT.1 ) .AND. ( NB.LT.SMINMN ) ) THEN
209: *
210: *           Determine when to cross over from blocked to unblocked code.
211: *
212:             NX = MAX( 0, ILAENV( IXOVER, 'DGEQRF', ' ', SM, SN, -1,
213:      $           -1 ) )
214: *
215: *
216:             IF( NX.LT.SMINMN ) THEN
217: *
218: *              Determine if workspace is large enough for blocked code.
219: *
220:                MINWS = 2*SN + ( SN+1 )*NB
221:                IWS = MAX( IWS, MINWS )
222:                IF( LWORK.LT.MINWS ) THEN
223: *
224: *                 Not enough workspace to use optimal NB: Reduce NB and
225: *                 determine the minimum value of NB.
226: *
227:                   NB = ( LWORK-2*SN ) / ( SN+1 )
228:                   NBMIN = MAX( 2, ILAENV( INBMIN, 'DGEQRF', ' ', SM, SN,
229:      $                    -1, -1 ) )
230: *
231: *
232:                END IF
233:             END IF
234:          END IF
235: *
236: *        Initialize partial column norms. The first N elements of work
237: *        store the exact column norms.
238: *
239:          DO 20 J = NFXD + 1, N
240:             WORK( J ) = DNRM2( SM, A( NFXD+1, J ), 1 )
241:             WORK( N+J ) = WORK( J )
242:    20    CONTINUE
243: *
244:          IF( ( NB.GE.NBMIN ) .AND. ( NB.LT.SMINMN ) .AND.
245:      $       ( NX.LT.SMINMN ) ) THEN
246: *
247: *           Use blocked code initially.
248: *
249:             J = NFXD + 1
250: *
251: *           Compute factorization: while loop.
252: *
253: *
254:             TOPBMN = MINMN - NX
255:    30       CONTINUE
256:             IF( J.LE.TOPBMN ) THEN
257:                JB = MIN( NB, TOPBMN-J+1 )
258: *
259: *              Factorize JB columns among columns J:N.
260: *
261:                CALL DLAQPS( M, N-J+1, J-1, JB, FJB, A( 1, J ), LDA,
262:      $                      JPVT( J ), TAU( J ), WORK( J ), WORK( N+J ),
263:      $                      WORK( 2*N+1 ), WORK( 2*N+JB+1 ), N-J+1 )
264: *
265:                J = J + FJB
266:                GO TO 30
267:             END IF
268:          ELSE
269:             J = NFXD + 1
270:          END IF
271: *
272: *        Use unblocked code to factor the last or only block.
273: *
274: *
275:          IF( J.LE.MINMN )
276:      $      CALL DLAQP2( M, N-J+1, J-1, A( 1, J ), LDA, JPVT( J ),
277:      $                   TAU( J ), WORK( J ), WORK( N+J ),
278:      $                   WORK( 2*N+1 ) )
279: *
280:       END IF
281: *
282:       WORK( 1 ) = IWS
283:       RETURN
284: *
285: *     End of DGEQP3
286: *
287:       END
288: