001:       SUBROUTINE SGEBD2( M, N, A, LDA, D, E, TAUQ, TAUP, WORK, INFO )
002: *
003: *  -- LAPACK routine (version 3.2) --
004: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
005: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       INTEGER            INFO, LDA, M, N
010: *     ..
011: *     .. Array Arguments ..
012:       REAL               A( LDA, * ), D( * ), E( * ), TAUP( * ),
013:      $                   TAUQ( * ), WORK( * )
014: *     ..
015: *
016: *  Purpose
017: *  =======
018: *
019: *  SGEBD2 reduces a real general m by n matrix A to upper or lower
020: *  bidiagonal form B by an orthogonal transformation: Q' * A * P = B.
021: *
022: *  If m >= n, B is upper bidiagonal; if m < n, B is lower bidiagonal.
023: *
024: *  Arguments
025: *  =========
026: *
027: *  M       (input) INTEGER
028: *          The number of rows in the matrix A.  M >= 0.
029: *
030: *  N       (input) INTEGER
031: *          The number of columns in the matrix A.  N >= 0.
032: *
033: *  A       (input/output) REAL array, dimension (LDA,N)
034: *          On entry, the m by n general matrix to be reduced.
035: *          On exit,
036: *          if m >= n, the diagonal and the first superdiagonal are
037: *            overwritten with the upper bidiagonal matrix B; the
038: *            elements below the diagonal, with the array TAUQ, represent
039: *            the orthogonal matrix Q as a product of elementary
040: *            reflectors, and the elements above the first superdiagonal,
041: *            with the array TAUP, represent the orthogonal matrix P as
042: *            a product of elementary reflectors;
043: *          if m < n, the diagonal and the first subdiagonal are
044: *            overwritten with the lower bidiagonal matrix B; the
045: *            elements below the first subdiagonal, with the array TAUQ,
046: *            represent the orthogonal matrix Q as a product of
047: *            elementary reflectors, and the elements above the diagonal,
048: *            with the array TAUP, represent the orthogonal matrix P as
049: *            a product of elementary reflectors.
050: *          See Further Details.
051: *
052: *  LDA     (input) INTEGER
053: *          The leading dimension of the array A.  LDA >= max(1,M).
054: *
055: *  D       (output) REAL array, dimension (min(M,N))
056: *          The diagonal elements of the bidiagonal matrix B:
057: *          D(i) = A(i,i).
058: *
059: *  E       (output) REAL array, dimension (min(M,N)-1)
060: *          The off-diagonal elements of the bidiagonal matrix B:
061: *          if m >= n, E(i) = A(i,i+1) for i = 1,2,...,n-1;
062: *          if m < n, E(i) = A(i+1,i) for i = 1,2,...,m-1.
063: *
064: *  TAUQ    (output) REAL array dimension (min(M,N))
065: *          The scalar factors of the elementary reflectors which
066: *          represent the orthogonal matrix Q. See Further Details.
067: *
068: *  TAUP    (output) REAL array, dimension (min(M,N))
069: *          The scalar factors of the elementary reflectors which
070: *          represent the orthogonal matrix P. See Further Details.
071: *
072: *  WORK    (workspace) REAL array, dimension (max(M,N))
073: *
074: *  INFO    (output) INTEGER
075: *          = 0: successful exit.
076: *          < 0: if INFO = -i, the i-th argument had an illegal value.
077: *
078: *  Further Details
079: *  ===============
080: *
081: *  The matrices Q and P are represented as products of elementary
082: *  reflectors:
083: *
084: *  If m >= n,
085: *
086: *     Q = H(1) H(2) . . . H(n)  and  P = G(1) G(2) . . . G(n-1)
087: *
088: *  Each H(i) and G(i) has the form:
089: *
090: *     H(i) = I - tauq * v * v'  and G(i) = I - taup * u * u'
091: *
092: *  where tauq and taup are real scalars, and v and u are real vectors;
093: *  v(1:i-1) = 0, v(i) = 1, and v(i+1:m) is stored on exit in A(i+1:m,i);
094: *  u(1:i) = 0, u(i+1) = 1, and u(i+2:n) is stored on exit in A(i,i+2:n);
095: *  tauq is stored in TAUQ(i) and taup in TAUP(i).
096: *
097: *  If m < n,
098: *
099: *     Q = H(1) H(2) . . . H(m-1)  and  P = G(1) G(2) . . . G(m)
100: *
101: *  Each H(i) and G(i) has the form:
102: *
103: *     H(i) = I - tauq * v * v'  and G(i) = I - taup * u * u'
104: *
105: *  where tauq and taup are real scalars, and v and u are real vectors;
106: *  v(1:i) = 0, v(i+1) = 1, and v(i+2:m) is stored on exit in A(i+2:m,i);
107: *  u(1:i-1) = 0, u(i) = 1, and u(i+1:n) is stored on exit in A(i,i+1:n);
108: *  tauq is stored in TAUQ(i) and taup in TAUP(i).
109: *
110: *  The contents of A on exit are illustrated by the following examples:
111: *
112: *  m = 6 and n = 5 (m > n):          m = 5 and n = 6 (m < n):
113: *
114: *    (  d   e   u1  u1  u1 )           (  d   u1  u1  u1  u1  u1 )
115: *    (  v1  d   e   u2  u2 )           (  e   d   u2  u2  u2  u2 )
116: *    (  v1  v2  d   e   u3 )           (  v1  e   d   u3  u3  u3 )
117: *    (  v1  v2  v3  d   e  )           (  v1  v2  e   d   u4  u4 )
118: *    (  v1  v2  v3  v4  d  )           (  v1  v2  v3  e   d   u5 )
119: *    (  v1  v2  v3  v4  v5 )
120: *
121: *  where d and e denote diagonal and off-diagonal elements of B, vi
122: *  denotes an element of the vector defining H(i), and ui an element of
123: *  the vector defining G(i).
124: *
125: *  =====================================================================
126: *
127: *     .. Parameters ..
128:       REAL               ZERO, ONE
129:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
130: *     ..
131: *     .. Local Scalars ..
132:       INTEGER            I
133: *     ..
134: *     .. External Subroutines ..
135:       EXTERNAL           SLARF, SLARFG, XERBLA
136: *     ..
137: *     .. Intrinsic Functions ..
138:       INTRINSIC          MAX, MIN
139: *     ..
140: *     .. Executable Statements ..
141: *
142: *     Test the input parameters
143: *
144:       INFO = 0
145:       IF( M.LT.0 ) THEN
146:          INFO = -1
147:       ELSE IF( N.LT.0 ) THEN
148:          INFO = -2
149:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
150:          INFO = -4
151:       END IF
152:       IF( INFO.LT.0 ) THEN
153:          CALL XERBLA( 'SGEBD2', -INFO )
154:          RETURN
155:       END IF
156: *
157:       IF( M.GE.N ) THEN
158: *
159: *        Reduce to upper bidiagonal form
160: *
161:          DO 10 I = 1, N
162: *
163: *           Generate elementary reflector H(i) to annihilate A(i+1:m,i)
164: *
165:             CALL SLARFG( M-I+1, A( I, I ), A( MIN( I+1, M ), I ), 1,
166:      $                   TAUQ( I ) )
167:             D( I ) = A( I, I )
168:             A( I, I ) = ONE
169: *
170: *           Apply H(i) to A(i:m,i+1:n) from the left
171: *
172:             IF( I.LT.N )
173:      $         CALL SLARF( 'Left', M-I+1, N-I, A( I, I ), 1, TAUQ( I ),
174:      $                     A( I, I+1 ), LDA, WORK )
175:             A( I, I ) = D( I )
176: *
177:             IF( I.LT.N ) THEN
178: *
179: *              Generate elementary reflector G(i) to annihilate
180: *              A(i,i+2:n)
181: *
182:                CALL SLARFG( N-I, A( I, I+1 ), A( I, MIN( I+2, N ) ),
183:      $                      LDA, TAUP( I ) )
184:                E( I ) = A( I, I+1 )
185:                A( I, I+1 ) = ONE
186: *
187: *              Apply G(i) to A(i+1:m,i+1:n) from the right
188: *
189:                CALL SLARF( 'Right', M-I, N-I, A( I, I+1 ), LDA,
190:      $                     TAUP( I ), A( I+1, I+1 ), LDA, WORK )
191:                A( I, I+1 ) = E( I )
192:             ELSE
193:                TAUP( I ) = ZERO
194:             END IF
195:    10    CONTINUE
196:       ELSE
197: *
198: *        Reduce to lower bidiagonal form
199: *
200:          DO 20 I = 1, M
201: *
202: *           Generate elementary reflector G(i) to annihilate A(i,i+1:n)
203: *
204:             CALL SLARFG( N-I+1, A( I, I ), A( I, MIN( I+1, N ) ), LDA,
205:      $                   TAUP( I ) )
206:             D( I ) = A( I, I )
207:             A( I, I ) = ONE
208: *
209: *           Apply G(i) to A(i+1:m,i:n) from the right
210: *
211:             IF( I.LT.M )
212:      $         CALL SLARF( 'Right', M-I, N-I+1, A( I, I ), LDA,
213:      $                     TAUP( I ), A( I+1, I ), LDA, WORK )
214:             A( I, I ) = D( I )
215: *
216:             IF( I.LT.M ) THEN
217: *
218: *              Generate elementary reflector H(i) to annihilate
219: *              A(i+2:m,i)
220: *
221:                CALL SLARFG( M-I, A( I+1, I ), A( MIN( I+2, M ), I ), 1,
222:      $                      TAUQ( I ) )
223:                E( I ) = A( I+1, I )
224:                A( I+1, I ) = ONE
225: *
226: *              Apply H(i) to A(i+1:m,i+1:n) from the left
227: *
228:                CALL SLARF( 'Left', M-I, N-I, A( I+1, I ), 1, TAUQ( I ),
229:      $                     A( I+1, I+1 ), LDA, WORK )
230:                A( I+1, I ) = E( I )
231:             ELSE
232:                TAUQ( I ) = ZERO
233:             END IF
234:    20    CONTINUE
235:       END IF
236:       RETURN
237: *
238: *     End of SGEBD2
239: *
240:       END
241: