```001:       SUBROUTINE SORG2R( 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: *  SORG2R generates an m by n real matrix Q with orthonormal columns,
018: *  which is defined as the first n columns of a product of k elementary
019: *  reflectors of order m
020: *
021: *        Q  =  H(1) H(2) . . . H(k)
022: *
023: *  as returned by SGEQRF.
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. M >= N >= 0.
033: *
034: *  K       (input) INTEGER
035: *          The number of elementary reflectors whose product defines the
036: *          matrix Q. N >= K >= 0.
037: *
038: *  A       (input/output) REAL array, dimension (LDA,N)
039: *          On entry, the i-th column must contain the vector which
040: *          defines the elementary reflector H(i), for i = 1,2,...,k, as
041: *          returned by SGEQRF in the first k columns of its array
042: *          argument 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 SGEQRF.
051: *
052: *  WORK    (workspace) REAL array, dimension (N)
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, 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.0 .OR. N.GT.M ) THEN
081:          INFO = -2
082:       ELSE IF( K.LT.0 .OR. K.GT.N ) 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( 'SORG2R', -INFO )
089:          RETURN
090:       END IF
091: *
092: *     Quick return if possible
093: *
094:       IF( N.LE.0 )
095:      \$   RETURN
096: *
097: *     Initialise columns k+1:n to columns of the unit matrix
098: *
099:       DO 20 J = K + 1, N
100:          DO 10 L = 1, M
101:             A( L, J ) = ZERO
102:    10    CONTINUE
103:          A( J, J ) = ONE
104:    20 CONTINUE
105: *
106:       DO 40 I = K, 1, -1
107: *
108: *        Apply H(i) to A(i:m,i:n) from the left
109: *
110:          IF( I.LT.N ) THEN
111:             A( I, I ) = ONE
112:             CALL SLARF( 'Left', M-I+1, N-I, A( I, I ), 1, TAU( I ),
113:      \$                  A( I, I+1 ), LDA, WORK )
114:          END IF
115:          IF( I.LT.M )
116:      \$      CALL SSCAL( M-I, -TAU( I ), A( I+1, I ), 1 )
117:          A( I, I ) = ONE - TAU( I )
118: *
119: *        Set A(1:i-1,i) to zero
120: *
121:          DO 30 L = 1, I - 1
122:             A( L, I ) = ZERO
123:    30    CONTINUE
124:    40 CONTINUE
125:       RETURN
126: *
127: *     End of SORG2R
128: *
129:       END
130: ```