LAPACK 3.3.0

ssptrf.f

Go to the documentation of this file.
00001       SUBROUTINE SSPTRF( UPLO, N, AP, IPIV, 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       CHARACTER          UPLO
00010       INTEGER            INFO, N
00011 *     ..
00012 *     .. Array Arguments ..
00013       INTEGER            IPIV( * )
00014       REAL               AP( * )
00015 *     ..
00016 *
00017 *  Purpose
00018 *  =======
00019 *
00020 *  SSPTRF computes the factorization of a real symmetric matrix A stored
00021 *  in packed format using the Bunch-Kaufman diagonal pivoting method:
00022 *
00023 *     A = U*D*U**T  or  A = L*D*L**T
00024 *
00025 *  where U (or L) is a product of permutation and unit upper (lower)
00026 *  triangular matrices, and D is symmetric and block diagonal with
00027 *  1-by-1 and 2-by-2 diagonal blocks.
00028 *
00029 *  Arguments
00030 *  =========
00031 *
00032 *  UPLO    (input) CHARACTER*1
00033 *          = 'U':  Upper triangle of A is stored;
00034 *          = 'L':  Lower triangle of A is stored.
00035 *
00036 *  N       (input) INTEGER
00037 *          The order of the matrix A.  N >= 0.
00038 *
00039 *  AP      (input/output) REAL array, dimension (N*(N+1)/2)
00040 *          On entry, the upper or lower triangle of the symmetric matrix
00041 *          A, packed columnwise in a linear array.  The j-th column of A
00042 *          is stored in the array AP as follows:
00043 *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
00044 *          if UPLO = 'L', AP(i + (j-1)*(2n-j)/2) = A(i,j) for j<=i<=n.
00045 *
00046 *          On exit, the block diagonal matrix D and the multipliers used
00047 *          to obtain the factor U or L, stored as a packed triangular
00048 *          matrix overwriting A (see below for further details).
00049 *
00050 *  IPIV    (output) INTEGER array, dimension (N)
00051 *          Details of the interchanges and the block structure of D.
00052 *          If IPIV(k) > 0, then rows and columns k and IPIV(k) were
00053 *          interchanged and D(k,k) is a 1-by-1 diagonal block.
00054 *          If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and
00055 *          columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k)
00056 *          is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) =
00057 *          IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were
00058 *          interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block.
00059 *
00060 *  INFO    (output) INTEGER
00061 *          = 0: successful exit
00062 *          < 0: if INFO = -i, the i-th argument had an illegal value
00063 *          > 0: if INFO = i, D(i,i) is exactly zero.  The factorization
00064 *               has been completed, but the block diagonal matrix D is
00065 *               exactly singular, and division by zero will occur if it
00066 *               is used to solve a system of equations.
00067 *
00068 *  Further Details
00069 *  ===============
00070 *
00071 *  5-96 - Based on modifications by J. Lewis, Boeing Computer Services
00072 *         Company
00073 *
00074 *  If UPLO = 'U', then A = U*D*U', where
00075 *     U = P(n)*U(n)* ... *P(k)U(k)* ...,
00076 *  i.e., U is a product of terms P(k)*U(k), where k decreases from n to
00077 *  1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1
00078 *  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as
00079 *  defined by IPIV(k), and U(k) is a unit upper triangular matrix, such
00080 *  that if the diagonal block D(k) is of order s (s = 1 or 2), then
00081 *
00082 *             (   I    v    0   )   k-s
00083 *     U(k) =  (   0    I    0   )   s
00084 *             (   0    0    I   )   n-k
00085 *                k-s   s   n-k
00086 *
00087 *  If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k).
00088 *  If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k),
00089 *  and A(k,k), and v overwrites A(1:k-2,k-1:k).
00090 *
00091 *  If UPLO = 'L', then A = L*D*L', where
00092 *     L = P(1)*L(1)* ... *P(k)*L(k)* ...,
00093 *  i.e., L is a product of terms P(k)*L(k), where k increases from 1 to
00094 *  n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1
00095 *  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as
00096 *  defined by IPIV(k), and L(k) is a unit lower triangular matrix, such
00097 *  that if the diagonal block D(k) is of order s (s = 1 or 2), then
00098 *
00099 *             (   I    0     0   )  k-1
00100 *     L(k) =  (   0    I     0   )  s
00101 *             (   0    v     I   )  n-k-s+1
00102 *                k-1   s  n-k-s+1
00103 *
00104 *  If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k).
00105 *  If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k),
00106 *  and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1).
00107 *
00108 *  =====================================================================
00109 *
00110 *     .. Parameters ..
00111       REAL               ZERO, ONE
00112       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
00113       REAL               EIGHT, SEVTEN
00114       PARAMETER          ( EIGHT = 8.0E+0, SEVTEN = 17.0E+0 )
00115 *     ..
00116 *     .. Local Scalars ..
00117       LOGICAL            UPPER
00118       INTEGER            I, IMAX, J, JMAX, K, KC, KK, KNC, KP, KPC,
00119      $                   KSTEP, KX, NPP
00120       REAL               ABSAKK, ALPHA, COLMAX, D11, D12, D21, D22, R1,
00121      $                   ROWMAX, T, WK, WKM1, WKP1
00122 *     ..
00123 *     .. External Functions ..
00124       LOGICAL            LSAME
00125       INTEGER            ISAMAX
00126       EXTERNAL           LSAME, ISAMAX
00127 *     ..
00128 *     .. External Subroutines ..
00129       EXTERNAL           SSCAL, SSPR, SSWAP, XERBLA
00130 *     ..
00131 *     .. Intrinsic Functions ..
00132       INTRINSIC          ABS, MAX, SQRT
00133 *     ..
00134 *     .. Executable Statements ..
00135 *
00136 *     Test the input parameters.
00137 *
00138       INFO = 0
00139       UPPER = LSAME( UPLO, 'U' )
00140       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
00141          INFO = -1
00142       ELSE IF( N.LT.0 ) THEN
00143          INFO = -2
00144       END IF
00145       IF( INFO.NE.0 ) THEN
00146          CALL XERBLA( 'SSPTRF', -INFO )
00147          RETURN
00148       END IF
00149 *
00150 *     Initialize ALPHA for use in choosing pivot block size.
00151 *
00152       ALPHA = ( ONE+SQRT( SEVTEN ) ) / EIGHT
00153 *
00154       IF( UPPER ) THEN
00155 *
00156 *        Factorize A as U*D*U' using the upper triangle of A
00157 *
00158 *        K is the main loop index, decreasing from N to 1 in steps of
00159 *        1 or 2
00160 *
00161          K = N
00162          KC = ( N-1 )*N / 2 + 1
00163    10    CONTINUE
00164          KNC = KC
00165 *
00166 *        If K < 1, exit from loop
00167 *
00168          IF( K.LT.1 )
00169      $      GO TO 110
00170          KSTEP = 1
00171 *
00172 *        Determine rows and columns to be interchanged and whether
00173 *        a 1-by-1 or 2-by-2 pivot block will be used
00174 *
00175          ABSAKK = ABS( AP( KC+K-1 ) )
00176 *
00177 *        IMAX is the row-index of the largest off-diagonal element in
00178 *        column K, and COLMAX is its absolute value
00179 *
00180          IF( K.GT.1 ) THEN
00181             IMAX = ISAMAX( K-1, AP( KC ), 1 )
00182             COLMAX = ABS( AP( KC+IMAX-1 ) )
00183          ELSE
00184             COLMAX = ZERO
00185          END IF
00186 *
00187          IF( MAX( ABSAKK, COLMAX ).EQ.ZERO ) THEN
00188 *
00189 *           Column K is zero: set INFO and continue
00190 *
00191             IF( INFO.EQ.0 )
00192      $         INFO = K
00193             KP = K
00194          ELSE
00195             IF( ABSAKK.GE.ALPHA*COLMAX ) THEN
00196 *
00197 *              no interchange, use 1-by-1 pivot block
00198 *
00199                KP = K
00200             ELSE
00201 *
00202 *              JMAX is the column-index of the largest off-diagonal
00203 *              element in row IMAX, and ROWMAX is its absolute value
00204 *
00205                ROWMAX = ZERO
00206                JMAX = IMAX
00207                KX = IMAX*( IMAX+1 ) / 2 + IMAX
00208                DO 20 J = IMAX + 1, K
00209                   IF( ABS( AP( KX ) ).GT.ROWMAX ) THEN
00210                      ROWMAX = ABS( AP( KX ) )
00211                      JMAX = J
00212                   END IF
00213                   KX = KX + J
00214    20          CONTINUE
00215                KPC = ( IMAX-1 )*IMAX / 2 + 1
00216                IF( IMAX.GT.1 ) THEN
00217                   JMAX = ISAMAX( IMAX-1, AP( KPC ), 1 )
00218                   ROWMAX = MAX( ROWMAX, ABS( AP( KPC+JMAX-1 ) ) )
00219                END IF
00220 *
00221                IF( ABSAKK.GE.ALPHA*COLMAX*( COLMAX / ROWMAX ) ) THEN
00222 *
00223 *                 no interchange, use 1-by-1 pivot block
00224 *
00225                   KP = K
00226                ELSE IF( ABS( AP( KPC+IMAX-1 ) ).GE.ALPHA*ROWMAX ) THEN
00227 *
00228 *                 interchange rows and columns K and IMAX, use 1-by-1
00229 *                 pivot block
00230 *
00231                   KP = IMAX
00232                ELSE
00233 *
00234 *                 interchange rows and columns K-1 and IMAX, use 2-by-2
00235 *                 pivot block
00236 *
00237                   KP = IMAX
00238                   KSTEP = 2
00239                END IF
00240             END IF
00241 *
00242             KK = K - KSTEP + 1
00243             IF( KSTEP.EQ.2 )
00244      $         KNC = KNC - K + 1
00245             IF( KP.NE.KK ) THEN
00246 *
00247 *              Interchange rows and columns KK and KP in the leading
00248 *              submatrix A(1:k,1:k)
00249 *
00250                CALL SSWAP( KP-1, AP( KNC ), 1, AP( KPC ), 1 )
00251                KX = KPC + KP - 1
00252                DO 30 J = KP + 1, KK - 1
00253                   KX = KX + J - 1
00254                   T = AP( KNC+J-1 )
00255                   AP( KNC+J-1 ) = AP( KX )
00256                   AP( KX ) = T
00257    30          CONTINUE
00258                T = AP( KNC+KK-1 )
00259                AP( KNC+KK-1 ) = AP( KPC+KP-1 )
00260                AP( KPC+KP-1 ) = T
00261                IF( KSTEP.EQ.2 ) THEN
00262                   T = AP( KC+K-2 )
00263                   AP( KC+K-2 ) = AP( KC+KP-1 )
00264                   AP( KC+KP-1 ) = T
00265                END IF
00266             END IF
00267 *
00268 *           Update the leading submatrix
00269 *
00270             IF( KSTEP.EQ.1 ) THEN
00271 *
00272 *              1-by-1 pivot block D(k): column k now holds
00273 *
00274 *              W(k) = U(k)*D(k)
00275 *
00276 *              where U(k) is the k-th column of U
00277 *
00278 *              Perform a rank-1 update of A(1:k-1,1:k-1) as
00279 *
00280 *              A := A - U(k)*D(k)*U(k)' = A - W(k)*1/D(k)*W(k)'
00281 *
00282                R1 = ONE / AP( KC+K-1 )
00283                CALL SSPR( UPLO, K-1, -R1, AP( KC ), 1, AP )
00284 *
00285 *              Store U(k) in column k
00286 *
00287                CALL SSCAL( K-1, R1, AP( KC ), 1 )
00288             ELSE
00289 *
00290 *              2-by-2 pivot block D(k): columns k and k-1 now hold
00291 *
00292 *              ( W(k-1) W(k) ) = ( U(k-1) U(k) )*D(k)
00293 *
00294 *              where U(k) and U(k-1) are the k-th and (k-1)-th columns
00295 *              of U
00296 *
00297 *              Perform a rank-2 update of A(1:k-2,1:k-2) as
00298 *
00299 *              A := A - ( U(k-1) U(k) )*D(k)*( U(k-1) U(k) )'
00300 *                 = A - ( W(k-1) W(k) )*inv(D(k))*( W(k-1) W(k) )'
00301 *
00302                IF( K.GT.2 ) THEN
00303 *
00304                   D12 = AP( K-1+( K-1 )*K / 2 )
00305                   D22 = AP( K-1+( K-2 )*( K-1 ) / 2 ) / D12
00306                   D11 = AP( K+( K-1 )*K / 2 ) / D12
00307                   T = ONE / ( D11*D22-ONE )
00308                   D12 = T / D12
00309 *
00310                   DO 50 J = K - 2, 1, -1
00311                      WKM1 = D12*( D11*AP( J+( K-2 )*( K-1 ) / 2 )-
00312      $                      AP( J+( K-1 )*K / 2 ) )
00313                      WK = D12*( D22*AP( J+( K-1 )*K / 2 )-
00314      $                    AP( J+( K-2 )*( K-1 ) / 2 ) )
00315                      DO 40 I = J, 1, -1
00316                         AP( I+( J-1 )*J / 2 ) = AP( I+( J-1 )*J / 2 ) -
00317      $                     AP( I+( K-1 )*K / 2 )*WK -
00318      $                     AP( I+( K-2 )*( K-1 ) / 2 )*WKM1
00319    40                CONTINUE
00320                      AP( J+( K-1 )*K / 2 ) = WK
00321                      AP( J+( K-2 )*( K-1 ) / 2 ) = WKM1
00322    50             CONTINUE
00323 *
00324                END IF
00325 *
00326             END IF
00327          END IF
00328 *
00329 *        Store details of the interchanges in IPIV
00330 *
00331          IF( KSTEP.EQ.1 ) THEN
00332             IPIV( K ) = KP
00333          ELSE
00334             IPIV( K ) = -KP
00335             IPIV( K-1 ) = -KP
00336          END IF
00337 *
00338 *        Decrease K and return to the start of the main loop
00339 *
00340          K = K - KSTEP
00341          KC = KNC - K
00342          GO TO 10
00343 *
00344       ELSE
00345 *
00346 *        Factorize A as L*D*L' using the lower triangle of A
00347 *
00348 *        K is the main loop index, increasing from 1 to N in steps of
00349 *        1 or 2
00350 *
00351          K = 1
00352          KC = 1
00353          NPP = N*( N+1 ) / 2
00354    60    CONTINUE
00355          KNC = KC
00356 *
00357 *        If K > N, exit from loop
00358 *
00359          IF( K.GT.N )
00360      $      GO TO 110
00361          KSTEP = 1
00362 *
00363 *        Determine rows and columns to be interchanged and whether
00364 *        a 1-by-1 or 2-by-2 pivot block will be used
00365 *
00366          ABSAKK = ABS( AP( KC ) )
00367 *
00368 *        IMAX is the row-index of the largest off-diagonal element in
00369 *        column K, and COLMAX is its absolute value
00370 *
00371          IF( K.LT.N ) THEN
00372             IMAX = K + ISAMAX( N-K, AP( KC+1 ), 1 )
00373             COLMAX = ABS( AP( KC+IMAX-K ) )
00374          ELSE
00375             COLMAX = ZERO
00376          END IF
00377 *
00378          IF( MAX( ABSAKK, COLMAX ).EQ.ZERO ) THEN
00379 *
00380 *           Column K is zero: set INFO and continue
00381 *
00382             IF( INFO.EQ.0 )
00383      $         INFO = K
00384             KP = K
00385          ELSE
00386             IF( ABSAKK.GE.ALPHA*COLMAX ) THEN
00387 *
00388 *              no interchange, use 1-by-1 pivot block
00389 *
00390                KP = K
00391             ELSE
00392 *
00393 *              JMAX is the column-index of the largest off-diagonal
00394 *              element in row IMAX, and ROWMAX is its absolute value
00395 *
00396                ROWMAX = ZERO
00397                KX = KC + IMAX - K
00398                DO 70 J = K, IMAX - 1
00399                   IF( ABS( AP( KX ) ).GT.ROWMAX ) THEN
00400                      ROWMAX = ABS( AP( KX ) )
00401                      JMAX = J
00402                   END IF
00403                   KX = KX + N - J
00404    70          CONTINUE
00405                KPC = NPP - ( N-IMAX+1 )*( N-IMAX+2 ) / 2 + 1
00406                IF( IMAX.LT.N ) THEN
00407                   JMAX = IMAX + ISAMAX( N-IMAX, AP( KPC+1 ), 1 )
00408                   ROWMAX = MAX( ROWMAX, ABS( AP( KPC+JMAX-IMAX ) ) )
00409                END IF
00410 *
00411                IF( ABSAKK.GE.ALPHA*COLMAX*( COLMAX / ROWMAX ) ) THEN
00412 *
00413 *                 no interchange, use 1-by-1 pivot block
00414 *
00415                   KP = K
00416                ELSE IF( ABS( AP( KPC ) ).GE.ALPHA*ROWMAX ) THEN
00417 *
00418 *                 interchange rows and columns K and IMAX, use 1-by-1
00419 *                 pivot block
00420 *
00421                   KP = IMAX
00422                ELSE
00423 *
00424 *                 interchange rows and columns K+1 and IMAX, use 2-by-2
00425 *                 pivot block
00426 *
00427                   KP = IMAX
00428                   KSTEP = 2
00429                END IF
00430             END IF
00431 *
00432             KK = K + KSTEP - 1
00433             IF( KSTEP.EQ.2 )
00434      $         KNC = KNC + N - K + 1
00435             IF( KP.NE.KK ) THEN
00436 *
00437 *              Interchange rows and columns KK and KP in the trailing
00438 *              submatrix A(k:n,k:n)
00439 *
00440                IF( KP.LT.N )
00441      $            CALL SSWAP( N-KP, AP( KNC+KP-KK+1 ), 1, AP( KPC+1 ),
00442      $                        1 )
00443                KX = KNC + KP - KK
00444                DO 80 J = KK + 1, KP - 1
00445                   KX = KX + N - J + 1
00446                   T = AP( KNC+J-KK )
00447                   AP( KNC+J-KK ) = AP( KX )
00448                   AP( KX ) = T
00449    80          CONTINUE
00450                T = AP( KNC )
00451                AP( KNC ) = AP( KPC )
00452                AP( KPC ) = T
00453                IF( KSTEP.EQ.2 ) THEN
00454                   T = AP( KC+1 )
00455                   AP( KC+1 ) = AP( KC+KP-K )
00456                   AP( KC+KP-K ) = T
00457                END IF
00458             END IF
00459 *
00460 *           Update the trailing submatrix
00461 *
00462             IF( KSTEP.EQ.1 ) THEN
00463 *
00464 *              1-by-1 pivot block D(k): column k now holds
00465 *
00466 *              W(k) = L(k)*D(k)
00467 *
00468 *              where L(k) is the k-th column of L
00469 *
00470                IF( K.LT.N ) THEN
00471 *
00472 *                 Perform a rank-1 update of A(k+1:n,k+1:n) as
00473 *
00474 *                 A := A - L(k)*D(k)*L(k)' = A - W(k)*(1/D(k))*W(k)'
00475 *
00476                   R1 = ONE / AP( KC )
00477                   CALL SSPR( UPLO, N-K, -R1, AP( KC+1 ), 1,
00478      $                       AP( KC+N-K+1 ) )
00479 *
00480 *                 Store L(k) in column K
00481 *
00482                   CALL SSCAL( N-K, R1, AP( KC+1 ), 1 )
00483                END IF
00484             ELSE
00485 *
00486 *              2-by-2 pivot block D(k): columns K and K+1 now hold
00487 *
00488 *              ( W(k) W(k+1) ) = ( L(k) L(k+1) )*D(k)
00489 *
00490 *              where L(k) and L(k+1) are the k-th and (k+1)-th columns
00491 *              of L
00492 *
00493                IF( K.LT.N-1 ) THEN
00494 *
00495 *                 Perform a rank-2 update of A(k+2:n,k+2:n) as
00496 *
00497 *                 A := A - ( L(k) L(k+1) )*D(k)*( L(k) L(k+1) )'
00498 *                    = A - ( W(k) W(k+1) )*inv(D(k))*( W(k) W(k+1) )'
00499 *
00500                   D21 = AP( K+1+( K-1 )*( 2*N-K ) / 2 )
00501                   D11 = AP( K+1+K*( 2*N-K-1 ) / 2 ) / D21
00502                   D22 = AP( K+( K-1 )*( 2*N-K ) / 2 ) / D21
00503                   T = ONE / ( D11*D22-ONE )
00504                   D21 = T / D21
00505 *
00506                   DO 100 J = K + 2, N
00507                      WK = D21*( D11*AP( J+( K-1 )*( 2*N-K ) / 2 )-
00508      $                    AP( J+K*( 2*N-K-1 ) / 2 ) )
00509                      WKP1 = D21*( D22*AP( J+K*( 2*N-K-1 ) / 2 )-
00510      $                      AP( J+( K-1 )*( 2*N-K ) / 2 ) )
00511 *
00512                      DO 90 I = J, N
00513                         AP( I+( J-1 )*( 2*N-J ) / 2 ) = AP( I+( J-1 )*
00514      $                     ( 2*N-J ) / 2 ) - AP( I+( K-1 )*( 2*N-K ) /
00515      $                     2 )*WK - AP( I+K*( 2*N-K-1 ) / 2 )*WKP1
00516    90                CONTINUE
00517 *
00518                      AP( J+( K-1 )*( 2*N-K ) / 2 ) = WK
00519                      AP( J+K*( 2*N-K-1 ) / 2 ) = WKP1
00520 *
00521   100             CONTINUE
00522                END IF
00523             END IF
00524          END IF
00525 *
00526 *        Store details of the interchanges in IPIV
00527 *
00528          IF( KSTEP.EQ.1 ) THEN
00529             IPIV( K ) = KP
00530          ELSE
00531             IPIV( K ) = -KP
00532             IPIV( K+1 ) = -KP
00533          END IF
00534 *
00535 *        Increase K and return to the start of the main loop
00536 *
00537          K = K + KSTEP
00538          KC = KNC + N - K + 2
00539          GO TO 60
00540 *
00541       END IF
00542 *
00543   110 CONTINUE
00544       RETURN
00545 *
00546 *     End of SSPTRF
00547 *
00548       END
 All Files Functions