1*> \brief \b DORMQR
2*
3*  =========== DOCUMENTATION ===========
4*
5* Online html documentation available at
6*            http://www.netlib.org/lapack/explore-html/
7*
8*> \htmlonly
9*> Download DORMQR + dependencies
10*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dormqr.f">
11*> [TGZ]</a>
12*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dormqr.f">
13*> [ZIP]</a>
14*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dormqr.f">
15*> [TXT]</a>
16*> \endhtmlonly
17*
18*  Definition:
19*  ===========
20*
21*       SUBROUTINE DORMQR( SIDE, TRANS, M, N, K, A, LDA, TAU, C, LDC,
22*                          WORK, LWORK, INFO )
23*
24*       .. Scalar Arguments ..
25*       CHARACTER          SIDE, TRANS
26*       INTEGER            INFO, K, LDA, LDC, LWORK, M, N
27*       ..
28*       .. Array Arguments ..
29*       DOUBLE PRECISION   A( LDA, * ), C( LDC, * ), TAU( * ), WORK( * )
30*       ..
31*
32*
33*> \par Purpose:
34*  =============
35*>
36*> \verbatim
37*>
38*> DORMQR overwrites the general real M-by-N matrix C with
39*>
40*>                 SIDE = 'L'     SIDE = 'R'
41*> TRANS = 'N':      Q * C          C * Q
42*> TRANS = 'T':      Q**T * C       C * Q**T
43*>
44*> where Q is a real orthogonal matrix defined as the product of k
45*> elementary reflectors
46*>
47*>       Q = H(1) H(2) . . . H(k)
48*>
49*> as returned by DGEQRF. Q is of order M if SIDE = 'L' and of order N
50*> if SIDE = 'R'.
51*> \endverbatim
52*
53*  Arguments:
54*  ==========
55*
56*> \param[in] SIDE
57*> \verbatim
58*>          SIDE is CHARACTER*1
59*>          = 'L': apply Q or Q**T from the Left;
60*>          = 'R': apply Q or Q**T from the Right.
61*> \endverbatim
62*>
63*> \param[in] TRANS
64*> \verbatim
65*>          TRANS is CHARACTER*1
66*>          = 'N':  No transpose, apply Q;
67*>          = 'T':  Transpose, apply Q**T.
68*> \endverbatim
69*>
70*> \param[in] M
71*> \verbatim
72*>          M is INTEGER
73*>          The number of rows of the matrix C. M >= 0.
74*> \endverbatim
75*>
76*> \param[in] N
77*> \verbatim
78*>          N is INTEGER
79*>          The number of columns of the matrix C. N >= 0.
80*> \endverbatim
81*>
82*> \param[in] K
83*> \verbatim
84*>          K is INTEGER
85*>          The number of elementary reflectors whose product defines
86*>          the matrix Q.
87*>          If SIDE = 'L', M >= K >= 0;
88*>          if SIDE = 'R', N >= K >= 0.
89*> \endverbatim
90*>
91*> \param[in] A
92*> \verbatim
93*>          A is DOUBLE PRECISION array, dimension (LDA,K)
94*>          The i-th column must contain the vector which defines the
95*>          elementary reflector H(i), for i = 1,2,...,k, as returned by
96*>          DGEQRF in the first k columns of its array argument A.
97*> \endverbatim
98*>
99*> \param[in] LDA
100*> \verbatim
101*>          LDA is INTEGER
102*>          The leading dimension of the array A.
103*>          If SIDE = 'L', LDA >= max(1,M);
104*>          if SIDE = 'R', LDA >= max(1,N).
105*> \endverbatim
106*>
107*> \param[in] TAU
108*> \verbatim
109*>          TAU is DOUBLE PRECISION array, dimension (K)
110*>          TAU(i) must contain the scalar factor of the elementary
111*>          reflector H(i), as returned by DGEQRF.
112*> \endverbatim
113*>
114*> \param[in,out] C
115*> \verbatim
116*>          C is DOUBLE PRECISION array, dimension (LDC,N)
117*>          On entry, the M-by-N matrix C.
118*>          On exit, C is overwritten by Q*C or Q**T*C or C*Q**T or C*Q.
119*> \endverbatim
120*>
121*> \param[in] LDC
122*> \verbatim
123*>          LDC is INTEGER
124*>          The leading dimension of the array C. LDC >= max(1,M).
125*> \endverbatim
126*>
127*> \param[out] WORK
128*> \verbatim
129*>          WORK is DOUBLE PRECISION array, dimension (MAX(1,LWORK))
130*>          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
131*> \endverbatim
132*>
133*> \param[in] LWORK
134*> \verbatim
135*>          LWORK is INTEGER
136*>          The dimension of the array WORK.
137*>          If SIDE = 'L', LWORK >= max(1,N);
138*>          if SIDE = 'R', LWORK >= max(1,M).
139*>          For good performance, LWORK should generally be larger.
140*>
141*>          If LWORK = -1, then a workspace query is assumed; the routine
142*>          only calculates the optimal size of the WORK array, returns
143*>          this value as the first entry of the WORK array, and no error
144*>          message related to LWORK is issued by XERBLA.
145*> \endverbatim
146*>
147*> \param[out] INFO
148*> \verbatim
149*>          INFO is INTEGER
150*>          = 0:  successful exit
151*>          < 0:  if INFO = -i, the i-th argument had an illegal value
152*> \endverbatim
153*
154*  Authors:
155*  ========
156*
157*> \author Univ. of Tennessee
158*> \author Univ. of California Berkeley
159*> \author Univ. of Colorado Denver
160*> \author NAG Ltd.
161*
162*> \ingroup doubleOTHERcomputational
163*
164*  =====================================================================
165      SUBROUTINE DORMQR( SIDE, TRANS, M, N, K, A, LDA, TAU, C, LDC,
166     $                   WORK, LWORK, INFO )
167*
168*  -- LAPACK computational routine --
169*  -- LAPACK is a software package provided by Univ. of Tennessee,    --
170*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
171*
172*     .. Scalar Arguments ..
173      CHARACTER          SIDE, TRANS
174      INTEGER            INFO, K, LDA, LDC, LWORK, M, N
175*     ..
176*     .. Array Arguments ..
177      DOUBLE PRECISION   A( LDA, * ), C( LDC, * ), TAU( * ), WORK( * )
178*     ..
179*
180*  =====================================================================
181*
182*     .. Parameters ..
183      INTEGER            NBMAX, LDT, TSIZE
184      PARAMETER          ( NBMAX = 64, LDT = NBMAX+1,
185     $                     TSIZE = LDT*NBMAX )
186*     ..
187*     .. Local Scalars ..
188      LOGICAL            LEFT, LQUERY, NOTRAN
189      INTEGER            I, I1, I2, I3, IB, IC, IINFO, IWT, JC, LDWORK,
190     $                   LWKOPT, MI, NB, NBMIN, NI, NQ, NW
191*     ..
192*     .. External Functions ..
193      LOGICAL            LSAME
194      INTEGER            ILAENV
195      EXTERNAL           LSAME, ILAENV
196*     ..
197*     .. External Subroutines ..
198      EXTERNAL           DLARFB, DLARFT, DORM2R, XERBLA
199*     ..
200*     .. Intrinsic Functions ..
201      INTRINSIC          MAX, MIN
202*     ..
203*     .. Executable Statements ..
204*
205*     Test the input arguments
206*
207      INFO = 0
208      LEFT = LSAME( SIDE, 'L' )
209      NOTRAN = LSAME( TRANS, 'N' )
210      LQUERY = ( LWORK.EQ.-1 )
211*
212*     NQ is the order of Q and NW is the minimum dimension of WORK
213*
214      IF( LEFT ) THEN
215         NQ = M
216         NW = MAX( 1, N )
217      ELSE
218         NQ = N
219         NW = MAX( 1, M )
220      END IF
221      IF( .NOT.LEFT .AND. .NOT.LSAME( SIDE, 'R' ) ) THEN
222         INFO = -1
223      ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) ) THEN
224         INFO = -2
225      ELSE IF( M.LT.0 ) THEN
226         INFO = -3
227      ELSE IF( N.LT.0 ) THEN
228         INFO = -4
229      ELSE IF( K.LT.0 .OR. K.GT.NQ ) THEN
230         INFO = -5
231      ELSE IF( LDA.LT.MAX( 1, NQ ) ) THEN
232         INFO = -7
233      ELSE IF( LDC.LT.MAX( 1, M ) ) THEN
234         INFO = -10
235      ELSE IF( LWORK.LT.NW .AND. .NOT.LQUERY ) THEN
236         INFO = -12
237      END IF
238*
239      IF( INFO.EQ.0 ) THEN
240*
241*        Compute the workspace requirements
242*
243         NB = MIN( NBMAX, ILAENV( 1, 'DORMQR', SIDE // TRANS, M, N, K,
244     $        -1 ) )
245         LWKOPT = NW*NB + TSIZE
246         WORK( 1 ) = LWKOPT
247      END IF
248*
249      IF( INFO.NE.0 ) THEN
250         CALL XERBLA( 'DORMQR', -INFO )
251         RETURN
252      ELSE IF( LQUERY ) THEN
253         RETURN
254      END IF
255*
256*     Quick return if possible
257*
258      IF( M.EQ.0 .OR. N.EQ.0 .OR. K.EQ.0 ) THEN
259         WORK( 1 ) = 1
260         RETURN
261      END IF
262*
263      NBMIN = 2
264      LDWORK = NW
265      IF( NB.GT.1 .AND. NB.LT.K ) THEN
266         IF( LWORK.LT.LWKOPT ) THEN
267            NB = (LWORK-TSIZE) / LDWORK
268            NBMIN = MAX( 2, ILAENV( 2, 'DORMQR', SIDE // TRANS, M, N, K,
269     $              -1 ) )
270         END IF
271      END IF
272*
273      IF( NB.LT.NBMIN .OR. NB.GE.K ) THEN
274*
275*        Use unblocked code
276*
277         CALL DORM2R( SIDE, TRANS, M, N, K, A, LDA, TAU, C, LDC, WORK,
278     $                IINFO )
279      ELSE
280*
281*        Use blocked code
282*
283         IWT = 1 + NW*NB
284         IF( ( LEFT .AND. .NOT.NOTRAN ) .OR.
285     $       ( .NOT.LEFT .AND. NOTRAN ) ) THEN
286            I1 = 1
287            I2 = K
288            I3 = NB
289         ELSE
290            I1 = ( ( K-1 ) / NB )*NB + 1
291            I2 = 1
292            I3 = -NB
293         END IF
294*
295         IF( LEFT ) THEN
296            NI = N
297            JC = 1
298         ELSE
299            MI = M
300            IC = 1
301         END IF
302*
303         DO 10 I = I1, I2, I3
304            IB = MIN( NB, K-I+1 )
305*
306*           Form the triangular factor of the block reflector
307*           H = H(i) H(i+1) . . . H(i+ib-1)
308*
309            CALL DLARFT( 'Forward', 'Columnwise', NQ-I+1, IB, A( I, I ),
310     $                   LDA, TAU( I ), WORK( IWT ), LDT )
311            IF( LEFT ) THEN
312*
313*              H or H**T is applied to C(i:m,1:n)
314*
315               MI = M - I + 1
316               IC = I
317            ELSE
318*
319*              H or H**T is applied to C(1:m,i:n)
320*
321               NI = N - I + 1
322               JC = I
323            END IF
324*
325*           Apply H or H**T
326*
327            CALL DLARFB( SIDE, TRANS, 'Forward', 'Columnwise', MI, NI,
328     $                   IB, A( I, I ), LDA, WORK( IWT ), LDT,
329     $                   C( IC, JC ), LDC, WORK, LDWORK )
330   10    CONTINUE
331      END IF
332      WORK( 1 ) = LWKOPT
333      RETURN
334*
335*     End of DORMQR
336*
337      END
338