001:       SUBROUTINE SORGBR( VECT, M, N, K, A, LDA, TAU, WORK, LWORK, 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:       CHARACTER          VECT
009:       INTEGER            INFO, K, LDA, LWORK, M, N
010: *     ..
011: *     .. Array Arguments ..
012:       REAL               A( LDA, * ), TAU( * ), WORK( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  SORGBR generates one of the real orthogonal matrices Q or P**T
019: *  determined by SGEBRD when reducing a real matrix A to bidiagonal
020: *  form: A = Q * B * P**T.  Q and P**T are defined as products of
021: *  elementary reflectors H(i) or G(i) respectively.
022: *
023: *  If VECT = 'Q', A is assumed to have been an M-by-K matrix, and Q
024: *  is of order M:
025: *  if m >= k, Q = H(1) H(2) . . . H(k) and SORGBR returns the first n
026: *  columns of Q, where m >= n >= k;
027: *  if m < k, Q = H(1) H(2) . . . H(m-1) and SORGBR returns Q as an
028: *  M-by-M matrix.
029: *
030: *  If VECT = 'P', A is assumed to have been a K-by-N matrix, and P**T
031: *  is of order N:
032: *  if k < n, P**T = G(k) . . . G(2) G(1) and SORGBR returns the first m
033: *  rows of P**T, where n >= m >= k;
034: *  if k >= n, P**T = G(n-1) . . . G(2) G(1) and SORGBR returns P**T as
035: *  an N-by-N matrix.
036: *
037: *  Arguments
038: *  =========
039: *
040: *  VECT    (input) CHARACTER*1
041: *          Specifies whether the matrix Q or the matrix P**T is
042: *          required, as defined in the transformation applied by SGEBRD:
043: *          = 'Q':  generate Q;
044: *          = 'P':  generate P**T.
045: *
046: *  M       (input) INTEGER
047: *          The number of rows of the matrix Q or P**T to be returned.
048: *          M >= 0.
049: *
050: *  N       (input) INTEGER
051: *          The number of columns of the matrix Q or P**T to be returned.
052: *          N >= 0.
053: *          If VECT = 'Q', M >= N >= min(M,K);
054: *          if VECT = 'P', N >= M >= min(N,K).
055: *
056: *  K       (input) INTEGER
057: *          If VECT = 'Q', the number of columns in the original M-by-K
058: *          matrix reduced by SGEBRD.
059: *          If VECT = 'P', the number of rows in the original K-by-N
060: *          matrix reduced by SGEBRD.
061: *          K >= 0.
062: *
063: *  A       (input/output) REAL array, dimension (LDA,N)
064: *          On entry, the vectors which define the elementary reflectors,
065: *          as returned by SGEBRD.
066: *          On exit, the M-by-N matrix Q or P**T.
067: *
068: *  LDA     (input) INTEGER
069: *          The leading dimension of the array A. LDA >= max(1,M).
070: *
071: *  TAU     (input) REAL array, dimension
072: *                                (min(M,K)) if VECT = 'Q'
073: *                                (min(N,K)) if VECT = 'P'
074: *          TAU(i) must contain the scalar factor of the elementary
075: *          reflector H(i) or G(i), which determines Q or P**T, as
076: *          returned by SGEBRD in its array argument TAUQ or TAUP.
077: *
078: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
079: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
080: *
081: *  LWORK   (input) INTEGER
082: *          The dimension of the array WORK. LWORK >= max(1,min(M,N)).
083: *          For optimum performance LWORK >= min(M,N)*NB, where NB
084: *          is the optimal blocksize.
085: *
086: *          If LWORK = -1, then a workspace query is assumed; the routine
087: *          only calculates the optimal size of the WORK array, returns
088: *          this value as the first entry of the WORK array, and no error
089: *          message related to LWORK is issued by XERBLA.
090: *
091: *  INFO    (output) INTEGER
092: *          = 0:  successful exit
093: *          < 0:  if INFO = -i, the i-th argument had an illegal value
094: *
095: *  =====================================================================
096: *
097: *     .. Parameters ..
098:       REAL               ZERO, ONE
099:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
100: *     ..
101: *     .. Local Scalars ..
102:       LOGICAL            LQUERY, WANTQ
103:       INTEGER            I, IINFO, J, LWKOPT, MN, NB
104: *     ..
105: *     .. External Functions ..
106:       LOGICAL            LSAME
107:       INTEGER            ILAENV
108:       EXTERNAL           ILAENV, LSAME
109: *     ..
110: *     .. External Subroutines ..
111:       EXTERNAL           SORGLQ, SORGQR, XERBLA
112: *     ..
113: *     .. Intrinsic Functions ..
114:       INTRINSIC          MAX, MIN
115: *     ..
116: *     .. Executable Statements ..
117: *
118: *     Test the input arguments
119: *
120:       INFO = 0
121:       WANTQ = LSAME( VECT, 'Q' )
122:       MN = MIN( M, N )
123:       LQUERY = ( LWORK.EQ.-1 )
124:       IF( .NOT.WANTQ .AND. .NOT.LSAME( VECT, 'P' ) ) THEN
125:          INFO = -1
126:       ELSE IF( M.LT.0 ) THEN
127:          INFO = -2
128:       ELSE IF( N.LT.0 .OR. ( WANTQ .AND. ( N.GT.M .OR. N.LT.MIN( M,
129:      $         K ) ) ) .OR. ( .NOT.WANTQ .AND. ( M.GT.N .OR. M.LT.
130:      $         MIN( N, K ) ) ) ) THEN
131:          INFO = -3
132:       ELSE IF( K.LT.0 ) THEN
133:          INFO = -4
134:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
135:          INFO = -6
136:       ELSE IF( LWORK.LT.MAX( 1, MN ) .AND. .NOT.LQUERY ) THEN
137:          INFO = -9
138:       END IF
139: *
140:       IF( INFO.EQ.0 ) THEN
141:          IF( WANTQ ) THEN
142:             NB = ILAENV( 1, 'SORGQR', ' ', M, N, K, -1 )
143:          ELSE
144:             NB = ILAENV( 1, 'SORGLQ', ' ', M, N, K, -1 )
145:          END IF
146:          LWKOPT = MAX( 1, MN )*NB
147:          WORK( 1 ) = LWKOPT
148:       END IF
149: *
150:       IF( INFO.NE.0 ) THEN
151:          CALL XERBLA( 'SORGBR', -INFO )
152:          RETURN
153:       ELSE IF( LQUERY ) THEN
154:          RETURN
155:       END IF
156: *
157: *     Quick return if possible
158: *
159:       IF( M.EQ.0 .OR. N.EQ.0 ) THEN
160:          WORK( 1 ) = 1
161:          RETURN
162:       END IF
163: *
164:       IF( WANTQ ) THEN
165: *
166: *        Form Q, determined by a call to SGEBRD to reduce an m-by-k
167: *        matrix
168: *
169:          IF( M.GE.K ) THEN
170: *
171: *           If m >= k, assume m >= n >= k
172: *
173:             CALL SORGQR( M, N, K, A, LDA, TAU, WORK, LWORK, IINFO )
174: *
175:          ELSE
176: *
177: *           If m < k, assume m = n
178: *
179: *           Shift the vectors which define the elementary reflectors one
180: *           column to the right, and set the first row and column of Q
181: *           to those of the unit matrix
182: *
183:             DO 20 J = M, 2, -1
184:                A( 1, J ) = ZERO
185:                DO 10 I = J + 1, M
186:                   A( I, J ) = A( I, J-1 )
187:    10          CONTINUE
188:    20       CONTINUE
189:             A( 1, 1 ) = ONE
190:             DO 30 I = 2, M
191:                A( I, 1 ) = ZERO
192:    30       CONTINUE
193:             IF( M.GT.1 ) THEN
194: *
195: *              Form Q(2:m,2:m)
196: *
197:                CALL SORGQR( M-1, M-1, M-1, A( 2, 2 ), LDA, TAU, WORK,
198:      $                      LWORK, IINFO )
199:             END IF
200:          END IF
201:       ELSE
202: *
203: *        Form P', determined by a call to SGEBRD to reduce a k-by-n
204: *        matrix
205: *
206:          IF( K.LT.N ) THEN
207: *
208: *           If k < n, assume k <= m <= n
209: *
210:             CALL SORGLQ( M, N, K, A, LDA, TAU, WORK, LWORK, IINFO )
211: *
212:          ELSE
213: *
214: *           If k >= n, assume m = n
215: *
216: *           Shift the vectors which define the elementary reflectors one
217: *           row downward, and set the first row and column of P' to
218: *           those of the unit matrix
219: *
220:             A( 1, 1 ) = ONE
221:             DO 40 I = 2, N
222:                A( I, 1 ) = ZERO
223:    40       CONTINUE
224:             DO 60 J = 2, N
225:                DO 50 I = J - 1, 2, -1
226:                   A( I, J ) = A( I-1, J )
227:    50          CONTINUE
228:                A( 1, J ) = ZERO
229:    60       CONTINUE
230:             IF( N.GT.1 ) THEN
231: *
232: *              Form P'(2:n,2:n)
233: *
234:                CALL SORGLQ( N-1, N-1, N-1, A( 2, 2 ), LDA, TAU, WORK,
235:      $                      LWORK, IINFO )
236:             END IF
237:          END IF
238:       END IF
239:       WORK( 1 ) = LWKOPT
240:       RETURN
241: *
242: *     End of SORGBR
243: *
244:       END
245: