```001:       SUBROUTINE CGEQR2( M, N, A, LDA, TAU, 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:       COMPLEX            A( LDA, * ), TAU( * ), WORK( * )
012: *     ..
013: *
014: *  Purpose
015: *  =======
016: *
017: *  CGEQR2 computes a QR factorization of a complex m by n matrix A:
018: *  A = Q * R.
019: *
020: *  Arguments
021: *  =========
022: *
023: *  M       (input) INTEGER
024: *          The number of rows of the matrix A.  M >= 0.
025: *
026: *  N       (input) INTEGER
027: *          The number of columns of the matrix A.  N >= 0.
028: *
029: *  A       (input/output) COMPLEX array, dimension (LDA,N)
030: *          On entry, the m by n matrix A.
031: *          On exit, the elements on and above the diagonal of the array
032: *          contain the min(m,n) by n upper trapezoidal matrix R (R is
033: *          upper triangular if m >= n); the elements below the diagonal,
034: *          with the array TAU, represent the unitary matrix Q as a
035: *          product of elementary reflectors (see Further Details).
036: *
037: *  LDA     (input) INTEGER
038: *          The leading dimension of the array A.  LDA >= max(1,M).
039: *
040: *  TAU     (output) COMPLEX array, dimension (min(M,N))
041: *          The scalar factors of the elementary reflectors (see Further
042: *          Details).
043: *
044: *  WORK    (workspace) COMPLEX array, dimension (N)
045: *
046: *  INFO    (output) INTEGER
047: *          = 0: successful exit
048: *          < 0: if INFO = -i, the i-th argument had an illegal value
049: *
050: *  Further Details
051: *  ===============
052: *
053: *  The matrix Q is represented as a product of elementary reflectors
054: *
055: *     Q = H(1) H(2) . . . H(k), where k = min(m,n).
056: *
057: *  Each H(i) has the form
058: *
059: *     H(i) = I - tau * v * v'
060: *
061: *  where tau is a complex scalar, and v is a complex vector with
062: *  v(1:i-1) = 0 and v(i) = 1; v(i+1:m) is stored on exit in A(i+1:m,i),
063: *  and tau in TAU(i).
064: *
065: *  =====================================================================
066: *
067: *     .. Parameters ..
068:       COMPLEX            ONE
069:       PARAMETER          ( ONE = ( 1.0E+0, 0.0E+0 ) )
070: *     ..
071: *     .. Local Scalars ..
072:       INTEGER            I, K
073:       COMPLEX            ALPHA
074: *     ..
075: *     .. External Subroutines ..
076:       EXTERNAL           CLARF, CLARFP, XERBLA
077: *     ..
078: *     .. Intrinsic Functions ..
079:       INTRINSIC          CONJG, MAX, MIN
080: *     ..
081: *     .. Executable Statements ..
082: *
083: *     Test the input arguments
084: *
085:       INFO = 0
086:       IF( M.LT.0 ) THEN
087:          INFO = -1
088:       ELSE IF( N.LT.0 ) THEN
089:          INFO = -2
090:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
091:          INFO = -4
092:       END IF
093:       IF( INFO.NE.0 ) THEN
094:          CALL XERBLA( 'CGEQR2', -INFO )
095:          RETURN
096:       END IF
097: *
098:       K = MIN( M, N )
099: *
100:       DO 10 I = 1, K
101: *
102: *        Generate elementary reflector H(i) to annihilate A(i+1:m,i)
103: *
104:          CALL CLARFP( M-I+1, A( I, I ), A( MIN( I+1, M ), I ), 1,
105:      \$                TAU( I ) )
106:          IF( I.LT.N ) THEN
107: *
108: *           Apply H(i)' to A(i:m,i+1:n) from the left
109: *
110:             ALPHA = A( I, I )
111:             A( I, I ) = ONE
112:             CALL CLARF( 'Left', M-I+1, N-I, A( I, I ), 1,
113:      \$                  CONJG( TAU( I ) ), A( I, I+1 ), LDA, WORK )
114:             A( I, I ) = ALPHA
115:          END IF
116:    10 CONTINUE
117:       RETURN
118: *
119: *     End of CGEQR2
120: *
121:       END
122: ```