001:       SUBROUTINE ZLAHR2( N, K, NB, A, LDA, TAU, T, LDT, Y, LDY )
002: *
003: *  -- LAPACK auxiliary routine (version 3.2) --
004: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
005: *     November 2006
006: *
007: *     .. Scalar Arguments ..
008:       INTEGER            K, LDA, LDT, LDY, N, NB
009: *     ..
010: *     .. Array Arguments ..
011:       COMPLEX*16        A( LDA, * ), T( LDT, NB ), TAU( NB ),
012:      $                   Y( LDY, NB )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  ZLAHR2 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 ZGEHRD.
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*16 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*16 array, dimension (NB)
053: *          The scalar factors of the elementary reflectors. See Further
054: *          Details.
055: *
056: *  T       (output) COMPLEX*16 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*16 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 file is a slight modification of LAPACK-3.0's ZLAHRD
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 routine. This function is
107: *  not backward compatible with LAPACK3.0.
108: *
109: *  =====================================================================
110: *
111: *     .. Parameters ..
112:       COMPLEX*16        ZERO, ONE
113:       PARAMETER          ( ZERO = ( 0.0D+0, 0.0D+0 ), 
114:      $                     ONE = ( 1.0D+0, 0.0D+0 ) )
115: *     ..
116: *     .. Local Scalars ..
117:       INTEGER            I
118:       COMPLEX*16        EI
119: *     ..
120: *     .. External Subroutines ..
121:       EXTERNAL           ZAXPY, ZCOPY, ZGEMM, ZGEMV, ZLACPY,
122:      $                   ZLARFG, ZSCAL, ZTRMM, ZTRMV, ZLACGV
123: *     ..
124: *     .. Intrinsic Functions ..
125:       INTRINSIC          MIN
126: *     ..
127: *     .. Executable Statements ..
128: *
129: *     Quick return if possible
130: *
131:       IF( N.LE.1 )
132:      $   RETURN
133: *
134:       DO 10 I = 1, NB
135:          IF( I.GT.1 ) THEN
136: *
137: *           Update A(K+1:N,I)
138: *
139: *           Update I-th column of A - Y * V'
140: *
141:             CALL ZLACGV( I-1, A( K+I-1, 1 ), LDA ) 
142:             CALL ZGEMV( 'NO TRANSPOSE', N-K, I-1, -ONE, Y(K+1,1), LDY,
143:      $                  A( K+I-1, 1 ), LDA, ONE, A( K+1, I ), 1 )
144:             CALL ZLACGV( I-1, A( K+I-1, 1 ), LDA ) 
145: *
146: *           Apply I - V * T' * V' to this column (call it b) from the
147: *           left, using the last column of T as workspace
148: *
149: *           Let  V = ( V1 )   and   b = ( b1 )   (first I-1 rows)
150: *                    ( V2 )             ( b2 )
151: *
152: *           where V1 is unit lower triangular
153: *
154: *           w := V1' * b1
155: *
156:             CALL ZCOPY( I-1, A( K+1, I ), 1, T( 1, NB ), 1 )
157:             CALL ZTRMV( 'Lower', 'Conjugate transpose', 'UNIT', 
158:      $                  I-1, A( K+1, 1 ),
159:      $                  LDA, T( 1, NB ), 1 )
160: *
161: *           w := w + V2'*b2
162: *
163:             CALL ZGEMV( 'Conjugate transpose', N-K-I+1, I-1, 
164:      $                  ONE, A( K+I, 1 ),
165:      $                  LDA, A( K+I, I ), 1, ONE, T( 1, NB ), 1 )
166: *
167: *           w := T'*w
168: *
169:             CALL ZTRMV( 'Upper', 'Conjugate transpose', 'NON-UNIT', 
170:      $                  I-1, T, LDT,
171:      $                  T( 1, NB ), 1 )
172: *
173: *           b2 := b2 - V2*w
174: *
175:             CALL ZGEMV( 'NO TRANSPOSE', N-K-I+1, I-1, -ONE, 
176:      $                  A( K+I, 1 ),
177:      $                  LDA, T( 1, NB ), 1, ONE, A( K+I, I ), 1 )
178: *
179: *           b1 := b1 - V1*w
180: *
181:             CALL ZTRMV( 'Lower', 'NO TRANSPOSE', 
182:      $                  'UNIT', I-1,
183:      $                  A( K+1, 1 ), LDA, T( 1, NB ), 1 )
184:             CALL ZAXPY( I-1, -ONE, T( 1, NB ), 1, A( K+1, I ), 1 )
185: *
186:             A( K+I-1, I-1 ) = EI
187:          END IF
188: *
189: *        Generate the elementary reflector H(I) to annihilate
190: *        A(K+I+1:N,I)
191: *
192:          CALL ZLARFG( N-K-I+1, A( K+I, I ), A( MIN( K+I+1, N ), I ), 1,
193:      $                TAU( I ) )
194:          EI = A( K+I, I )
195:          A( K+I, I ) = ONE
196: *
197: *        Compute  Y(K+1:N,I)
198: *
199:          CALL ZGEMV( 'NO TRANSPOSE', N-K, N-K-I+1, 
200:      $               ONE, A( K+1, I+1 ),
201:      $               LDA, A( K+I, I ), 1, ZERO, Y( K+1, I ), 1 )
202:          CALL ZGEMV( 'Conjugate transpose', N-K-I+1, I-1, 
203:      $               ONE, A( K+I, 1 ), LDA,
204:      $               A( K+I, I ), 1, ZERO, T( 1, I ), 1 )
205:          CALL ZGEMV( 'NO TRANSPOSE', N-K, I-1, -ONE, 
206:      $               Y( K+1, 1 ), LDY,
207:      $               T( 1, I ), 1, ONE, Y( K+1, I ), 1 )
208:          CALL ZSCAL( N-K, TAU( I ), Y( K+1, I ), 1 )
209: *
210: *        Compute T(1:I,I)
211: *
212:          CALL ZSCAL( I-1, -TAU( I ), T( 1, I ), 1 )
213:          CALL ZTRMV( 'Upper', 'No Transpose', 'NON-UNIT', 
214:      $               I-1, T, LDT,
215:      $               T( 1, I ), 1 )
216:          T( I, I ) = TAU( I )
217: *
218:    10 CONTINUE
219:       A( K+NB, NB ) = EI
220: *
221: *     Compute Y(1:K,1:NB)
222: *
223:       CALL ZLACPY( 'ALL', K, NB, A( 1, 2 ), LDA, Y, LDY )
224:       CALL ZTRMM( 'RIGHT', 'Lower', 'NO TRANSPOSE', 
225:      $            'UNIT', K, NB,
226:      $            ONE, A( K+1, 1 ), LDA, Y, LDY )
227:       IF( N.GT.K+NB )
228:      $   CALL ZGEMM( 'NO TRANSPOSE', 'NO TRANSPOSE', K, 
229:      $               NB, N-K-NB, ONE,
230:      $               A( 1, 2+NB ), LDA, A( K+1+NB, 1 ), LDA, ONE, Y,
231:      $               LDY )
232:       CALL ZTRMM( 'RIGHT', 'Upper', 'NO TRANSPOSE', 
233:      $            'NON-UNIT', K, NB,
234:      $            ONE, T, LDT, Y, LDY )
235: *
236:       RETURN
237: *
238: *     End of ZLAHR2
239: *
240:       END
241: