1*> \brief \b CUNGL2 generates all or part of the unitary matrix Q from an LQ factorization determined by cgelqf (unblocked algorithm).
2*
3*  =========== DOCUMENTATION ===========
4*
5* Online html documentation available at
6*            http://www.netlib.org/lapack/explore-html/
7*
8*> \htmlonly
9*> Download CUNGL2 + dependencies
10*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cungl2.f">
11*> [TGZ]</a>
12*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cungl2.f">
13*> [ZIP]</a>
14*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cungl2.f">
15*> [TXT]</a>
16*> \endhtmlonly
17*
18*  Definition:
19*  ===========
20*
21*       SUBROUTINE CUNGL2( M, N, K, A, LDA, TAU, WORK, INFO )
22*
23*       .. Scalar Arguments ..
24*       INTEGER            INFO, K, LDA, M, N
25*       ..
26*       .. Array Arguments ..
27*       COMPLEX            A( LDA, * ), TAU( * ), WORK( * )
28*       ..
29*
30*
31*> \par Purpose:
32*  =============
33*>
34*> \verbatim
35*>
36*> CUNGL2 generates an m-by-n complex matrix Q with orthonormal rows,
37*> which is defined as the first m rows of a product of k elementary
38*> reflectors of order n
39*>
40*>       Q  =  H(k)**H . . . H(2)**H H(1)**H
41*>
42*> as returned by CGELQF.
43*> \endverbatim
44*
45*  Arguments:
46*  ==========
47*
48*> \param[in] M
49*> \verbatim
50*>          M is INTEGER
51*>          The number of rows of the matrix Q. M >= 0.
52*> \endverbatim
53*>
54*> \param[in] N
55*> \verbatim
56*>          N is INTEGER
57*>          The number of columns of the matrix Q. N >= M.
58*> \endverbatim
59*>
60*> \param[in] K
61*> \verbatim
62*>          K is INTEGER
63*>          The number of elementary reflectors whose product defines the
64*>          matrix Q. M >= K >= 0.
65*> \endverbatim
66*>
67*> \param[in,out] A
68*> \verbatim
69*>          A is COMPLEX array, dimension (LDA,N)
70*>          On entry, the i-th row must contain the vector which defines
71*>          the elementary reflector H(i), for i = 1,2,...,k, as returned
72*>          by CGELQF in the first k rows of its array argument A.
73*>          On exit, the m by n matrix Q.
74*> \endverbatim
75*>
76*> \param[in] LDA
77*> \verbatim
78*>          LDA is INTEGER
79*>          The first dimension of the array A. LDA >= max(1,M).
80*> \endverbatim
81*>
82*> \param[in] TAU
83*> \verbatim
84*>          TAU is COMPLEX array, dimension (K)
85*>          TAU(i) must contain the scalar factor of the elementary
86*>          reflector H(i), as returned by CGELQF.
87*> \endverbatim
88*>
89*> \param[out] WORK
90*> \verbatim
91*>          WORK is COMPLEX array, dimension (M)
92*> \endverbatim
93*>
94*> \param[out] INFO
95*> \verbatim
96*>          INFO is INTEGER
97*>          = 0: successful exit
98*>          < 0: if INFO = -i, the i-th argument has an illegal value
99*> \endverbatim
100*
101*  Authors:
102*  ========
103*
104*> \author Univ. of Tennessee
105*> \author Univ. of California Berkeley
106*> \author Univ. of Colorado Denver
107*> \author NAG Ltd.
108*
109*> \date September 2012
110*
111*> \ingroup complexOTHERcomputational
112*
113*  =====================================================================
114      SUBROUTINE CUNGL2( M, N, K, A, LDA, TAU, WORK, INFO )
115*
116*  -- LAPACK computational routine (version 3.4.2) --
117*  -- LAPACK is a software package provided by Univ. of Tennessee,    --
118*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
119*     September 2012
120*
121*     .. Scalar Arguments ..
122      INTEGER            INFO, K, LDA, M, N
123*     ..
124*     .. Array Arguments ..
125      COMPLEX            A( LDA, * ), TAU( * ), WORK( * )
126*     ..
127*
128*  =====================================================================
129*
130*     .. Parameters ..
131      COMPLEX            ONE, ZERO
132      PARAMETER          ( ONE = ( 1.0E+0, 0.0E+0 ),
133     $                   ZERO = ( 0.0E+0, 0.0E+0 ) )
134*     ..
135*     .. Local Scalars ..
136      INTEGER            I, J, L
137*     ..
138*     .. External Subroutines ..
139      EXTERNAL           CLACGV, CLARF, CSCAL, XERBLA
140*     ..
141*     .. Intrinsic Functions ..
142      INTRINSIC          CONJG, MAX
143*     ..
144*     .. Executable Statements ..
145*
146*     Test the input arguments
147*
148      INFO = 0
149      IF( M.LT.0 ) THEN
150         INFO = -1
151      ELSE IF( N.LT.M ) THEN
152         INFO = -2
153      ELSE IF( K.LT.0 .OR. K.GT.M ) THEN
154         INFO = -3
155      ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
156         INFO = -5
157      END IF
158      IF( INFO.NE.0 ) THEN
159         CALL XERBLA( 'CUNGL2', -INFO )
160         RETURN
161      END IF
162*
163*     Quick return if possible
164*
165      IF( M.LE.0 )
166     $   RETURN
167*
168      IF( K.LT.M ) THEN
169*
170*        Initialise rows k+1:m to rows of the unit matrix
171*
172         DO 20 J = 1, N
173            DO 10 L = K + 1, M
174               A( L, J ) = ZERO
175   10       CONTINUE
176            IF( J.GT.K .AND. J.LE.M )
177     $         A( J, J ) = ONE
178   20    CONTINUE
179      END IF
180*
181      DO 40 I = K, 1, -1
182*
183*        Apply H(i)**H to A(i:m,i:n) from the right
184*
185         IF( I.LT.N ) THEN
186            CALL CLACGV( N-I, A( I, I+1 ), LDA )
187            IF( I.LT.M ) THEN
188               A( I, I ) = ONE
189               CALL CLARF( 'Right', M-I, N-I+1, A( I, I ), LDA,
190     $                     CONJG( TAU( I ) ), A( I+1, I ), LDA, WORK )
191            END IF
192            CALL CSCAL( N-I, -TAU( I ), A( I, I+1 ), LDA )
193            CALL CLACGV( N-I, A( I, I+1 ), LDA )
194         END IF
195         A( I, I ) = ONE - CONJG( TAU( I ) )
196*
197*        Set A(i,1:i-1,i) to zero
198*
199         DO 30 L = 1, I - 1
200            A( I, L ) = ZERO
201   30    CONTINUE
202   40 CONTINUE
203      RETURN
204*
205*     End of CUNGL2
206*
207      END
208