001:       SUBROUTINE CLAQPS( M, N, OFFSET, NB, KB, A, LDA, JPVT, TAU, VN1,
002:      $                   VN2, AUXV, F, LDF )
003: *
004: *  -- LAPACK auxiliary routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       INTEGER            KB, LDA, LDF, M, N, NB, OFFSET
010: *     ..
011: *     .. Array Arguments ..
012:       INTEGER            JPVT( * )
013:       REAL               VN1( * ), VN2( * )
014:       COMPLEX            A( LDA, * ), AUXV( * ), F( LDF, * ), TAU( * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  CLAQPS computes a step of QR factorization with column pivoting
021: *  of a complex M-by-N matrix A by using Blas-3.  It tries to factorize
022: *  NB columns from A starting from the row OFFSET+1, and updates all
023: *  of the matrix with Blas-3 xGEMM.
024: *
025: *  In some cases, due to catastrophic cancellations, it cannot
026: *  factorize NB columns.  Hence, the actual number of factorized
027: *  columns is returned in KB.
028: *
029: *  Block A(1:OFFSET,1:N) is accordingly pivoted, but not factorized.
030: *
031: *  Arguments
032: *  =========
033: *
034: *  M       (input) INTEGER
035: *          The number of rows of the matrix A. M >= 0.
036: *
037: *  N       (input) INTEGER
038: *          The number of columns of the matrix A. N >= 0
039: *
040: *  OFFSET  (input) INTEGER
041: *          The number of rows of A that have been factorized in
042: *          previous steps.
043: *
044: *  NB      (input) INTEGER
045: *          The number of columns to factorize.
046: *
047: *  KB      (output) INTEGER
048: *          The number of columns actually factorized.
049: *
050: *  A       (input/output) COMPLEX array, dimension (LDA,N)
051: *          On entry, the M-by-N matrix A.
052: *          On exit, block A(OFFSET+1:M,1:KB) is the triangular
053: *          factor obtained and block A(1:OFFSET,1:N) has been
054: *          accordingly pivoted, but no factorized.
055: *          The rest of the matrix, block A(OFFSET+1:M,KB+1:N) has
056: *          been updated.
057: *
058: *  LDA     (input) INTEGER
059: *          The leading dimension of the array A. LDA >= max(1,M).
060: *
061: *  JPVT    (input/output) INTEGER array, dimension (N)
062: *          JPVT(I) = K <==> Column K of the full matrix A has been
063: *          permuted into position I in AP.
064: *
065: *  TAU     (output) COMPLEX array, dimension (KB)
066: *          The scalar factors of the elementary reflectors.
067: *
068: *  VN1     (input/output) REAL array, dimension (N)
069: *          The vector with the partial column norms.
070: *
071: *  VN2     (input/output) REAL array, dimension (N)
072: *          The vector with the exact column norms.
073: *
074: *  AUXV    (input/output) COMPLEX array, dimension (NB)
075: *          Auxiliar vector.
076: *
077: *  F       (input/output) COMPLEX array, dimension (LDF,NB)
078: *          Matrix F' = L*Y'*A.
079: *
080: *  LDF     (input) INTEGER
081: *          The leading dimension of the array F. LDF >= max(1,N).
082: *
083: *  Further Details
084: *  ===============
085: *
086: *  Based on contributions by
087: *    G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain
088: *    X. Sun, Computer Science Dept., Duke University, USA
089: *
090: *  Partial column norm updating strategy modified by
091: *    Z. Drmac and Z. Bujanovic, Dept. of Mathematics,
092: *    University of Zagreb, Croatia.
093: *    June 2006.
094: *  For more details see LAPACK Working Note 176.
095: *  =====================================================================
096: *
097: *     .. Parameters ..
098:       REAL               ZERO, ONE
099:       COMPLEX            CZERO, CONE
100:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0,
101:      $                   CZERO = ( 0.0E+0, 0.0E+0 ),
102:      $                   CONE = ( 1.0E+0, 0.0E+0 ) )
103: *     ..
104: *     .. Local Scalars ..
105:       INTEGER            ITEMP, J, K, LASTRK, LSTICC, PVT, RK
106:       REAL               TEMP, TEMP2, TOL3Z
107:       COMPLEX            AKK
108: *     ..
109: *     .. External Subroutines ..
110:       EXTERNAL           CGEMM, CGEMV, CLARFG, CSWAP
111: *     ..
112: *     .. Intrinsic Functions ..
113:       INTRINSIC          ABS, CONJG, MAX, MIN, NINT, REAL, SQRT
114: *     ..
115: *     .. External Functions ..
116:       INTEGER            ISAMAX
117:       REAL               SCNRM2, SLAMCH
118:       EXTERNAL           ISAMAX, SCNRM2, SLAMCH
119: *     ..
120: *     .. Executable Statements ..
121: *
122:       LASTRK = MIN( M, N+OFFSET )
123:       LSTICC = 0
124:       K = 0
125:       TOL3Z = SQRT(SLAMCH('Epsilon'))
126: *
127: *     Beginning of while loop.
128: *
129:    10 CONTINUE
130:       IF( ( K.LT.NB ) .AND. ( LSTICC.EQ.0 ) ) THEN
131:          K = K + 1
132:          RK = OFFSET + K
133: *
134: *        Determine ith pivot column and swap if necessary
135: *
136:          PVT = ( K-1 ) + ISAMAX( N-K+1, VN1( K ), 1 )
137:          IF( PVT.NE.K ) THEN
138:             CALL CSWAP( M, A( 1, PVT ), 1, A( 1, K ), 1 )
139:             CALL CSWAP( K-1, F( PVT, 1 ), LDF, F( K, 1 ), LDF )
140:             ITEMP = JPVT( PVT )
141:             JPVT( PVT ) = JPVT( K )
142:             JPVT( K ) = ITEMP
143:             VN1( PVT ) = VN1( K )
144:             VN2( PVT ) = VN2( K )
145:          END IF
146: *
147: *        Apply previous Householder reflectors to column K:
148: *        A(RK:M,K) := A(RK:M,K) - A(RK:M,1:K-1)*F(K,1:K-1)'.
149: *
150:          IF( K.GT.1 ) THEN
151:             DO 20 J = 1, K - 1
152:                F( K, J ) = CONJG( F( K, J ) )
153:    20       CONTINUE
154:             CALL CGEMV( 'No transpose', M-RK+1, K-1, -CONE, A( RK, 1 ),
155:      $                  LDA, F( K, 1 ), LDF, CONE, A( RK, K ), 1 )
156:             DO 30 J = 1, K - 1
157:                F( K, J ) = CONJG( F( K, J ) )
158:    30       CONTINUE
159:          END IF
160: *
161: *        Generate elementary reflector H(k).
162: *
163:          IF( RK.LT.M ) THEN
164:             CALL CLARFG( M-RK+1, A( RK, K ), A( RK+1, K ), 1, TAU( K ) )
165:          ELSE
166:             CALL CLARFG( 1, A( RK, K ), A( RK, K ), 1, TAU( K ) )
167:          END IF
168: *
169:          AKK = A( RK, K )
170:          A( RK, K ) = CONE
171: *
172: *        Compute Kth column of F:
173: *
174: *        Compute  F(K+1:N,K) := tau(K)*A(RK:M,K+1:N)'*A(RK:M,K).
175: *
176:          IF( K.LT.N ) THEN
177:             CALL CGEMV( 'Conjugate transpose', M-RK+1, N-K, TAU( K ),
178:      $                  A( RK, K+1 ), LDA, A( RK, K ), 1, CZERO,
179:      $                  F( K+1, K ), 1 )
180:          END IF
181: *
182: *        Padding F(1:K,K) with zeros.
183: *
184:          DO 40 J = 1, K
185:             F( J, K ) = CZERO
186:    40    CONTINUE
187: *
188: *        Incremental updating of F:
189: *        F(1:N,K) := F(1:N,K) - tau(K)*F(1:N,1:K-1)*A(RK:M,1:K-1)'
190: *                    *A(RK:M,K).
191: *
192:          IF( K.GT.1 ) THEN
193:             CALL CGEMV( 'Conjugate transpose', M-RK+1, K-1, -TAU( K ),
194:      $                  A( RK, 1 ), LDA, A( RK, K ), 1, CZERO,
195:      $                  AUXV( 1 ), 1 )
196: *
197:             CALL CGEMV( 'No transpose', N, K-1, CONE, F( 1, 1 ), LDF,
198:      $                  AUXV( 1 ), 1, CONE, F( 1, K ), 1 )
199:          END IF
200: *
201: *        Update the current row of A:
202: *        A(RK,K+1:N) := A(RK,K+1:N) - A(RK,1:K)*F(K+1:N,1:K)'.
203: *
204:          IF( K.LT.N ) THEN
205:             CALL CGEMM( 'No transpose', 'Conjugate transpose', 1, N-K,
206:      $                  K, -CONE, A( RK, 1 ), LDA, F( K+1, 1 ), LDF,
207:      $                  CONE, A( RK, K+1 ), LDA )
208:          END IF
209: *
210: *        Update partial column norms.
211: *
212:          IF( RK.LT.LASTRK ) THEN
213:             DO 50 J = K + 1, N
214:                IF( VN1( J ).NE.ZERO ) THEN
215: *
216: *                 NOTE: The following 4 lines follow from the analysis in
217: *                 Lapack Working Note 176.
218: *
219:                   TEMP = ABS( A( RK, J ) ) / VN1( J )
220:                   TEMP = MAX( ZERO, ( ONE+TEMP )*( ONE-TEMP ) )
221:                   TEMP2 = TEMP*( VN1( J ) / VN2( J ) )**2
222:                   IF( TEMP2 .LE. TOL3Z ) THEN
223:                      VN2( J ) = REAL( LSTICC )
224:                      LSTICC = J
225:                   ELSE
226:                      VN1( J ) = VN1( J )*SQRT( TEMP )
227:                   END IF
228:                END IF
229:    50       CONTINUE
230:          END IF
231: *
232:          A( RK, K ) = AKK
233: *
234: *        End of while loop.
235: *
236:          GO TO 10
237:       END IF
238:       KB = K
239:       RK = OFFSET + KB
240: *
241: *     Apply the block reflector to the rest of the matrix:
242: *     A(OFFSET+KB+1:M,KB+1:N) := A(OFFSET+KB+1:M,KB+1:N) -
243: *                         A(OFFSET+KB+1:M,1:KB)*F(KB+1:N,1:KB)'.
244: *
245:       IF( KB.LT.MIN( N, M-OFFSET ) ) THEN
246:          CALL CGEMM( 'No transpose', 'Conjugate transpose', M-RK, N-KB,
247:      $               KB, -CONE, A( RK+1, 1 ), LDA, F( KB+1, 1 ), LDF,
248:      $               CONE, A( RK+1, KB+1 ), LDA )
249:       END IF
250: *
251: *     Recomputation of difficult columns.
252: *
253:    60 CONTINUE
254:       IF( LSTICC.GT.0 ) THEN
255:          ITEMP = NINT( VN2( LSTICC ) )
256:          VN1( LSTICC ) = SCNRM2( M-RK, A( RK+1, LSTICC ), 1 )
257: *
258: *        NOTE: The computation of VN1( LSTICC ) relies on the fact that 
259: *        SNRM2 does not fail on vectors with norm below the value of
260: *        SQRT(DLAMCH('S')) 
261: *
262:          VN2( LSTICC ) = VN1( LSTICC )
263:          LSTICC = ITEMP
264:          GO TO 60
265:       END IF
266: *
267:       RETURN
268: *
269: *     End of CLAQPS
270: *
271:       END
272: