001:       SUBROUTINE DLAQP2( M, N, OFFSET, A, LDA, JPVT, TAU, VN1, VN2,
002:      $                   WORK )
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            LDA, M, N, OFFSET
011: *     ..
012: *     .. Array Arguments ..
013:       INTEGER            JPVT( * )
014:       DOUBLE PRECISION   A( LDA, * ), TAU( * ), VN1( * ), VN2( * ),
015:      $                   WORK( * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  DLAQP2 computes a QR factorization with column pivoting of
022: *  the block A(OFFSET+1:M,1:N).
023: *  The block A(1:OFFSET,1:N) is accordingly pivoted, but not factorized.
024: *
025: *  Arguments
026: *  =========
027: *
028: *  M       (input) INTEGER
029: *          The number of rows of the matrix A. M >= 0.
030: *
031: *  N       (input) INTEGER
032: *          The number of columns of the matrix A. N >= 0.
033: *
034: *  OFFSET  (input) INTEGER
035: *          The number of rows of the matrix A that must be pivoted
036: *          but no factorized. OFFSET >= 0.
037: *
038: *  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)
039: *          On entry, the M-by-N matrix A.
040: *          On exit, the upper triangle of block A(OFFSET+1:M,1:N) is 
041: *          the triangular factor obtained; the elements in block
042: *          A(OFFSET+1:M,1:N) below the diagonal, together with the
043: *          array TAU, represent the orthogonal matrix Q as a product of
044: *          elementary reflectors. Block A(1:OFFSET,1:N) has been
045: *          accordingly pivoted, but no factorized.
046: *
047: *  LDA     (input) INTEGER
048: *          The leading dimension of the array A. LDA >= max(1,M).
049: *
050: *  JPVT    (input/output) INTEGER array, dimension (N)
051: *          On entry, if JPVT(i) .ne. 0, the i-th column of A is permuted
052: *          to the front of A*P (a leading column); if JPVT(i) = 0,
053: *          the i-th column of A is a free column.
054: *          On exit, if JPVT(i) = k, then the i-th column of A*P
055: *          was the k-th column of A.
056: *
057: *  TAU     (output) DOUBLE PRECISION array, dimension (min(M,N))
058: *          The scalar factors of the elementary reflectors.
059: *
060: *  VN1     (input/output) DOUBLE PRECISION array, dimension (N)
061: *          The vector with the partial column norms.
062: *
063: *  VN2     (input/output) DOUBLE PRECISION array, dimension (N)
064: *          The vector with the exact column norms.
065: *
066: *  WORK    (workspace) DOUBLE PRECISION array, dimension (N)
067: *
068: *  Further Details
069: *  ===============
070: *
071: *  Based on contributions by
072: *    G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain
073: *    X. Sun, Computer Science Dept., Duke University, USA
074: *
075: *  Partial column norm updating strategy modified by
076: *    Z. Drmac and Z. Bujanovic, Dept. of Mathematics,
077: *    University of Zagreb, Croatia.
078: *    June 2006.
079: *  For more details see LAPACK Working Note 176.
080: *  =====================================================================
081: *
082: *     .. Parameters ..
083:       DOUBLE PRECISION   ZERO, ONE
084:       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+0 )
085: *     ..
086: *     .. Local Scalars ..
087:       INTEGER            I, ITEMP, J, MN, OFFPI, PVT
088:       DOUBLE PRECISION   AII, TEMP, TEMP2, TOL3Z
089: *     ..
090: *     .. External Subroutines ..
091:       EXTERNAL           DLARF, DLARFP, DSWAP
092: *     ..
093: *     .. Intrinsic Functions ..
094:       INTRINSIC          ABS, MAX, MIN, SQRT
095: *     ..
096: *     .. External Functions ..
097:       INTEGER            IDAMAX
098:       DOUBLE PRECISION   DLAMCH, DNRM2
099:       EXTERNAL           IDAMAX, DLAMCH, DNRM2
100: *     ..
101: *     .. Executable Statements ..
102: *
103:       MN = MIN( M-OFFSET, N )
104:       TOL3Z = SQRT(DLAMCH('Epsilon'))
105: *
106: *     Compute factorization.
107: *
108:       DO 20 I = 1, MN
109: *
110:          OFFPI = OFFSET + I
111: *
112: *        Determine ith pivot column and swap if necessary.
113: *
114:          PVT = ( I-1 ) + IDAMAX( N-I+1, VN1( I ), 1 )
115: *
116:          IF( PVT.NE.I ) THEN
117:             CALL DSWAP( M, A( 1, PVT ), 1, A( 1, I ), 1 )
118:             ITEMP = JPVT( PVT )
119:             JPVT( PVT ) = JPVT( I )
120:             JPVT( I ) = ITEMP
121:             VN1( PVT ) = VN1( I )
122:             VN2( PVT ) = VN2( I )
123:          END IF
124: *
125: *        Generate elementary reflector H(i).
126: *
127:          IF( OFFPI.LT.M ) THEN
128:             CALL DLARFP( M-OFFPI+1, A( OFFPI, I ), A( OFFPI+1, I ), 1,
129:      $                   TAU( I ) )
130:          ELSE
131:             CALL DLARFP( 1, A( M, I ), A( M, I ), 1, TAU( I ) )
132:          END IF
133: *
134:          IF( I.LE.N ) THEN
135: *
136: *           Apply H(i)' to A(offset+i:m,i+1:n) from the left.
137: *
138:             AII = A( OFFPI, I )
139:             A( OFFPI, I ) = ONE
140:             CALL DLARF( 'Left', M-OFFPI+1, N-I, A( OFFPI, I ), 1,
141:      $                  TAU( I ), A( OFFPI, I+1 ), LDA, WORK( 1 ) )
142:             A( OFFPI, I ) = AII
143:          END IF
144: *
145: *        Update partial column norms.
146: *
147:          DO 10 J = I + 1, N
148:             IF( VN1( J ).NE.ZERO ) THEN
149: *
150: *              NOTE: The following 4 lines follow from the analysis in
151: *              Lapack Working Note 176.
152: *
153:                TEMP = ONE - ( ABS( A( OFFPI, J ) ) / VN1( J ) )**2
154:                TEMP = MAX( TEMP, ZERO )
155:                TEMP2 = TEMP*( VN1( J ) / VN2( J ) )**2
156:                IF( TEMP2 .LE. TOL3Z ) THEN
157:                   IF( OFFPI.LT.M ) THEN
158:                      VN1( J ) = DNRM2( M-OFFPI, A( OFFPI+1, J ), 1 )
159:                      VN2( J ) = VN1( J )
160:                   ELSE
161:                      VN1( J ) = ZERO
162:                      VN2( J ) = ZERO
163:                   END IF
164:                ELSE
165:                   VN1( J ) = VN1( J )*SQRT( TEMP )
166:                END IF
167:             END IF
168:    10    CONTINUE
169: *
170:    20 CONTINUE
171: *
172:       RETURN
173: *
174: *     End of DLAQP2
175: *
176:       END
177: