```001:       SUBROUTINE SORGR2( M, N, K, 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, K, LDA, M, N
009: *     ..
010: *     .. Array Arguments ..
011:       REAL               A( LDA, * ), TAU( * ), WORK( * )
012: *     ..
013: *
014: *  Purpose
015: *  =======
016: *
017: *  SORGR2 generates an m by n real matrix Q with orthonormal rows,
018: *  which is defined as the last m rows of a product of k elementary
019: *  reflectors of order n
020: *
021: *        Q  =  H(1) H(2) . . . H(k)
022: *
023: *  as returned by SGERQF.
024: *
025: *  Arguments
026: *  =========
027: *
028: *  M       (input) INTEGER
029: *          The number of rows of the matrix Q. M >= 0.
030: *
031: *  N       (input) INTEGER
032: *          The number of columns of the matrix Q. N >= M.
033: *
034: *  K       (input) INTEGER
035: *          The number of elementary reflectors whose product defines the
036: *          matrix Q. M >= K >= 0.
037: *
038: *  A       (input/output) REAL array, dimension (LDA,N)
039: *          On entry, the (m-k+i)-th row must contain the vector which
040: *          defines the elementary reflector H(i), for i = 1,2,...,k, as
041: *          returned by SGERQF in the last k rows of its array argument
042: *          A.
043: *          On exit, the m by n matrix Q.
044: *
045: *  LDA     (input) INTEGER
046: *          The first dimension of the array A. LDA >= max(1,M).
047: *
048: *  TAU     (input) REAL array, dimension (K)
049: *          TAU(i) must contain the scalar factor of the elementary
050: *          reflector H(i), as returned by SGERQF.
051: *
052: *  WORK    (workspace) REAL array, dimension (M)
053: *
054: *  INFO    (output) INTEGER
055: *          = 0: successful exit
056: *          < 0: if INFO = -i, the i-th argument has an illegal value
057: *
058: *  =====================================================================
059: *
060: *     .. Parameters ..
061:       REAL               ONE, ZERO
062:       PARAMETER          ( ONE = 1.0E+0, ZERO = 0.0E+0 )
063: *     ..
064: *     .. Local Scalars ..
065:       INTEGER            I, II, J, L
066: *     ..
067: *     .. External Subroutines ..
068:       EXTERNAL           SLARF, SSCAL, XERBLA
069: *     ..
070: *     .. Intrinsic Functions ..
071:       INTRINSIC          MAX
072: *     ..
073: *     .. Executable Statements ..
074: *
075: *     Test the input arguments
076: *
077:       INFO = 0
078:       IF( M.LT.0 ) THEN
079:          INFO = -1
080:       ELSE IF( N.LT.M ) THEN
081:          INFO = -2
082:       ELSE IF( K.LT.0 .OR. K.GT.M ) THEN
083:          INFO = -3
084:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
085:          INFO = -5
086:       END IF
087:       IF( INFO.NE.0 ) THEN
088:          CALL XERBLA( 'SORGR2', -INFO )
089:          RETURN
090:       END IF
091: *
092: *     Quick return if possible
093: *
094:       IF( M.LE.0 )
095:      \$   RETURN
096: *
097:       IF( K.LT.M ) THEN
098: *
099: *        Initialise rows 1:m-k to rows of the unit matrix
100: *
101:          DO 20 J = 1, N
102:             DO 10 L = 1, M - K
103:                A( L, J ) = ZERO
104:    10       CONTINUE
105:             IF( J.GT.N-M .AND. J.LE.N-K )
106:      \$         A( M-N+J, J ) = ONE
107:    20    CONTINUE
108:       END IF
109: *
110:       DO 40 I = 1, K
111:          II = M - K + I
112: *
113: *        Apply H(i) to A(1:m-k+i,1:n-k+i) from the right
114: *
115:          A( II, N-M+II ) = ONE
116:          CALL SLARF( 'Right', II-1, N-M+II, A( II, 1 ), LDA, TAU( I ),
117:      \$               A, LDA, WORK )
118:          CALL SSCAL( N-M+II-1, -TAU( I ), A( II, 1 ), LDA )
119:          A( II, N-M+II ) = ONE - TAU( I )
120: *
121: *        Set A(m-k+i,n-k+i+1:n) to zero
122: *
123:          DO 30 L = N - M + II + 1, N
124:             A( II, L ) = ZERO
125:    30    CONTINUE
126:    40 CONTINUE
127:       RETURN
128: *
129: *     End of SORGR2
130: *
131:       END
132: ```