001:       SUBROUTINE CLAHR2( N, K, NB, A, LDA, TAU, T, LDT, Y, LDY )
002: *
003: *  -- LAPACK auxiliary routine (version 3.2.1)                        --
004: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --*  -- April 2009                                                      --
005: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
006: *
007: *     .. Scalar Arguments ..
008:       INTEGER            K, LDA, LDT, LDY, N, NB
009: *     ..
010: *     .. Array Arguments ..
011:       COMPLEX            A( LDA, * ), T( LDT, NB ), TAU( NB ),
012:      $                   Y( LDY, NB )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  CLAHR2 reduces the first NB columns of A complex general n-BY-(n-k+1)
019: *  matrix A so that elements below the k-th subdiagonal are zero. The
020: *  reduction is performed by an unitary similarity transformation
021: *  Q' * A * Q. The routine returns the matrices V and T which determine
022: *  Q as a block reflector I - V*T*V', and also the matrix Y = A * V * T.
023: *
024: *  This is an auxiliary routine called by CGEHRD.
025: *
026: *  Arguments
027: *  =========
028: *
029: *  N       (input) INTEGER
030: *          The order of the matrix A.
031: *
032: *  K       (input) INTEGER
033: *          The offset for the reduction. Elements below the k-th
034: *          subdiagonal in the first NB columns are reduced to zero.
035: *          K < N.
036: *
037: *  NB      (input) INTEGER
038: *          The number of columns to be reduced.
039: *
040: *  A       (input/output) COMPLEX array, dimension (LDA,N-K+1)
041: *          On entry, the n-by-(n-k+1) general matrix A.
042: *          On exit, the elements on and above the k-th subdiagonal in
043: *          the first NB columns are overwritten with the corresponding
044: *          elements of the reduced matrix; the elements below the k-th
045: *          subdiagonal, with the array TAU, represent the matrix Q as a
046: *          product of elementary reflectors. The other columns of A are
047: *          unchanged. See Further Details.
048: *
049: *  LDA     (input) INTEGER
050: *          The leading dimension of the array A.  LDA >= max(1,N).
051: *
052: *  TAU     (output) COMPLEX array, dimension (NB)
053: *          The scalar factors of the elementary reflectors. See Further
054: *          Details.
055: *
056: *  T       (output) COMPLEX array, dimension (LDT,NB)
057: *          The upper triangular matrix T.
058: *
059: *  LDT     (input) INTEGER
060: *          The leading dimension of the array T.  LDT >= NB.
061: *
062: *  Y       (output) COMPLEX array, dimension (LDY,NB)
063: *          The n-by-nb matrix Y.
064: *
065: *  LDY     (input) INTEGER
066: *          The leading dimension of the array Y. LDY >= N.
067: *
068: *  Further Details
069: *  ===============
070: *
071: *  The matrix Q is represented as a product of nb elementary reflectors
072: *
073: *     Q = H(1) H(2) . . . H(nb).
074: *
075: *  Each H(i) has the form
076: *
077: *     H(i) = I - tau * v * v'
078: *
079: *  where tau is a complex scalar, and v is a complex vector with
080: *  v(1:i+k-1) = 0, v(i+k) = 1; v(i+k+1:n) is stored on exit in
081: *  A(i+k+1:n,i), and tau in TAU(i).
082: *
083: *  The elements of the vectors v together form the (n-k+1)-by-nb matrix
084: *  V which is needed, with T and Y, to apply the transformation to the
085: *  unreduced part of the matrix, using an update of the form:
086: *  A := (I - V*T*V') * (A - Y*V').
087: *
088: *  The contents of A on exit are illustrated by the following example
089: *  with n = 7, k = 3 and nb = 2:
090: *
091: *     ( a   a   a   a   a )
092: *     ( a   a   a   a   a )
093: *     ( a   a   a   a   a )
094: *     ( h   h   a   a   a )
095: *     ( v1  h   a   a   a )
096: *     ( v1  v2  a   a   a )
097: *     ( v1  v2  a   a   a )
098: *
099: *  where a denotes an element of the original matrix A, h denotes a
100: *  modified element of the upper Hessenberg matrix H, and vi denotes an
101: *  element of the vector defining H(i).
102: *
103: *  This subroutine is a slight modification of LAPACK-3.0's DLAHRD
104: *  incorporating improvements proposed by Quintana-Orti and Van de
105: *  Gejin. Note that the entries of A(1:K,2:NB) differ from those
106: *  returned by the original LAPACK-3.0's DLAHRD routine. (This
107: *  subroutine is not backward compatible with LAPACK-3.0's DLAHRD.)
108: *
109: *  References
110: *  ==========
111: *
112: *  Gregorio Quintana-Orti and Robert van de Geijn, "Improving the
113: *  performance of reduction to Hessenberg form," ACM Transactions on
114: *  Mathematical Software, 32(2):180-194, June 2006.
115: *
116: *  =====================================================================
117: *
118: *     .. Parameters ..
119:       COMPLEX            ZERO, ONE
120:       PARAMETER          ( ZERO = ( 0.0E+0, 0.0E+0 ), 
121:      $                     ONE = ( 1.0E+0, 0.0E+0 ) )
122: *     ..
123: *     .. Local Scalars ..
124:       INTEGER            I
125:       COMPLEX            EI
126: *     ..
127: *     .. External Subroutines ..
128:       EXTERNAL           CAXPY, CCOPY, CGEMM, CGEMV, CLACPY,
129:      $                   CLARFG, CSCAL, CTRMM, CTRMV, CLACGV
130: *     ..
131: *     .. Intrinsic Functions ..
132:       INTRINSIC          MIN
133: *     ..
134: *     .. Executable Statements ..
135: *
136: *     Quick return if possible
137: *
138:       IF( N.LE.1 )
139:      $   RETURN
140: *
141:       DO 10 I = 1, NB
142:          IF( I.GT.1 ) THEN
143: *
144: *           Update A(K+1:N,I)
145: *
146: *           Update I-th column of A - Y * V'
147: *
148:             CALL CLACGV( I-1, A( K+I-1, 1 ), LDA ) 
149:             CALL CGEMV( 'NO TRANSPOSE', N-K, I-1, -ONE, Y(K+1,1), LDY,
150:      $                  A( K+I-1, 1 ), LDA, ONE, A( K+1, I ), 1 )
151:             CALL CLACGV( I-1, A( K+I-1, 1 ), LDA ) 
152: *
153: *           Apply I - V * T' * V' to this column (call it b) from the
154: *           left, using the last column of T as workspace
155: *
156: *           Let  V = ( V1 )   and   b = ( b1 )   (first I-1 rows)
157: *                    ( V2 )             ( b2 )
158: *
159: *           where V1 is unit lower triangular
160: *
161: *           w := V1' * b1
162: *
163:             CALL CCOPY( I-1, A( K+1, I ), 1, T( 1, NB ), 1 )
164:             CALL CTRMV( 'Lower', 'Conjugate transpose', 'UNIT', 
165:      $                  I-1, A( K+1, 1 ),
166:      $                  LDA, T( 1, NB ), 1 )
167: *
168: *           w := w + V2'*b2
169: *
170:             CALL CGEMV( 'Conjugate transpose', N-K-I+1, I-1, 
171:      $                  ONE, A( K+I, 1 ),
172:      $                  LDA, A( K+I, I ), 1, ONE, T( 1, NB ), 1 )
173: *
174: *           w := T'*w
175: *
176:             CALL CTRMV( 'Upper', 'Conjugate transpose', 'NON-UNIT', 
177:      $                  I-1, T, LDT,
178:      $                  T( 1, NB ), 1 )
179: *
180: *           b2 := b2 - V2*w
181: *
182:             CALL CGEMV( 'NO TRANSPOSE', N-K-I+1, I-1, -ONE, 
183:      $                  A( K+I, 1 ),
184:      $                  LDA, T( 1, NB ), 1, ONE, A( K+I, I ), 1 )
185: *
186: *           b1 := b1 - V1*w
187: *
188:             CALL CTRMV( 'Lower', 'NO TRANSPOSE', 
189:      $                  'UNIT', I-1,
190:      $                  A( K+1, 1 ), LDA, T( 1, NB ), 1 )
191:             CALL CAXPY( I-1, -ONE, T( 1, NB ), 1, A( K+1, I ), 1 )
192: *
193:             A( K+I-1, I-1 ) = EI
194:          END IF
195: *
196: *        Generate the elementary reflector H(I) to annihilate
197: *        A(K+I+1:N,I)
198: *
199:          CALL CLARFG( N-K-I+1, A( K+I, I ), A( MIN( K+I+1, N ), I ), 1,
200:      $                TAU( I ) )
201:          EI = A( K+I, I )
202:          A( K+I, I ) = ONE
203: *
204: *        Compute  Y(K+1:N,I)
205: *
206:          CALL CGEMV( 'NO TRANSPOSE', N-K, N-K-I+1, 
207:      $               ONE, A( K+1, I+1 ),
208:      $               LDA, A( K+I, I ), 1, ZERO, Y( K+1, I ), 1 )
209:          CALL CGEMV( 'Conjugate transpose', N-K-I+1, I-1, 
210:      $               ONE, A( K+I, 1 ), LDA,
211:      $               A( K+I, I ), 1, ZERO, T( 1, I ), 1 )
212:          CALL CGEMV( 'NO TRANSPOSE', N-K, I-1, -ONE, 
213:      $               Y( K+1, 1 ), LDY,
214:      $               T( 1, I ), 1, ONE, Y( K+1, I ), 1 )
215:          CALL CSCAL( N-K, TAU( I ), Y( K+1, I ), 1 )
216: *
217: *        Compute T(1:I,I)
218: *
219:          CALL CSCAL( I-1, -TAU( I ), T( 1, I ), 1 )
220:          CALL CTRMV( 'Upper', 'No Transpose', 'NON-UNIT', 
221:      $               I-1, T, LDT,
222:      $               T( 1, I ), 1 )
223:          T( I, I ) = TAU( I )
224: *
225:    10 CONTINUE
226:       A( K+NB, NB ) = EI
227: *
228: *     Compute Y(1:K,1:NB)
229: *
230:       CALL CLACPY( 'ALL', K, NB, A( 1, 2 ), LDA, Y, LDY )
231:       CALL CTRMM( 'RIGHT', 'Lower', 'NO TRANSPOSE', 
232:      $            'UNIT', K, NB,
233:      $            ONE, A( K+1, 1 ), LDA, Y, LDY )
234:       IF( N.GT.K+NB )
235:      $   CALL CGEMM( 'NO TRANSPOSE', 'NO TRANSPOSE', K, 
236:      $               NB, N-K-NB, ONE,
237:      $               A( 1, 2+NB ), LDA, A( K+1+NB, 1 ), LDA, ONE, Y,
238:      $               LDY )
239:       CALL CTRMM( 'RIGHT', 'Upper', 'NO TRANSPOSE', 
240:      $            'NON-UNIT', K, NB,
241:      $            ONE, T, LDT, Y, LDY )
242: *
243:       RETURN
244: *
245: *     End of CLAHR2
246: *
247:       END
248: