001:       SUBROUTINE SLAHR2( 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:       REAL              A( LDA, * ), T( LDT, NB ), TAU( NB ),
012:      $                   Y( LDY, NB )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  SLAHR2 reduces the first NB columns of A real 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 orthogonal 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 SGEHRD.
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) REAL 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) REAL array, dimension (NB)
053: *          The scalar factors of the elementary reflectors. See Further
054: *          Details.
055: *
056: *  T       (output) REAL 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) REAL 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 real scalar, and v is a real 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 SLAHRD
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:       REAL              ZERO, ONE
113:       PARAMETER          ( ZERO = 0.0E+0, 
114:      $                     ONE = 1.0E+0 )
115: *     ..
116: *     .. Local Scalars ..
117:       INTEGER            I
118:       REAL              EI
119: *     ..
120: *     .. External Subroutines ..
121:       EXTERNAL           SAXPY, SCOPY, SGEMM, SGEMV, SLACPY,
122:      $                   SLARFG, SSCAL, STRMM, STRMV
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 SGEMV( 'NO TRANSPOSE', N-K, I-1, -ONE, Y(K+1,1), LDY,
142:      $                  A( K+I-1, 1 ), LDA, ONE, A( K+1, I ), 1 )
143: *
144: *           Apply I - V * T' * V' to this column (call it b) from the
145: *           left, using the last column of T as workspace
146: *
147: *           Let  V = ( V1 )   and   b = ( b1 )   (first I-1 rows)
148: *                    ( V2 )             ( b2 )
149: *
150: *           where V1 is unit lower triangular
151: *
152: *           w := V1' * b1
153: *
154:             CALL SCOPY( I-1, A( K+1, I ), 1, T( 1, NB ), 1 )
155:             CALL STRMV( 'Lower', 'Transpose', 'UNIT', 
156:      $                  I-1, A( K+1, 1 ),
157:      $                  LDA, T( 1, NB ), 1 )
158: *
159: *           w := w + V2'*b2
160: *
161:             CALL SGEMV( 'Transpose', N-K-I+1, I-1, 
162:      $                  ONE, A( K+I, 1 ),
163:      $                  LDA, A( K+I, I ), 1, ONE, T( 1, NB ), 1 )
164: *
165: *           w := T'*w
166: *
167:             CALL STRMV( 'Upper', 'Transpose', 'NON-UNIT', 
168:      $                  I-1, T, LDT,
169:      $                  T( 1, NB ), 1 )
170: *
171: *           b2 := b2 - V2*w
172: *
173:             CALL SGEMV( 'NO TRANSPOSE', N-K-I+1, I-1, -ONE, 
174:      $                  A( K+I, 1 ),
175:      $                  LDA, T( 1, NB ), 1, ONE, A( K+I, I ), 1 )
176: *
177: *           b1 := b1 - V1*w
178: *
179:             CALL STRMV( 'Lower', 'NO TRANSPOSE', 
180:      $                  'UNIT', I-1,
181:      $                  A( K+1, 1 ), LDA, T( 1, NB ), 1 )
182:             CALL SAXPY( I-1, -ONE, T( 1, NB ), 1, A( K+1, I ), 1 )
183: *
184:             A( K+I-1, I-1 ) = EI
185:          END IF
186: *
187: *        Generate the elementary reflector H(I) to annihilate
188: *        A(K+I+1:N,I)
189: *
190:          CALL SLARFG( N-K-I+1, A( K+I, I ), A( MIN( K+I+1, N ), I ), 1,
191:      $                TAU( I ) )
192:          EI = A( K+I, I )
193:          A( K+I, I ) = ONE
194: *
195: *        Compute  Y(K+1:N,I)
196: *
197:          CALL SGEMV( 'NO TRANSPOSE', N-K, N-K-I+1, 
198:      $               ONE, A( K+1, I+1 ),
199:      $               LDA, A( K+I, I ), 1, ZERO, Y( K+1, I ), 1 )
200:          CALL SGEMV( 'Transpose', N-K-I+1, I-1, 
201:      $               ONE, A( K+I, 1 ), LDA,
202:      $               A( K+I, I ), 1, ZERO, T( 1, I ), 1 )
203:          CALL SGEMV( 'NO TRANSPOSE', N-K, I-1, -ONE, 
204:      $               Y( K+1, 1 ), LDY,
205:      $               T( 1, I ), 1, ONE, Y( K+1, I ), 1 )
206:          CALL SSCAL( N-K, TAU( I ), Y( K+1, I ), 1 )
207: *
208: *        Compute T(1:I,I)
209: *
210:          CALL SSCAL( I-1, -TAU( I ), T( 1, I ), 1 )
211:          CALL STRMV( 'Upper', 'No Transpose', 'NON-UNIT', 
212:      $               I-1, T, LDT,
213:      $               T( 1, I ), 1 )
214:          T( I, I ) = TAU( I )
215: *
216:    10 CONTINUE
217:       A( K+NB, NB ) = EI
218: *
219: *     Compute Y(1:K,1:NB)
220: *
221:       CALL SLACPY( 'ALL', K, NB, A( 1, 2 ), LDA, Y, LDY )
222:       CALL STRMM( 'RIGHT', 'Lower', 'NO TRANSPOSE', 
223:      $            'UNIT', K, NB,
224:      $            ONE, A( K+1, 1 ), LDA, Y, LDY )
225:       IF( N.GT.K+NB )
226:      $   CALL SGEMM( 'NO TRANSPOSE', 'NO TRANSPOSE', K, 
227:      $               NB, N-K-NB, ONE,
228:      $               A( 1, 2+NB ), LDA, A( K+1+NB, 1 ), LDA, ONE, Y,
229:      $               LDY )
230:       CALL STRMM( 'RIGHT', 'Upper', 'NO TRANSPOSE', 
231:      $            'NON-UNIT', K, NB,
232:      $            ONE, T, LDT, Y, LDY )
233: *
234:       RETURN
235: *
236: *     End of SLAHR2
237: *
238:       END
239: