001:       SUBROUTINE SGEBRD( M, N, A, LDA, D, E, TAUQ, TAUP, WORK, LWORK,
002:      $                   INFO )
003: *
004: *  -- LAPACK 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            INFO, LDA, LWORK, M, N
011: *     ..
012: *     .. Array Arguments ..
013:       REAL               A( LDA, * ), D( * ), E( * ), TAUP( * ),
014:      $                   TAUQ( * ), WORK( * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  SGEBRD reduces a general real M-by-N matrix A to upper or lower
021: *  bidiagonal form B by an orthogonal transformation: Q**T * A * P = B.
022: *
023: *  If m >= n, B is upper bidiagonal; if m < n, B is lower bidiagonal.
024: *
025: *  Arguments
026: *  =========
027: *
028: *  M       (input) INTEGER
029: *          The number of rows in the matrix A.  M >= 0.
030: *
031: *  N       (input) INTEGER
032: *          The number of columns in the matrix A.  N >= 0.
033: *
034: *  A       (input/output) REAL array, dimension (LDA,N)
035: *          On entry, the M-by-N general matrix to be reduced.
036: *          On exit,
037: *          if m >= n, the diagonal and the first superdiagonal are
038: *            overwritten with the upper bidiagonal matrix B; the
039: *            elements below the diagonal, with the array TAUQ, represent
040: *            the orthogonal matrix Q as a product of elementary
041: *            reflectors, and the elements above the first superdiagonal,
042: *            with the array TAUP, represent the orthogonal matrix P as
043: *            a product of elementary reflectors;
044: *          if m < n, the diagonal and the first subdiagonal are
045: *            overwritten with the lower bidiagonal matrix B; the
046: *            elements below the first subdiagonal, with the array TAUQ,
047: *            represent the orthogonal matrix Q as a product of
048: *            elementary reflectors, and the elements above the diagonal,
049: *            with the array TAUP, represent the orthogonal matrix P as
050: *            a product of elementary reflectors.
051: *          See Further Details.
052: *
053: *  LDA     (input) INTEGER
054: *          The leading dimension of the array A.  LDA >= max(1,M).
055: *
056: *  D       (output) REAL array, dimension (min(M,N))
057: *          The diagonal elements of the bidiagonal matrix B:
058: *          D(i) = A(i,i).
059: *
060: *  E       (output) REAL array, dimension (min(M,N)-1)
061: *          The off-diagonal elements of the bidiagonal matrix B:
062: *          if m >= n, E(i) = A(i,i+1) for i = 1,2,...,n-1;
063: *          if m < n, E(i) = A(i+1,i) for i = 1,2,...,m-1.
064: *
065: *  TAUQ    (output) REAL array dimension (min(M,N))
066: *          The scalar factors of the elementary reflectors which
067: *          represent the orthogonal matrix Q. See Further Details.
068: *
069: *  TAUP    (output) REAL array, dimension (min(M,N))
070: *          The scalar factors of the elementary reflectors which
071: *          represent the orthogonal matrix P. See Further Details.
072: *
073: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
074: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
075: *
076: *  LWORK   (input) INTEGER
077: *          The length of the array WORK.  LWORK >= max(1,M,N).
078: *          For optimum performance LWORK >= (M+N)*NB, where NB
079: *          is the optimal blocksize.
080: *
081: *          If LWORK = -1, then a workspace query is assumed; the routine
082: *          only calculates the optimal size of the WORK array, returns
083: *          this value as the first entry of the WORK array, and no error
084: *          message related to LWORK is issued by XERBLA.
085: *
086: *  INFO    (output) INTEGER
087: *          = 0:  successful exit 
088: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
089: *
090: *  Further Details
091: *  ===============
092: *
093: *  The matrices Q and P are represented as products of elementary
094: *  reflectors:
095: *
096: *  If m >= n,
097: *
098: *     Q = H(1) H(2) . . . H(n)  and  P = G(1) G(2) . . . G(n-1)
099: *
100: *  Each H(i) and G(i) has the form:
101: *
102: *     H(i) = I - tauq * v * v'  and G(i) = I - taup * u * u'
103: *
104: *  where tauq and taup are real scalars, and v and u are real vectors;
105: *  v(1:i-1) = 0, v(i) = 1, and v(i+1:m) is stored on exit in A(i+1:m,i);
106: *  u(1:i) = 0, u(i+1) = 1, and u(i+2:n) is stored on exit in A(i,i+2:n);
107: *  tauq is stored in TAUQ(i) and taup in TAUP(i).
108: *
109: *  If m < n,
110: *
111: *     Q = H(1) H(2) . . . H(m-1)  and  P = G(1) G(2) . . . G(m)
112: *
113: *  Each H(i) and G(i) has the form:
114: *
115: *     H(i) = I - tauq * v * v'  and G(i) = I - taup * u * u'
116: *
117: *  where tauq and taup are real scalars, and v and u are real vectors;
118: *  v(1:i) = 0, v(i+1) = 1, and v(i+2:m) is stored on exit in A(i+2:m,i);
119: *  u(1:i-1) = 0, u(i) = 1, and u(i+1:n) is stored on exit in A(i,i+1:n);
120: *  tauq is stored in TAUQ(i) and taup in TAUP(i).
121: *
122: *  The contents of A on exit are illustrated by the following examples:
123: *
124: *  m = 6 and n = 5 (m > n):          m = 5 and n = 6 (m < n):
125: *
126: *    (  d   e   u1  u1  u1 )           (  d   u1  u1  u1  u1  u1 )
127: *    (  v1  d   e   u2  u2 )           (  e   d   u2  u2  u2  u2 )
128: *    (  v1  v2  d   e   u3 )           (  v1  e   d   u3  u3  u3 )
129: *    (  v1  v2  v3  d   e  )           (  v1  v2  e   d   u4  u4 )
130: *    (  v1  v2  v3  v4  d  )           (  v1  v2  v3  e   d   u5 )
131: *    (  v1  v2  v3  v4  v5 )
132: *
133: *  where d and e denote diagonal and off-diagonal elements of B, vi
134: *  denotes an element of the vector defining H(i), and ui an element of
135: *  the vector defining G(i).
136: *
137: *  =====================================================================
138: *
139: *     .. Parameters ..
140:       REAL               ONE
141:       PARAMETER          ( ONE = 1.0E+0 )
142: *     ..
143: *     .. Local Scalars ..
144:       LOGICAL            LQUERY
145:       INTEGER            I, IINFO, J, LDWRKX, LDWRKY, LWKOPT, MINMN, NB,
146:      $                   NBMIN, NX
147:       REAL               WS
148: *     ..
149: *     .. External Subroutines ..
150:       EXTERNAL           SGEBD2, SGEMM, SLABRD, XERBLA
151: *     ..
152: *     .. Intrinsic Functions ..
153:       INTRINSIC          MAX, MIN, REAL
154: *     ..
155: *     .. External Functions ..
156:       INTEGER            ILAENV
157:       EXTERNAL           ILAENV
158: *     ..
159: *     .. Executable Statements ..
160: *
161: *     Test the input parameters
162: *
163:       INFO = 0
164:       NB = MAX( 1, ILAENV( 1, 'SGEBRD', ' ', M, N, -1, -1 ) )
165:       LWKOPT = ( M+N )*NB
166:       WORK( 1 ) = REAL( LWKOPT )
167:       LQUERY = ( LWORK.EQ.-1 )
168:       IF( M.LT.0 ) THEN
169:          INFO = -1
170:       ELSE IF( N.LT.0 ) THEN
171:          INFO = -2
172:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
173:          INFO = -4
174:       ELSE IF( LWORK.LT.MAX( 1, M, N ) .AND. .NOT.LQUERY ) THEN
175:          INFO = -10
176:       END IF
177:       IF( INFO.LT.0 ) THEN
178:          CALL XERBLA( 'SGEBRD', -INFO )
179:          RETURN
180:       ELSE IF( LQUERY ) THEN
181:          RETURN
182:       END IF
183: *
184: *     Quick return if possible
185: *
186:       MINMN = MIN( M, N )
187:       IF( MINMN.EQ.0 ) THEN
188:          WORK( 1 ) = 1
189:          RETURN
190:       END IF
191: *
192:       WS = MAX( M, N )
193:       LDWRKX = M
194:       LDWRKY = N
195: *
196:       IF( NB.GT.1 .AND. NB.LT.MINMN ) THEN
197: *
198: *        Set the crossover point NX.
199: *
200:          NX = MAX( NB, ILAENV( 3, 'SGEBRD', ' ', M, N, -1, -1 ) )
201: *
202: *        Determine when to switch from blocked to unblocked code.
203: *
204:          IF( NX.LT.MINMN ) THEN
205:             WS = ( M+N )*NB
206:             IF( LWORK.LT.WS ) THEN
207: *
208: *              Not enough work space for the optimal NB, consider using
209: *              a smaller block size.
210: *
211:                NBMIN = ILAENV( 2, 'SGEBRD', ' ', M, N, -1, -1 )
212:                IF( LWORK.GE.( M+N )*NBMIN ) THEN
213:                   NB = LWORK / ( M+N )
214:                ELSE
215:                   NB = 1
216:                   NX = MINMN
217:                END IF
218:             END IF
219:          END IF
220:       ELSE
221:          NX = MINMN
222:       END IF
223: *
224:       DO 30 I = 1, MINMN - NX, NB
225: *
226: *        Reduce rows and columns i:i+nb-1 to bidiagonal form and return
227: *        the matrices X and Y which are needed to update the unreduced
228: *        part of the matrix
229: *
230:          CALL SLABRD( M-I+1, N-I+1, NB, A( I, I ), LDA, D( I ), E( I ),
231:      $                TAUQ( I ), TAUP( I ), WORK, LDWRKX,
232:      $                WORK( LDWRKX*NB+1 ), LDWRKY )
233: *
234: *        Update the trailing submatrix A(i+nb:m,i+nb:n), using an update
235: *        of the form  A := A - V*Y' - X*U'
236: *
237:          CALL SGEMM( 'No transpose', 'Transpose', M-I-NB+1, N-I-NB+1,
238:      $               NB, -ONE, A( I+NB, I ), LDA,
239:      $               WORK( LDWRKX*NB+NB+1 ), LDWRKY, ONE,
240:      $               A( I+NB, I+NB ), LDA )
241:          CALL SGEMM( 'No transpose', 'No transpose', M-I-NB+1, N-I-NB+1,
242:      $               NB, -ONE, WORK( NB+1 ), LDWRKX, A( I, I+NB ), LDA,
243:      $               ONE, A( I+NB, I+NB ), LDA )
244: *
245: *        Copy diagonal and off-diagonal elements of B back into A
246: *
247:          IF( M.GE.N ) THEN
248:             DO 10 J = I, I + NB - 1
249:                A( J, J ) = D( J )
250:                A( J, J+1 ) = E( J )
251:    10       CONTINUE
252:          ELSE
253:             DO 20 J = I, I + NB - 1
254:                A( J, J ) = D( J )
255:                A( J+1, J ) = E( J )
256:    20       CONTINUE
257:          END IF
258:    30 CONTINUE
259: *
260: *     Use unblocked code to reduce the remainder of the matrix
261: *
262:       CALL SGEBD2( M-I+1, N-I+1, A( I, I ), LDA, D( I ), E( I ),
263:      $             TAUQ( I ), TAUP( I ), WORK, IINFO )
264:       WORK( 1 ) = WS
265:       RETURN
266: *
267: *     End of SGEBRD
268: *
269:       END
270: