LAPACK 3.3.1
Linear Algebra PACKage

spstf2.f

Go to the documentation of this file.
00001       SUBROUTINE SPSTF2( UPLO, N, A, LDA, PIV, RANK, TOL, WORK, INFO )
00002 *
00003 *  -- LAPACK PROTOTYPE routine (version 3.2.2) --
00004 *     Craig Lucas, University of Manchester / NAG Ltd.
00005 *     October, 2008
00006 *
00007 *     .. Scalar Arguments ..
00008       REAL               TOL
00009       INTEGER            INFO, LDA, N, RANK
00010       CHARACTER          UPLO
00011 *     ..
00012 *     .. Array Arguments ..
00013       REAL               A( LDA, * ), WORK( 2*N )
00014       INTEGER            PIV( N )
00015 *     ..
00016 *
00017 *  Purpose
00018 *  =======
00019 *
00020 *  SPSTF2 computes the Cholesky factorization with complete
00021 *  pivoting of a real symmetric positive semidefinite matrix A.
00022 *
00023 *  The factorization has the form
00024 *     P**T * A * P = U**T * U ,  if UPLO = 'U',
00025 *     P**T * A * P = L  * L**T,  if UPLO = 'L',
00026 *  where U is an upper triangular matrix and L is lower triangular, and
00027 *  P is stored as vector PIV.
00028 *
00029 *  This algorithm does not attempt to check that A is positive
00030 *  semidefinite. This version of the algorithm calls level 2 BLAS.
00031 *
00032 *  Arguments
00033 *  =========
00034 *
00035 *  UPLO    (input) CHARACTER*1
00036 *          Specifies whether the upper or lower triangular part of the
00037 *          symmetric matrix A is stored.
00038 *          = 'U':  Upper triangular
00039 *          = 'L':  Lower triangular
00040 *
00041 *  N       (input) INTEGER
00042 *          The order of the matrix A.  N >= 0.
00043 *
00044 *  A       (input/output) REAL array, dimension (LDA,N)
00045 *          On entry, the symmetric matrix A.  If UPLO = 'U', the leading
00046 *          n by n upper triangular part of A contains the upper
00047 *          triangular part of the matrix A, and the strictly lower
00048 *          triangular part of A is not referenced.  If UPLO = 'L', the
00049 *          leading n by n lower triangular part of A contains the lower
00050 *          triangular part of the matrix A, and the strictly upper
00051 *          triangular part of A is not referenced.
00052 *
00053 *          On exit, if INFO = 0, the factor U or L from the Cholesky
00054 *          factorization as above.
00055 *
00056 *  PIV     (output) INTEGER array, dimension (N)
00057 *          PIV is such that the nonzero entries are P( PIV(K), K ) = 1.
00058 *
00059 *  RANK    (output) INTEGER
00060 *          The rank of A given by the number of steps the algorithm
00061 *          completed.
00062 *
00063 *  TOL     (input) REAL
00064 *          User defined tolerance. If TOL < 0, then N*U*MAX( A( K,K ) )
00065 *          will be used. The algorithm terminates at the (K-1)st step
00066 *          if the pivot <= TOL.
00067 *
00068 *  LDA     (input) INTEGER
00069 *          The leading dimension of the array A.  LDA >= max(1,N).
00070 *
00071 *  WORK    (workspace) REAL array, dimension (2*N)
00072 *          Work space.
00073 *
00074 *  INFO    (output) INTEGER
00075 *          < 0: If INFO = -K, the K-th argument had an illegal value,
00076 *          = 0: algorithm completed successfully, and
00077 *          > 0: the matrix A is either rank deficient with computed rank
00078 *               as returned in RANK, or is indefinite.  See Section 7 of
00079 *               LAPACK Working Note #161 for further information.
00080 *
00081 *  =====================================================================
00082 *
00083 *     .. Parameters ..
00084       REAL               ONE, ZERO
00085       PARAMETER          ( ONE = 1.0E+0, ZERO = 0.0E+0 )
00086 *     ..
00087 *     .. Local Scalars ..
00088       REAL               AJJ, SSTOP, STEMP
00089       INTEGER            I, ITEMP, J, PVT
00090       LOGICAL            UPPER
00091 *     ..
00092 *     .. External Functions ..
00093       REAL               SLAMCH
00094       LOGICAL            LSAME, SISNAN
00095       EXTERNAL           SLAMCH, LSAME, SISNAN
00096 *     ..
00097 *     .. External Subroutines ..
00098       EXTERNAL           SGEMV, SSCAL, SSWAP, XERBLA
00099 *     ..
00100 *     .. Intrinsic Functions ..
00101       INTRINSIC          MAX, SQRT, MAXLOC
00102 *     ..
00103 *     .. Executable Statements ..
00104 *
00105 *     Test the input parameters
00106 *
00107       INFO = 0
00108       UPPER = LSAME( UPLO, 'U' )
00109       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
00110          INFO = -1
00111       ELSE IF( N.LT.0 ) THEN
00112          INFO = -2
00113       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
00114          INFO = -4
00115       END IF
00116       IF( INFO.NE.0 ) THEN
00117          CALL XERBLA( 'SPSTF2', -INFO )
00118          RETURN
00119       END IF
00120 *
00121 *     Quick return if possible
00122 *
00123       IF( N.EQ.0 )
00124      $   RETURN
00125 *
00126 *     Initialize PIV
00127 *
00128       DO 100 I = 1, N
00129          PIV( I ) = I
00130   100 CONTINUE
00131 *
00132 *     Compute stopping value
00133 *
00134       PVT = 1
00135       AJJ = A( PVT, PVT )
00136       DO I = 2, N
00137          IF( A( I, I ).GT.AJJ ) THEN
00138             PVT = I
00139             AJJ = A( PVT, PVT )
00140          END IF
00141       END DO
00142       IF( AJJ.EQ.ZERO.OR.SISNAN( AJJ ) ) THEN
00143          RANK = 0
00144          INFO = 1
00145          GO TO 170
00146       END IF
00147 *
00148 *     Compute stopping value if not supplied
00149 *
00150       IF( TOL.LT.ZERO ) THEN
00151          SSTOP = N * SLAMCH( 'Epsilon' ) * AJJ
00152       ELSE
00153          SSTOP = TOL
00154       END IF
00155 *
00156 *     Set first half of WORK to zero, holds dot products
00157 *
00158       DO 110 I = 1, N
00159          WORK( I ) = 0
00160   110 CONTINUE
00161 *
00162       IF( UPPER ) THEN
00163 *
00164 *        Compute the Cholesky factorization P**T * A * P = U**T * U
00165 *
00166          DO 130 J = 1, N
00167 *
00168 *        Find pivot, test for exit, else swap rows and columns
00169 *        Update dot products, compute possible pivots which are
00170 *        stored in the second half of WORK
00171 *
00172             DO 120 I = J, N
00173 *
00174                IF( J.GT.1 ) THEN
00175                   WORK( I ) = WORK( I ) + A( J-1, I )**2
00176                END IF
00177                WORK( N+I ) = A( I, I ) - WORK( I )
00178 *
00179   120       CONTINUE
00180 *
00181             IF( J.GT.1 ) THEN
00182                ITEMP = MAXLOC( WORK( (N+J):(2*N) ), 1 )
00183                PVT = ITEMP + J - 1
00184                AJJ = WORK( N+PVT )
00185                IF( AJJ.LE.SSTOP.OR.SISNAN( AJJ ) ) THEN
00186                   A( J, J ) = AJJ
00187                   GO TO 160
00188                END IF
00189             END IF
00190 *
00191             IF( J.NE.PVT ) THEN
00192 *
00193 *              Pivot OK, so can now swap pivot rows and columns
00194 *
00195                A( PVT, PVT ) = A( J, J )
00196                CALL SSWAP( J-1, A( 1, J ), 1, A( 1, PVT ), 1 )
00197                IF( PVT.LT.N )
00198      $            CALL SSWAP( N-PVT, A( J, PVT+1 ), LDA,
00199      $                        A( PVT, PVT+1 ), LDA )
00200                CALL SSWAP( PVT-J-1, A( J, J+1 ), LDA, A( J+1, PVT ), 1 )
00201 *
00202 *              Swap dot products and PIV
00203 *
00204                STEMP = WORK( J )
00205                WORK( J ) = WORK( PVT )
00206                WORK( PVT ) = STEMP
00207                ITEMP = PIV( PVT )
00208                PIV( PVT ) = PIV( J )
00209                PIV( J ) = ITEMP
00210             END IF
00211 *
00212             AJJ = SQRT( AJJ )
00213             A( J, J ) = AJJ
00214 *
00215 *           Compute elements J+1:N of row J
00216 *
00217             IF( J.LT.N ) THEN
00218                CALL SGEMV( 'Trans', J-1, N-J, -ONE, A( 1, J+1 ), LDA,
00219      $                     A( 1, J ), 1, ONE, A( J, J+1 ), LDA )
00220                CALL SSCAL( N-J, ONE / AJJ, A( J, J+1 ), LDA )
00221             END IF
00222 *
00223   130    CONTINUE
00224 *
00225       ELSE
00226 *
00227 *        Compute the Cholesky factorization P**T * A * P = L * L**T
00228 *
00229          DO 150 J = 1, N
00230 *
00231 *        Find pivot, test for exit, else swap rows and columns
00232 *        Update dot products, compute possible pivots which are
00233 *        stored in the second half of WORK
00234 *
00235             DO 140 I = J, N
00236 *
00237                IF( J.GT.1 ) THEN
00238                   WORK( I ) = WORK( I ) + A( I, J-1 )**2
00239                END IF
00240                WORK( N+I ) = A( I, I ) - WORK( I )
00241 *
00242   140       CONTINUE
00243 *
00244             IF( J.GT.1 ) THEN
00245                ITEMP = MAXLOC( WORK( (N+J):(2*N) ), 1 )
00246                PVT = ITEMP + J - 1
00247                AJJ = WORK( N+PVT )
00248                IF( AJJ.LE.SSTOP.OR.SISNAN( AJJ ) ) THEN
00249                   A( J, J ) = AJJ
00250                   GO TO 160
00251                END IF
00252             END IF
00253 *
00254             IF( J.NE.PVT ) THEN
00255 *
00256 *              Pivot OK, so can now swap pivot rows and columns
00257 *
00258                A( PVT, PVT ) = A( J, J )
00259                CALL SSWAP( J-1, A( J, 1 ), LDA, A( PVT, 1 ), LDA )
00260                IF( PVT.LT.N )
00261      $            CALL SSWAP( N-PVT, A( PVT+1, J ), 1, A( PVT+1, PVT ),
00262      $                        1 )
00263                CALL SSWAP( PVT-J-1, A( J+1, J ), 1, A( PVT, J+1 ), LDA )
00264 *
00265 *              Swap dot products and PIV
00266 *
00267                STEMP = WORK( J )
00268                WORK( J ) = WORK( PVT )
00269                WORK( PVT ) = STEMP
00270                ITEMP = PIV( PVT )
00271                PIV( PVT ) = PIV( J )
00272                PIV( J ) = ITEMP
00273             END IF
00274 *
00275             AJJ = SQRT( AJJ )
00276             A( J, J ) = AJJ
00277 *
00278 *           Compute elements J+1:N of column J
00279 *
00280             IF( J.LT.N ) THEN
00281                CALL SGEMV( 'No Trans', N-J, J-1, -ONE, A( J+1, 1 ), LDA,
00282      $                     A( J, 1 ), LDA, ONE, A( J+1, J ), 1 )
00283                CALL SSCAL( N-J, ONE / AJJ, A( J+1, J ), 1 )
00284             END IF
00285 *
00286   150    CONTINUE
00287 *
00288       END IF
00289 *
00290 *     Ran to completion, A has full rank
00291 *
00292       RANK = N
00293 *
00294       GO TO 170
00295   160 CONTINUE
00296 *
00297 *     Rank is number of steps completed.  Set INFO = 1 to signal
00298 *     that the factorization cannot be used to solve a system.
00299 *
00300       RANK = J - 1
00301       INFO = 1
00302 *
00303   170 CONTINUE
00304       RETURN
00305 *
00306 *     End of SPSTF2
00307 *
00308       END
 All Files Functions