001:       SUBROUTINE SLAQPS( 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               A( LDA, * ), AUXV( * ), F( LDF, * ), TAU( * ),
014:      $                   VN1( * ), VN2( * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  SLAQPS computes a step of QR factorization with column pivoting
021: *  of a real 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) REAL 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) REAL 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) REAL array, dimension (NB)
075: *          Auxiliar vector.
076: *
077: *  F       (input/output) REAL 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:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
100: *     ..
101: *     .. Local Scalars ..
102:       INTEGER            ITEMP, J, K, LASTRK, LSTICC, PVT, RK
103:       REAL               AKK, TEMP, TEMP2, TOL3Z
104: *     ..
105: *     .. External Subroutines ..
106:       EXTERNAL           SGEMM, SGEMV, SLARFP, SSWAP
107: *     ..
108: *     .. Intrinsic Functions ..
109:       INTRINSIC          ABS, MAX, MIN, NINT, REAL, SQRT
110: *     ..
111: *     .. External Functions ..
112:       INTEGER            ISAMAX
113:       REAL               SLAMCH, SNRM2
114:       EXTERNAL           ISAMAX, SLAMCH, SNRM2
115: *     ..
116: *     .. Executable Statements ..
117: *
118:       LASTRK = MIN( M, N+OFFSET )
119:       LSTICC = 0
120:       K = 0
121:       TOL3Z = SQRT(SLAMCH('Epsilon'))
122: *
123: *     Beginning of while loop.
124: *
125:    10 CONTINUE
126:       IF( ( K.LT.NB ) .AND. ( LSTICC.EQ.0 ) ) THEN
127:          K = K + 1
128:          RK = OFFSET + K
129: *
130: *        Determine ith pivot column and swap if necessary
131: *
132:          PVT = ( K-1 ) + ISAMAX( N-K+1, VN1( K ), 1 )
133:          IF( PVT.NE.K ) THEN
134:             CALL SSWAP( M, A( 1, PVT ), 1, A( 1, K ), 1 )
135:             CALL SSWAP( K-1, F( PVT, 1 ), LDF, F( K, 1 ), LDF )
136:             ITEMP = JPVT( PVT )
137:             JPVT( PVT ) = JPVT( K )
138:             JPVT( K ) = ITEMP
139:             VN1( PVT ) = VN1( K )
140:             VN2( PVT ) = VN2( K )
141:          END IF
142: *
143: *        Apply previous Householder reflectors to column K:
144: *        A(RK:M,K) := A(RK:M,K) - A(RK:M,1:K-1)*F(K,1:K-1)'.
145: *
146:          IF( K.GT.1 ) THEN
147:             CALL SGEMV( 'No transpose', M-RK+1, K-1, -ONE, A( RK, 1 ),
148:      $                  LDA, F( K, 1 ), LDF, ONE, A( RK, K ), 1 )
149:          END IF
150: *
151: *        Generate elementary reflector H(k).
152: *
153:          IF( RK.LT.M ) THEN
154:             CALL SLARFP( M-RK+1, A( RK, K ), A( RK+1, K ), 1, TAU( K ) )
155:          ELSE
156:             CALL SLARFP( 1, A( RK, K ), A( RK, K ), 1, TAU( K ) )
157:          END IF
158: *
159:          AKK = A( RK, K )
160:          A( RK, K ) = ONE
161: *
162: *        Compute Kth column of F:
163: *
164: *        Compute  F(K+1:N,K) := tau(K)*A(RK:M,K+1:N)'*A(RK:M,K).
165: *
166:          IF( K.LT.N ) THEN
167:             CALL SGEMV( 'Transpose', M-RK+1, N-K, TAU( K ),
168:      $                  A( RK, K+1 ), LDA, A( RK, K ), 1, ZERO,
169:      $                  F( K+1, K ), 1 )
170:          END IF
171: *
172: *        Padding F(1:K,K) with zeros.
173: *
174:          DO 20 J = 1, K
175:             F( J, K ) = ZERO
176:    20    CONTINUE
177: *
178: *        Incremental updating of F:
179: *        F(1:N,K) := F(1:N,K) - tau(K)*F(1:N,1:K-1)*A(RK:M,1:K-1)'
180: *                    *A(RK:M,K).
181: *
182:          IF( K.GT.1 ) THEN
183:             CALL SGEMV( 'Transpose', M-RK+1, K-1, -TAU( K ), A( RK, 1 ),
184:      $                  LDA, A( RK, K ), 1, ZERO, AUXV( 1 ), 1 )
185: *
186:             CALL SGEMV( 'No transpose', N, K-1, ONE, F( 1, 1 ), LDF,
187:      $                  AUXV( 1 ), 1, ONE, F( 1, K ), 1 )
188:          END IF
189: *
190: *        Update the current row of A:
191: *        A(RK,K+1:N) := A(RK,K+1:N) - A(RK,1:K)*F(K+1:N,1:K)'.
192: *
193:          IF( K.LT.N ) THEN
194:             CALL SGEMV( 'No transpose', N-K, K, -ONE, F( K+1, 1 ), LDF,
195:      $                  A( RK, 1 ), LDA, ONE, A( RK, K+1 ), LDA )
196:          END IF
197: *
198: *        Update partial column norms.
199: *
200:          IF( RK.LT.LASTRK ) THEN
201:             DO 30 J = K + 1, N
202:                IF( VN1( J ).NE.ZERO ) THEN
203: *
204: *                 NOTE: The following 4 lines follow from the analysis in
205: *                 Lapack Working Note 176.
206: *
207:                   TEMP = ABS( A( RK, J ) ) / VN1( J )
208:                   TEMP = MAX( ZERO, ( ONE+TEMP )*( ONE-TEMP ) )
209:                   TEMP2 = TEMP*( VN1( J ) / VN2( J ) )**2
210:                   IF( TEMP2 .LE. TOL3Z ) THEN
211:                      VN2( J ) = REAL( LSTICC )
212:                      LSTICC = J
213:                   ELSE
214:                      VN1( J ) = VN1( J )*SQRT( TEMP )
215:                   END IF
216:                END IF
217:    30       CONTINUE
218:          END IF
219: *
220:          A( RK, K ) = AKK
221: *
222: *        End of while loop.
223: *
224:          GO TO 10
225:       END IF
226:       KB = K
227:       RK = OFFSET + KB
228: *
229: *     Apply the block reflector to the rest of the matrix:
230: *     A(OFFSET+KB+1:M,KB+1:N) := A(OFFSET+KB+1:M,KB+1:N) -
231: *                         A(OFFSET+KB+1:M,1:KB)*F(KB+1:N,1:KB)'.
232: *
233:       IF( KB.LT.MIN( N, M-OFFSET ) ) THEN
234:          CALL SGEMM( 'No transpose', 'Transpose', M-RK, N-KB, KB, -ONE,
235:      $               A( RK+1, 1 ), LDA, F( KB+1, 1 ), LDF, ONE,
236:      $               A( RK+1, KB+1 ), LDA )
237:       END IF
238: *
239: *     Recomputation of difficult columns.
240: *
241:    40 CONTINUE
242:       IF( LSTICC.GT.0 ) THEN
243:          ITEMP = NINT( VN2( LSTICC ) )
244:          VN1( LSTICC ) = SNRM2( M-RK, A( RK+1, LSTICC ), 1 )
245: *
246: *        NOTE: The computation of VN1( LSTICC ) relies on the fact that 
247: *        SNRM2 does not fail on vectors with norm below the value of
248: *        SQRT(DLAMCH('S')) 
249: *
250:          VN2( LSTICC ) = VN1( LSTICC )
251:          LSTICC = ITEMP
252:          GO TO 40
253:       END IF
254: *
255:       RETURN
256: *
257: *     End of SLAQPS
258: *
259:       END
260: