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