```001:       SUBROUTINE DGELQF( M, N, 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:       INTEGER            INFO, LDA, LWORK, M, N
009: *     ..
010: *     .. Array Arguments ..
011:       DOUBLE PRECISION   A( LDA, * ), TAU( * ), WORK( * )
012: *     ..
013: *
014: *  Purpose
015: *  =======
016: *
017: *  DGELQF computes an LQ factorization of a real M-by-N matrix A:
018: *  A = L * Q.
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) DOUBLE PRECISION array, dimension (LDA,N)
030: *          On entry, the M-by-N matrix A.
031: *          On exit, the elements on and below the diagonal of the array
032: *          contain the m-by-min(m,n) lower trapezoidal matrix L (L is
033: *          lower triangular if m <= n); the elements above the diagonal,
034: *          with the array TAU, represent the orthogonal 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) DOUBLE PRECISION array, dimension (min(M,N))
041: *          The scalar factors of the elementary reflectors (see Further
042: *          Details).
043: *
044: *  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK))
045: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
046: *
047: *  LWORK   (input) INTEGER
048: *          The dimension of the array WORK.  LWORK >= max(1,M).
049: *          For optimum performance LWORK >= M*NB, where NB is the
050: *          optimal blocksize.
051: *
052: *          If LWORK = -1, then a workspace query is assumed; the routine
053: *          only calculates the optimal size of the WORK array, returns
054: *          this value as the first entry of the WORK array, and no error
055: *          message related to LWORK is issued by XERBLA.
056: *
057: *  INFO    (output) INTEGER
058: *          = 0:  successful exit
059: *          < 0:  if INFO = -i, the i-th argument had an illegal value
060: *
061: *  Further Details
062: *  ===============
063: *
064: *  The matrix Q is represented as a product of elementary reflectors
065: *
066: *     Q = H(k) . . . H(2) H(1), where k = min(m,n).
067: *
068: *  Each H(i) has the form
069: *
070: *     H(i) = I - tau * v * v'
071: *
072: *  where tau is a real scalar, and v is a real vector with
073: *  v(1:i-1) = 0 and v(i) = 1; v(i+1:n) is stored on exit in A(i,i+1:n),
074: *  and tau in TAU(i).
075: *
076: *  =====================================================================
077: *
078: *     .. Local Scalars ..
079:       LOGICAL            LQUERY
080:       INTEGER            I, IB, IINFO, IWS, K, LDWORK, LWKOPT, NB,
081:      \$                   NBMIN, NX
082: *     ..
083: *     .. External Subroutines ..
084:       EXTERNAL           DGELQ2, DLARFB, DLARFT, XERBLA
085: *     ..
086: *     .. Intrinsic Functions ..
087:       INTRINSIC          MAX, MIN
088: *     ..
089: *     .. External Functions ..
090:       INTEGER            ILAENV
091:       EXTERNAL           ILAENV
092: *     ..
093: *     .. Executable Statements ..
094: *
095: *     Test the input arguments
096: *
097:       INFO = 0
098:       NB = ILAENV( 1, 'DGELQF', ' ', M, N, -1, -1 )
099:       LWKOPT = M*NB
100:       WORK( 1 ) = LWKOPT
101:       LQUERY = ( LWORK.EQ.-1 )
102:       IF( M.LT.0 ) THEN
103:          INFO = -1
104:       ELSE IF( N.LT.0 ) THEN
105:          INFO = -2
106:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
107:          INFO = -4
108:       ELSE IF( LWORK.LT.MAX( 1, M ) .AND. .NOT.LQUERY ) THEN
109:          INFO = -7
110:       END IF
111:       IF( INFO.NE.0 ) THEN
112:          CALL XERBLA( 'DGELQF', -INFO )
113:          RETURN
114:       ELSE IF( LQUERY ) THEN
115:          RETURN
116:       END IF
117: *
118: *     Quick return if possible
119: *
120:       K = MIN( M, N )
121:       IF( K.EQ.0 ) THEN
122:          WORK( 1 ) = 1
123:          RETURN
124:       END IF
125: *
126:       NBMIN = 2
127:       NX = 0
128:       IWS = M
129:       IF( NB.GT.1 .AND. NB.LT.K ) THEN
130: *
131: *        Determine when to cross over from blocked to unblocked code.
132: *
133:          NX = MAX( 0, ILAENV( 3, 'DGELQF', ' ', M, N, -1, -1 ) )
134:          IF( NX.LT.K ) THEN
135: *
136: *           Determine if workspace is large enough for blocked code.
137: *
138:             LDWORK = M
139:             IWS = LDWORK*NB
140:             IF( LWORK.LT.IWS ) THEN
141: *
142: *              Not enough workspace to use optimal NB:  reduce NB and
143: *              determine the minimum value of NB.
144: *
145:                NB = LWORK / LDWORK
146:                NBMIN = MAX( 2, ILAENV( 2, 'DGELQF', ' ', M, N, -1,
147:      \$                 -1 ) )
148:             END IF
149:          END IF
150:       END IF
151: *
152:       IF( NB.GE.NBMIN .AND. NB.LT.K .AND. NX.LT.K ) THEN
153: *
154: *        Use blocked code initially
155: *
156:          DO 10 I = 1, K - NX, NB
157:             IB = MIN( K-I+1, NB )
158: *
159: *           Compute the LQ factorization of the current block
160: *           A(i:i+ib-1,i:n)
161: *
162:             CALL DGELQ2( IB, N-I+1, A( I, I ), LDA, TAU( I ), WORK,
163:      \$                   IINFO )
164:             IF( I+IB.LE.M ) THEN
165: *
166: *              Form the triangular factor of the block reflector
167: *              H = H(i) H(i+1) . . . H(i+ib-1)
168: *
169:                CALL DLARFT( 'Forward', 'Rowwise', N-I+1, IB, A( I, I ),
170:      \$                      LDA, TAU( I ), WORK, LDWORK )
171: *
172: *              Apply H to A(i+ib:m,i:n) from the right
173: *
174:                CALL DLARFB( 'Right', 'No transpose', 'Forward',
175:      \$                      'Rowwise', M-I-IB+1, N-I+1, IB, A( I, I ),
176:      \$                      LDA, WORK, LDWORK, A( I+IB, I ), LDA,
177:      \$                      WORK( IB+1 ), LDWORK )
178:             END IF
179:    10    CONTINUE
180:       ELSE
181:          I = 1
182:       END IF
183: *
184: *     Use unblocked code to factor the last or only block.
185: *
186:       IF( I.LE.K )
187:      \$   CALL DGELQ2( M-I+1, N-I+1, A( I, I ), LDA, TAU( I ), WORK,
188:      \$                IINFO )
189: *
190:       WORK( 1 ) = IWS
191:       RETURN
192: *
193: *     End of DGELQF
194: *
195:       END
196: ```