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