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