LAPACK 3.3.1 Linear Algebra PACKage

# sorgqr.f

Go to the documentation of this file.
```00001       SUBROUTINE SORGQR( M, N, K, A, LDA, TAU, WORK, LWORK, INFO )
00002 *
00003 *  -- LAPACK routine (version 3.2) --
00004 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
00005 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
00006 *     November 2006
00007 *
00008 *     .. Scalar Arguments ..
00009       INTEGER            INFO, K, LDA, LWORK, M, N
00010 *     ..
00011 *     .. Array Arguments ..
00012       REAL               A( LDA, * ), TAU( * ), WORK( * )
00013 *     ..
00014 *
00015 *  Purpose
00016 *  =======
00017 *
00018 *  SORGQR generates an M-by-N real matrix Q with orthonormal columns,
00019 *  which is defined as the first N columns of a product of K elementary
00020 *  reflectors of order M
00021 *
00022 *        Q  =  H(1) H(2) . . . H(k)
00023 *
00024 *  as returned by SGEQRF.
00025 *
00026 *  Arguments
00027 *  =========
00028 *
00029 *  M       (input) INTEGER
00030 *          The number of rows of the matrix Q. M >= 0.
00031 *
00032 *  N       (input) INTEGER
00033 *          The number of columns of the matrix Q. M >= N >= 0.
00034 *
00035 *  K       (input) INTEGER
00036 *          The number of elementary reflectors whose product defines the
00037 *          matrix Q. N >= K >= 0.
00038 *
00039 *  A       (input/output) REAL array, dimension (LDA,N)
00040 *          On entry, the i-th column must contain the vector which
00041 *          defines the elementary reflector H(i), for i = 1,2,...,k, as
00042 *          returned by SGEQRF in the first k columns of its array
00043 *          argument A.
00044 *          On exit, the M-by-N matrix Q.
00045 *
00046 *  LDA     (input) INTEGER
00047 *          The first dimension of the array A. LDA >= max(1,M).
00048 *
00049 *  TAU     (input) REAL array, dimension (K)
00050 *          TAU(i) must contain the scalar factor of the elementary
00051 *          reflector H(i), as returned by SGEQRF.
00052 *
00053 *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
00054 *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
00055 *
00056 *  LWORK   (input) INTEGER
00057 *          The dimension of the array WORK. LWORK >= max(1,N).
00058 *          For optimum performance LWORK >= N*NB, where NB is the
00059 *          optimal blocksize.
00060 *
00061 *          If LWORK = -1, then a workspace query is assumed; the routine
00062 *          only calculates the optimal size of the WORK array, returns
00063 *          this value as the first entry of the WORK array, and no error
00064 *          message related to LWORK is issued by XERBLA.
00065 *
00066 *  INFO    (output) INTEGER
00067 *          = 0:  successful exit
00068 *          < 0:  if INFO = -i, the i-th argument has an illegal value
00069 *
00070 *  =====================================================================
00071 *
00072 *     .. Parameters ..
00073       REAL               ZERO
00074       PARAMETER          ( ZERO = 0.0E+0 )
00075 *     ..
00076 *     .. Local Scalars ..
00077       LOGICAL            LQUERY
00078       INTEGER            I, IB, IINFO, IWS, J, KI, KK, L, LDWORK,
00079      \$                   LWKOPT, NB, NBMIN, NX
00080 *     ..
00081 *     .. External Subroutines ..
00082       EXTERNAL           SLARFB, SLARFT, SORG2R, XERBLA
00083 *     ..
00084 *     .. Intrinsic Functions ..
00085       INTRINSIC          MAX, MIN
00086 *     ..
00087 *     .. External Functions ..
00088       INTEGER            ILAENV
00089       EXTERNAL           ILAENV
00090 *     ..
00091 *     .. Executable Statements ..
00092 *
00093 *     Test the input arguments
00094 *
00095       INFO = 0
00096       NB = ILAENV( 1, 'SORGQR', ' ', M, N, K, -1 )
00097       LWKOPT = MAX( 1, N )*NB
00098       WORK( 1 ) = LWKOPT
00099       LQUERY = ( LWORK.EQ.-1 )
00100       IF( M.LT.0 ) THEN
00101          INFO = -1
00102       ELSE IF( N.LT.0 .OR. N.GT.M ) THEN
00103          INFO = -2
00104       ELSE IF( K.LT.0 .OR. K.GT.N ) THEN
00105          INFO = -3
00106       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
00107          INFO = -5
00108       ELSE IF( LWORK.LT.MAX( 1, N ) .AND. .NOT.LQUERY ) THEN
00109          INFO = -8
00110       END IF
00111       IF( INFO.NE.0 ) THEN
00112          CALL XERBLA( 'SORGQR', -INFO )
00113          RETURN
00114       ELSE IF( LQUERY ) THEN
00115          RETURN
00116       END IF
00117 *
00118 *     Quick return if possible
00119 *
00120       IF( N.LE.0 ) THEN
00121          WORK( 1 ) = 1
00122          RETURN
00123       END IF
00124 *
00125       NBMIN = 2
00126       NX = 0
00127       IWS = N
00128       IF( NB.GT.1 .AND. NB.LT.K ) THEN
00129 *
00130 *        Determine when to cross over from blocked to unblocked code.
00131 *
00132          NX = MAX( 0, ILAENV( 3, 'SORGQR', ' ', M, N, K, -1 ) )
00133          IF( NX.LT.K ) THEN
00134 *
00135 *           Determine if workspace is large enough for blocked code.
00136 *
00137             LDWORK = N
00138             IWS = LDWORK*NB
00139             IF( LWORK.LT.IWS ) THEN
00140 *
00141 *              Not enough workspace to use optimal NB:  reduce NB and
00142 *              determine the minimum value of NB.
00143 *
00144                NB = LWORK / LDWORK
00145                NBMIN = MAX( 2, ILAENV( 2, 'SORGQR', ' ', M, N, K, -1 ) )
00146             END IF
00147          END IF
00148       END IF
00149 *
00150       IF( NB.GE.NBMIN .AND. NB.LT.K .AND. NX.LT.K ) THEN
00151 *
00152 *        Use blocked code after the last block.
00153 *        The first kk columns are handled by the block method.
00154 *
00155          KI = ( ( K-NX-1 ) / NB )*NB
00156          KK = MIN( K, KI+NB )
00157 *
00158 *        Set A(1:kk,kk+1:n) to zero.
00159 *
00160          DO 20 J = KK + 1, N
00161             DO 10 I = 1, KK
00162                A( I, J ) = ZERO
00163    10       CONTINUE
00164    20    CONTINUE
00165       ELSE
00166          KK = 0
00167       END IF
00168 *
00169 *     Use unblocked code for the last or only block.
00170 *
00171       IF( KK.LT.N )
00172      \$   CALL SORG2R( M-KK, N-KK, K-KK, A( KK+1, KK+1 ), LDA,
00173      \$                TAU( KK+1 ), WORK, IINFO )
00174 *
00175       IF( KK.GT.0 ) THEN
00176 *
00177 *        Use blocked code
00178 *
00179          DO 50 I = KI + 1, 1, -NB
00180             IB = MIN( NB, K-I+1 )
00181             IF( I+IB.LE.N ) THEN
00182 *
00183 *              Form the triangular factor of the block reflector
00184 *              H = H(i) H(i+1) . . . H(i+ib-1)
00185 *
00186                CALL SLARFT( 'Forward', 'Columnwise', M-I+1, IB,
00187      \$                      A( I, I ), LDA, TAU( I ), WORK, LDWORK )
00188 *
00189 *              Apply H to A(i:m,i+ib:n) from the left
00190 *
00191                CALL SLARFB( 'Left', 'No transpose', 'Forward',
00192      \$                      'Columnwise', M-I+1, N-I-IB+1, IB,
00193      \$                      A( I, I ), LDA, WORK, LDWORK, A( I, I+IB ),
00194      \$                      LDA, WORK( IB+1 ), LDWORK )
00195             END IF
00196 *
00197 *           Apply H to rows i:m of current block
00198 *
00199             CALL SORG2R( M-I+1, IB, IB, A( I, I ), LDA, TAU( I ), WORK,
00200      \$                   IINFO )
00201 *
00202 *           Set rows 1:i-1 of current block to zero
00203 *
00204             DO 40 J = I, I + IB - 1
00205                DO 30 L = 1, I - 1
00206                   A( L, J ) = ZERO
00207    30          CONTINUE
00208    40       CONTINUE
00209    50    CONTINUE
00210       END IF
00211 *
00212       WORK( 1 ) = IWS
00213       RETURN
00214 *
00215 *     End of SORGQR
00216 *
00217       END
```