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