001:       SUBROUTINE SGBBRD( VECT, M, N, NCC, KL, KU, AB, LDAB, D, E, Q,
002:      $                   LDQ, PT, LDPT, C, LDC, WORK, 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:       CHARACTER          VECT
011:       INTEGER            INFO, KL, KU, LDAB, LDC, LDPT, LDQ, M, N, NCC
012: *     ..
013: *     .. Array Arguments ..
014:       REAL               AB( LDAB, * ), C( LDC, * ), D( * ), E( * ),
015:      $                   PT( LDPT, * ), Q( LDQ, * ), WORK( * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  SGBBRD reduces a real general m-by-n band matrix A to upper
022: *  bidiagonal form B by an orthogonal transformation: Q' * A * P = B.
023: *
024: *  The routine computes B, and optionally forms Q or P', or computes
025: *  Q'*C for a given matrix C.
026: *
027: *  Arguments
028: *  =========
029: *
030: *  VECT    (input) CHARACTER*1
031: *          Specifies whether or not the matrices Q and P' are to be
032: *          formed.
033: *          = 'N': do not form Q or P';
034: *          = 'Q': form Q only;
035: *          = 'P': form P' only;
036: *          = 'B': form both.
037: *
038: *  M       (input) INTEGER
039: *          The number of rows of the matrix A.  M >= 0.
040: *
041: *  N       (input) INTEGER
042: *          The number of columns of the matrix A.  N >= 0.
043: *
044: *  NCC     (input) INTEGER
045: *          The number of columns of the matrix C.  NCC >= 0.
046: *
047: *  KL      (input) INTEGER
048: *          The number of subdiagonals of the matrix A. KL >= 0.
049: *
050: *  KU      (input) INTEGER
051: *          The number of superdiagonals of the matrix A. KU >= 0.
052: *
053: *  AB      (input/output) REAL array, dimension (LDAB,N)
054: *          On entry, the m-by-n band matrix A, stored in rows 1 to
055: *          KL+KU+1. The j-th column of A is stored in the j-th column of
056: *          the array AB as follows:
057: *          AB(ku+1+i-j,j) = A(i,j) for max(1,j-ku)<=i<=min(m,j+kl).
058: *          On exit, A is overwritten by values generated during the
059: *          reduction.
060: *
061: *  LDAB    (input) INTEGER
062: *          The leading dimension of the array A. LDAB >= KL+KU+1.
063: *
064: *  D       (output) REAL array, dimension (min(M,N))
065: *          The diagonal elements of the bidiagonal matrix B.
066: *
067: *  E       (output) REAL array, dimension (min(M,N)-1)
068: *          The superdiagonal elements of the bidiagonal matrix B.
069: *
070: *  Q       (output) REAL array, dimension (LDQ,M)
071: *          If VECT = 'Q' or 'B', the m-by-m orthogonal matrix Q.
072: *          If VECT = 'N' or 'P', the array Q is not referenced.
073: *
074: *  LDQ     (input) INTEGER
075: *          The leading dimension of the array Q.
076: *          LDQ >= max(1,M) if VECT = 'Q' or 'B'; LDQ >= 1 otherwise.
077: *
078: *  PT      (output) REAL array, dimension (LDPT,N)
079: *          If VECT = 'P' or 'B', the n-by-n orthogonal matrix P'.
080: *          If VECT = 'N' or 'Q', the array PT is not referenced.
081: *
082: *  LDPT    (input) INTEGER
083: *          The leading dimension of the array PT.
084: *          LDPT >= max(1,N) if VECT = 'P' or 'B'; LDPT >= 1 otherwise.
085: *
086: *  C       (input/output) REAL array, dimension (LDC,NCC)
087: *          On entry, an m-by-ncc matrix C.
088: *          On exit, C is overwritten by Q'*C.
089: *          C is not referenced if NCC = 0.
090: *
091: *  LDC     (input) INTEGER
092: *          The leading dimension of the array C.
093: *          LDC >= max(1,M) if NCC > 0; LDC >= 1 if NCC = 0.
094: *
095: *  WORK    (workspace) REAL array, dimension (2*max(M,N))
096: *
097: *  INFO    (output) INTEGER
098: *          = 0:  successful exit.
099: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
100: *
101: *  =====================================================================
102: *
103: *     .. Parameters ..
104:       REAL               ZERO, ONE
105:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
106: *     ..
107: *     .. Local Scalars ..
108:       LOGICAL            WANTB, WANTC, WANTPT, WANTQ
109:       INTEGER            I, INCA, J, J1, J2, KB, KB1, KK, KLM, KLU1,
110:      $                   KUN, L, MINMN, ML, ML0, MN, MU, MU0, NR, NRT
111:       REAL               RA, RB, RC, RS
112: *     ..
113: *     .. External Subroutines ..
114:       EXTERNAL           SLARGV, SLARTG, SLARTV, SLASET, SROT, XERBLA
115: *     ..
116: *     .. Intrinsic Functions ..
117:       INTRINSIC          MAX, MIN
118: *     ..
119: *     .. External Functions ..
120:       LOGICAL            LSAME
121:       EXTERNAL           LSAME
122: *     ..
123: *     .. Executable Statements ..
124: *
125: *     Test the input parameters
126: *
127:       WANTB = LSAME( VECT, 'B' )
128:       WANTQ = LSAME( VECT, 'Q' ) .OR. WANTB
129:       WANTPT = LSAME( VECT, 'P' ) .OR. WANTB
130:       WANTC = NCC.GT.0
131:       KLU1 = KL + KU + 1
132:       INFO = 0
133:       IF( .NOT.WANTQ .AND. .NOT.WANTPT .AND. .NOT.LSAME( VECT, 'N' ) )
134:      $     THEN
135:          INFO = -1
136:       ELSE IF( M.LT.0 ) THEN
137:          INFO = -2
138:       ELSE IF( N.LT.0 ) THEN
139:          INFO = -3
140:       ELSE IF( NCC.LT.0 ) THEN
141:          INFO = -4
142:       ELSE IF( KL.LT.0 ) THEN
143:          INFO = -5
144:       ELSE IF( KU.LT.0 ) THEN
145:          INFO = -6
146:       ELSE IF( LDAB.LT.KLU1 ) THEN
147:          INFO = -8
148:       ELSE IF( LDQ.LT.1 .OR. WANTQ .AND. LDQ.LT.MAX( 1, M ) ) THEN
149:          INFO = -12
150:       ELSE IF( LDPT.LT.1 .OR. WANTPT .AND. LDPT.LT.MAX( 1, N ) ) THEN
151:          INFO = -14
152:       ELSE IF( LDC.LT.1 .OR. WANTC .AND. LDC.LT.MAX( 1, M ) ) THEN
153:          INFO = -16
154:       END IF
155:       IF( INFO.NE.0 ) THEN
156:          CALL XERBLA( 'SGBBRD', -INFO )
157:          RETURN
158:       END IF
159: *
160: *     Initialize Q and P' to the unit matrix, if needed
161: *
162:       IF( WANTQ )
163:      $   CALL SLASET( 'Full', M, M, ZERO, ONE, Q, LDQ )
164:       IF( WANTPT )
165:      $   CALL SLASET( 'Full', N, N, ZERO, ONE, PT, LDPT )
166: *
167: *     Quick return if possible.
168: *
169:       IF( M.EQ.0 .OR. N.EQ.0 )
170:      $   RETURN
171: *
172:       MINMN = MIN( M, N )
173: *
174:       IF( KL+KU.GT.1 ) THEN
175: *
176: *        Reduce to upper bidiagonal form if KU > 0; if KU = 0, reduce
177: *        first to lower bidiagonal form and then transform to upper
178: *        bidiagonal
179: *
180:          IF( KU.GT.0 ) THEN
181:             ML0 = 1
182:             MU0 = 2
183:          ELSE
184:             ML0 = 2
185:             MU0 = 1
186:          END IF
187: *
188: *        Wherever possible, plane rotations are generated and applied in
189: *        vector operations of length NR over the index set J1:J2:KLU1.
190: *
191: *        The sines of the plane rotations are stored in WORK(1:max(m,n))
192: *        and the cosines in WORK(max(m,n)+1:2*max(m,n)).
193: *
194:          MN = MAX( M, N )
195:          KLM = MIN( M-1, KL )
196:          KUN = MIN( N-1, KU )
197:          KB = KLM + KUN
198:          KB1 = KB + 1
199:          INCA = KB1*LDAB
200:          NR = 0
201:          J1 = KLM + 2
202:          J2 = 1 - KUN
203: *
204:          DO 90 I = 1, MINMN
205: *
206: *           Reduce i-th column and i-th row of matrix to bidiagonal form
207: *
208:             ML = KLM + 1
209:             MU = KUN + 1
210:             DO 80 KK = 1, KB
211:                J1 = J1 + KB
212:                J2 = J2 + KB
213: *
214: *              generate plane rotations to annihilate nonzero elements
215: *              which have been created below the band
216: *
217:                IF( NR.GT.0 )
218:      $            CALL SLARGV( NR, AB( KLU1, J1-KLM-1 ), INCA,
219:      $                         WORK( J1 ), KB1, WORK( MN+J1 ), KB1 )
220: *
221: *              apply plane rotations from the left
222: *
223:                DO 10 L = 1, KB
224:                   IF( J2-KLM+L-1.GT.N ) THEN
225:                      NRT = NR - 1
226:                   ELSE
227:                      NRT = NR
228:                   END IF
229:                   IF( NRT.GT.0 )
230:      $               CALL SLARTV( NRT, AB( KLU1-L, J1-KLM+L-1 ), INCA,
231:      $                            AB( KLU1-L+1, J1-KLM+L-1 ), INCA,
232:      $                            WORK( MN+J1 ), WORK( J1 ), KB1 )
233:    10          CONTINUE
234: *
235:                IF( ML.GT.ML0 ) THEN
236:                   IF( ML.LE.M-I+1 ) THEN
237: *
238: *                    generate plane rotation to annihilate a(i+ml-1,i)
239: *                    within the band, and apply rotation from the left
240: *
241:                      CALL SLARTG( AB( KU+ML-1, I ), AB( KU+ML, I ),
242:      $                            WORK( MN+I+ML-1 ), WORK( I+ML-1 ),
243:      $                            RA )
244:                      AB( KU+ML-1, I ) = RA
245:                      IF( I.LT.N )
246:      $                  CALL SROT( MIN( KU+ML-2, N-I ),
247:      $                             AB( KU+ML-2, I+1 ), LDAB-1,
248:      $                             AB( KU+ML-1, I+1 ), LDAB-1,
249:      $                             WORK( MN+I+ML-1 ), WORK( I+ML-1 ) )
250:                   END IF
251:                   NR = NR + 1
252:                   J1 = J1 - KB1
253:                END IF
254: *
255:                IF( WANTQ ) THEN
256: *
257: *                 accumulate product of plane rotations in Q
258: *
259:                   DO 20 J = J1, J2, KB1
260:                      CALL SROT( M, Q( 1, J-1 ), 1, Q( 1, J ), 1,
261:      $                          WORK( MN+J ), WORK( J ) )
262:    20             CONTINUE
263:                END IF
264: *
265:                IF( WANTC ) THEN
266: *
267: *                 apply plane rotations to C
268: *
269:                   DO 30 J = J1, J2, KB1
270:                      CALL SROT( NCC, C( J-1, 1 ), LDC, C( J, 1 ), LDC,
271:      $                          WORK( MN+J ), WORK( J ) )
272:    30             CONTINUE
273:                END IF
274: *
275:                IF( J2+KUN.GT.N ) THEN
276: *
277: *                 adjust J2 to keep within the bounds of the matrix
278: *
279:                   NR = NR - 1
280:                   J2 = J2 - KB1
281:                END IF
282: *
283:                DO 40 J = J1, J2, KB1
284: *
285: *                 create nonzero element a(j-1,j+ku) above the band
286: *                 and store it in WORK(n+1:2*n)
287: *
288:                   WORK( J+KUN ) = WORK( J )*AB( 1, J+KUN )
289:                   AB( 1, J+KUN ) = WORK( MN+J )*AB( 1, J+KUN )
290:    40          CONTINUE
291: *
292: *              generate plane rotations to annihilate nonzero elements
293: *              which have been generated above the band
294: *
295:                IF( NR.GT.0 )
296:      $            CALL SLARGV( NR, AB( 1, J1+KUN-1 ), INCA,
297:      $                         WORK( J1+KUN ), KB1, WORK( MN+J1+KUN ),
298:      $                         KB1 )
299: *
300: *              apply plane rotations from the right
301: *
302:                DO 50 L = 1, KB
303:                   IF( J2+L-1.GT.M ) THEN
304:                      NRT = NR - 1
305:                   ELSE
306:                      NRT = NR
307:                   END IF
308:                   IF( NRT.GT.0 )
309:      $               CALL SLARTV( NRT, AB( L+1, J1+KUN-1 ), INCA,
310:      $                            AB( L, J1+KUN ), INCA,
311:      $                            WORK( MN+J1+KUN ), WORK( J1+KUN ),
312:      $                            KB1 )
313:    50          CONTINUE
314: *
315:                IF( ML.EQ.ML0 .AND. MU.GT.MU0 ) THEN
316:                   IF( MU.LE.N-I+1 ) THEN
317: *
318: *                    generate plane rotation to annihilate a(i,i+mu-1)
319: *                    within the band, and apply rotation from the right
320: *
321:                      CALL SLARTG( AB( KU-MU+3, I+MU-2 ),
322:      $                            AB( KU-MU+2, I+MU-1 ),
323:      $                            WORK( MN+I+MU-1 ), WORK( I+MU-1 ),
324:      $                            RA )
325:                      AB( KU-MU+3, I+MU-2 ) = RA
326:                      CALL SROT( MIN( KL+MU-2, M-I ),
327:      $                          AB( KU-MU+4, I+MU-2 ), 1,
328:      $                          AB( KU-MU+3, I+MU-1 ), 1,
329:      $                          WORK( MN+I+MU-1 ), WORK( I+MU-1 ) )
330:                   END IF
331:                   NR = NR + 1
332:                   J1 = J1 - KB1
333:                END IF
334: *
335:                IF( WANTPT ) THEN
336: *
337: *                 accumulate product of plane rotations in P'
338: *
339:                   DO 60 J = J1, J2, KB1
340:                      CALL SROT( N, PT( J+KUN-1, 1 ), LDPT,
341:      $                          PT( J+KUN, 1 ), LDPT, WORK( MN+J+KUN ),
342:      $                          WORK( J+KUN ) )
343:    60             CONTINUE
344:                END IF
345: *
346:                IF( J2+KB.GT.M ) THEN
347: *
348: *                 adjust J2 to keep within the bounds of the matrix
349: *
350:                   NR = NR - 1
351:                   J2 = J2 - KB1
352:                END IF
353: *
354:                DO 70 J = J1, J2, KB1
355: *
356: *                 create nonzero element a(j+kl+ku,j+ku-1) below the
357: *                 band and store it in WORK(1:n)
358: *
359:                   WORK( J+KB ) = WORK( J+KUN )*AB( KLU1, J+KUN )
360:                   AB( KLU1, J+KUN ) = WORK( MN+J+KUN )*AB( KLU1, J+KUN )
361:    70          CONTINUE
362: *
363:                IF( ML.GT.ML0 ) THEN
364:                   ML = ML - 1
365:                ELSE
366:                   MU = MU - 1
367:                END IF
368:    80       CONTINUE
369:    90    CONTINUE
370:       END IF
371: *
372:       IF( KU.EQ.0 .AND. KL.GT.0 ) THEN
373: *
374: *        A has been reduced to lower bidiagonal form
375: *
376: *        Transform lower bidiagonal form to upper bidiagonal by applying
377: *        plane rotations from the left, storing diagonal elements in D
378: *        and off-diagonal elements in E
379: *
380:          DO 100 I = 1, MIN( M-1, N )
381:             CALL SLARTG( AB( 1, I ), AB( 2, I ), RC, RS, RA )
382:             D( I ) = RA
383:             IF( I.LT.N ) THEN
384:                E( I ) = RS*AB( 1, I+1 )
385:                AB( 1, I+1 ) = RC*AB( 1, I+1 )
386:             END IF
387:             IF( WANTQ )
388:      $         CALL SROT( M, Q( 1, I ), 1, Q( 1, I+1 ), 1, RC, RS )
389:             IF( WANTC )
390:      $         CALL SROT( NCC, C( I, 1 ), LDC, C( I+1, 1 ), LDC, RC,
391:      $                    RS )
392:   100    CONTINUE
393:          IF( M.LE.N )
394:      $      D( M ) = AB( 1, M )
395:       ELSE IF( KU.GT.0 ) THEN
396: *
397: *        A has been reduced to upper bidiagonal form
398: *
399:          IF( M.LT.N ) THEN
400: *
401: *           Annihilate a(m,m+1) by applying plane rotations from the
402: *           right, storing diagonal elements in D and off-diagonal
403: *           elements in E
404: *
405:             RB = AB( KU, M+1 )
406:             DO 110 I = M, 1, -1
407:                CALL SLARTG( AB( KU+1, I ), RB, RC, RS, RA )
408:                D( I ) = RA
409:                IF( I.GT.1 ) THEN
410:                   RB = -RS*AB( KU, I )
411:                   E( I-1 ) = RC*AB( KU, I )
412:                END IF
413:                IF( WANTPT )
414:      $            CALL SROT( N, PT( I, 1 ), LDPT, PT( M+1, 1 ), LDPT,
415:      $                       RC, RS )
416:   110       CONTINUE
417:          ELSE
418: *
419: *           Copy off-diagonal elements to E and diagonal elements to D
420: *
421:             DO 120 I = 1, MINMN - 1
422:                E( I ) = AB( KU, I+1 )
423:   120       CONTINUE
424:             DO 130 I = 1, MINMN
425:                D( I ) = AB( KU+1, I )
426:   130       CONTINUE
427:          END IF
428:       ELSE
429: *
430: *        A is diagonal. Set elements of E to zero and copy diagonal
431: *        elements to D.
432: *
433:          DO 140 I = 1, MINMN - 1
434:             E( I ) = ZERO
435:   140    CONTINUE
436:          DO 150 I = 1, MINMN
437:             D( I ) = AB( 1, I )
438:   150    CONTINUE
439:       END IF
440:       RETURN
441: *
442: *     End of SGBBRD
443: *
444:       END
445: