LAPACK 3.3.0

sgtrfs.f

Go to the documentation of this file.
00001       SUBROUTINE SGTRFS( TRANS, N, NRHS, DL, D, DU, DLF, DF, DUF, DU2,
00002      $                   IPIV, B, LDB, X, LDX, FERR, BERR, WORK, IWORK,
00003      $                   INFO )
00004 *
00005 *  -- LAPACK routine (version 3.2) --
00006 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
00007 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
00008 *     November 2006
00009 *
00010 *     Modified to call SLACN2 in place of SLACON, 7 Feb 03, SJH.
00011 *
00012 *     .. Scalar Arguments ..
00013       CHARACTER          TRANS
00014       INTEGER            INFO, LDB, LDX, N, NRHS
00015 *     ..
00016 *     .. Array Arguments ..
00017       INTEGER            IPIV( * ), IWORK( * )
00018       REAL               B( LDB, * ), BERR( * ), D( * ), DF( * ),
00019      $                   DL( * ), DLF( * ), DU( * ), DU2( * ), DUF( * ),
00020      $                   FERR( * ), WORK( * ), X( LDX, * )
00021 *     ..
00022 *
00023 *  Purpose
00024 *  =======
00025 *
00026 *  SGTRFS improves the computed solution to a system of linear
00027 *  equations when the coefficient matrix is tridiagonal, and provides
00028 *  error bounds and backward error estimates for the solution.
00029 *
00030 *  Arguments
00031 *  =========
00032 *
00033 *  TRANS   (input) CHARACTER*1
00034 *          Specifies the form of the system of equations:
00035 *          = 'N':  A * X = B     (No transpose)
00036 *          = 'T':  A**T * X = B  (Transpose)
00037 *          = 'C':  A**H * X = B  (Conjugate transpose = Transpose)
00038 *
00039 *  N       (input) INTEGER
00040 *          The order of the matrix A.  N >= 0.
00041 *
00042 *  NRHS    (input) INTEGER
00043 *          The number of right hand sides, i.e., the number of columns
00044 *          of the matrix B.  NRHS >= 0.
00045 *
00046 *  DL      (input) REAL array, dimension (N-1)
00047 *          The (n-1) subdiagonal elements of A.
00048 *
00049 *  D       (input) REAL array, dimension (N)
00050 *          The diagonal elements of A.
00051 *
00052 *  DU      (input) REAL array, dimension (N-1)
00053 *          The (n-1) superdiagonal elements of A.
00054 *
00055 *  DLF     (input) REAL array, dimension (N-1)
00056 *          The (n-1) multipliers that define the matrix L from the
00057 *          LU factorization of A as computed by SGTTRF.
00058 *
00059 *  DF      (input) REAL array, dimension (N)
00060 *          The n diagonal elements of the upper triangular matrix U from
00061 *          the LU factorization of A.
00062 *
00063 *  DUF     (input) REAL array, dimension (N-1)
00064 *          The (n-1) elements of the first superdiagonal of U.
00065 *
00066 *  DU2     (input) REAL array, dimension (N-2)
00067 *          The (n-2) elements of the second superdiagonal of U.
00068 *
00069 *  IPIV    (input) INTEGER array, dimension (N)
00070 *          The pivot indices; for 1 <= i <= n, row i of the matrix was
00071 *          interchanged with row IPIV(i).  IPIV(i) will always be either
00072 *          i or i+1; IPIV(i) = i indicates a row interchange was not
00073 *          required.
00074 *
00075 *  B       (input) REAL array, dimension (LDB,NRHS)
00076 *          The right hand side matrix B.
00077 *
00078 *  LDB     (input) INTEGER
00079 *          The leading dimension of the array B.  LDB >= max(1,N).
00080 *
00081 *  X       (input/output) REAL array, dimension (LDX,NRHS)
00082 *          On entry, the solution matrix X, as computed by SGTTRS.
00083 *          On exit, the improved solution matrix X.
00084 *
00085 *  LDX     (input) INTEGER
00086 *          The leading dimension of the array X.  LDX >= max(1,N).
00087 *
00088 *  FERR    (output) REAL array, dimension (NRHS)
00089 *          The estimated forward error bound for each solution vector
00090 *          X(j) (the j-th column of the solution matrix X).
00091 *          If XTRUE is the true solution corresponding to X(j), FERR(j)
00092 *          is an estimated upper bound for the magnitude of the largest
00093 *          element in (X(j) - XTRUE) divided by the magnitude of the
00094 *          largest element in X(j).  The estimate is as reliable as
00095 *          the estimate for RCOND, and is almost always a slight
00096 *          overestimate of the true error.
00097 *
00098 *  BERR    (output) REAL array, dimension (NRHS)
00099 *          The componentwise relative backward error of each solution
00100 *          vector X(j) (i.e., the smallest relative change in
00101 *          any element of A or B that makes X(j) an exact solution).
00102 *
00103 *  WORK    (workspace) REAL array, dimension (3*N)
00104 *
00105 *  IWORK   (workspace) INTEGER array, dimension (N)
00106 *
00107 *  INFO    (output) INTEGER
00108 *          = 0:  successful exit
00109 *          < 0:  if INFO = -i, the i-th argument had an illegal value
00110 *
00111 *  Internal Parameters
00112 *  ===================
00113 *
00114 *  ITMAX is the maximum number of steps of iterative refinement.
00115 *
00116 *  =====================================================================
00117 *
00118 *     .. Parameters ..
00119       INTEGER            ITMAX
00120       PARAMETER          ( ITMAX = 5 )
00121       REAL               ZERO, ONE
00122       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
00123       REAL               TWO
00124       PARAMETER          ( TWO = 2.0E+0 )
00125       REAL               THREE
00126       PARAMETER          ( THREE = 3.0E+0 )
00127 *     ..
00128 *     .. Local Scalars ..
00129       LOGICAL            NOTRAN
00130       CHARACTER          TRANSN, TRANST
00131       INTEGER            COUNT, I, J, KASE, NZ
00132       REAL               EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN
00133 *     ..
00134 *     .. Local Arrays ..
00135       INTEGER            ISAVE( 3 )
00136 *     ..
00137 *     .. External Subroutines ..
00138       EXTERNAL           SAXPY, SCOPY, SGTTRS, SLACN2, SLAGTM, XERBLA
00139 *     ..
00140 *     .. Intrinsic Functions ..
00141       INTRINSIC          ABS, MAX
00142 *     ..
00143 *     .. External Functions ..
00144       LOGICAL            LSAME
00145       REAL               SLAMCH
00146       EXTERNAL           LSAME, SLAMCH
00147 *     ..
00148 *     .. Executable Statements ..
00149 *
00150 *     Test the input parameters.
00151 *
00152       INFO = 0
00153       NOTRAN = LSAME( TRANS, 'N' )
00154       IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
00155      $    LSAME( TRANS, 'C' ) ) THEN
00156          INFO = -1
00157       ELSE IF( N.LT.0 ) THEN
00158          INFO = -2
00159       ELSE IF( NRHS.LT.0 ) THEN
00160          INFO = -3
00161       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
00162          INFO = -13
00163       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
00164          INFO = -15
00165       END IF
00166       IF( INFO.NE.0 ) THEN
00167          CALL XERBLA( 'SGTRFS', -INFO )
00168          RETURN
00169       END IF
00170 *
00171 *     Quick return if possible
00172 *
00173       IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
00174          DO 10 J = 1, NRHS
00175             FERR( J ) = ZERO
00176             BERR( J ) = ZERO
00177    10    CONTINUE
00178          RETURN
00179       END IF
00180 *
00181       IF( NOTRAN ) THEN
00182          TRANSN = 'N'
00183          TRANST = 'T'
00184       ELSE
00185          TRANSN = 'T'
00186          TRANST = 'N'
00187       END IF
00188 *
00189 *     NZ = maximum number of nonzero elements in each row of A, plus 1
00190 *
00191       NZ = 4
00192       EPS = SLAMCH( 'Epsilon' )
00193       SAFMIN = SLAMCH( 'Safe minimum' )
00194       SAFE1 = NZ*SAFMIN
00195       SAFE2 = SAFE1 / EPS
00196 *
00197 *     Do for each right hand side
00198 *
00199       DO 110 J = 1, NRHS
00200 *
00201          COUNT = 1
00202          LSTRES = THREE
00203    20    CONTINUE
00204 *
00205 *        Loop until stopping criterion is satisfied.
00206 *
00207 *        Compute residual R = B - op(A) * X,
00208 *        where op(A) = A, A**T, or A**H, depending on TRANS.
00209 *
00210          CALL SCOPY( N, B( 1, J ), 1, WORK( N+1 ), 1 )
00211          CALL SLAGTM( TRANS, N, 1, -ONE, DL, D, DU, X( 1, J ), LDX, ONE,
00212      $                WORK( N+1 ), N )
00213 *
00214 *        Compute abs(op(A))*abs(x) + abs(b) for use in the backward
00215 *        error bound.
00216 *
00217          IF( NOTRAN ) THEN
00218             IF( N.EQ.1 ) THEN
00219                WORK( 1 ) = ABS( B( 1, J ) ) + ABS( D( 1 )*X( 1, J ) )
00220             ELSE
00221                WORK( 1 ) = ABS( B( 1, J ) ) + ABS( D( 1 )*X( 1, J ) ) +
00222      $                     ABS( DU( 1 )*X( 2, J ) )
00223                DO 30 I = 2, N - 1
00224                   WORK( I ) = ABS( B( I, J ) ) +
00225      $                        ABS( DL( I-1 )*X( I-1, J ) ) +
00226      $                        ABS( D( I )*X( I, J ) ) +
00227      $                        ABS( DU( I )*X( I+1, J ) )
00228    30          CONTINUE
00229                WORK( N ) = ABS( B( N, J ) ) +
00230      $                     ABS( DL( N-1 )*X( N-1, J ) ) +
00231      $                     ABS( D( N )*X( N, J ) )
00232             END IF
00233          ELSE
00234             IF( N.EQ.1 ) THEN
00235                WORK( 1 ) = ABS( B( 1, J ) ) + ABS( D( 1 )*X( 1, J ) )
00236             ELSE
00237                WORK( 1 ) = ABS( B( 1, J ) ) + ABS( D( 1 )*X( 1, J ) ) +
00238      $                     ABS( DL( 1 )*X( 2, J ) )
00239                DO 40 I = 2, N - 1
00240                   WORK( I ) = ABS( B( I, J ) ) +
00241      $                        ABS( DU( I-1 )*X( I-1, J ) ) +
00242      $                        ABS( D( I )*X( I, J ) ) +
00243      $                        ABS( DL( I )*X( I+1, J ) )
00244    40          CONTINUE
00245                WORK( N ) = ABS( B( N, J ) ) +
00246      $                     ABS( DU( N-1 )*X( N-1, J ) ) +
00247      $                     ABS( D( N )*X( N, J ) )
00248             END IF
00249          END IF
00250 *
00251 *        Compute componentwise relative backward error from formula
00252 *
00253 *        max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) )
00254 *
00255 *        where abs(Z) is the componentwise absolute value of the matrix
00256 *        or vector Z.  If the i-th component of the denominator is less
00257 *        than SAFE2, then SAFE1 is added to the i-th components of the
00258 *        numerator and denominator before dividing.
00259 *
00260          S = ZERO
00261          DO 50 I = 1, N
00262             IF( WORK( I ).GT.SAFE2 ) THEN
00263                S = MAX( S, ABS( WORK( N+I ) ) / WORK( I ) )
00264             ELSE
00265                S = MAX( S, ( ABS( WORK( N+I ) )+SAFE1 ) /
00266      $             ( WORK( I )+SAFE1 ) )
00267             END IF
00268    50    CONTINUE
00269          BERR( J ) = S
00270 *
00271 *        Test stopping criterion. Continue iterating if
00272 *           1) The residual BERR(J) is larger than machine epsilon, and
00273 *           2) BERR(J) decreased by at least a factor of 2 during the
00274 *              last iteration, and
00275 *           3) At most ITMAX iterations tried.
00276 *
00277          IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
00278      $       COUNT.LE.ITMAX ) THEN
00279 *
00280 *           Update solution and try again.
00281 *
00282             CALL SGTTRS( TRANS, N, 1, DLF, DF, DUF, DU2, IPIV,
00283      $                   WORK( N+1 ), N, INFO )
00284             CALL SAXPY( N, ONE, WORK( N+1 ), 1, X( 1, J ), 1 )
00285             LSTRES = BERR( J )
00286             COUNT = COUNT + 1
00287             GO TO 20
00288          END IF
00289 *
00290 *        Bound error from formula
00291 *
00292 *        norm(X - XTRUE) / norm(X) .le. FERR =
00293 *        norm( abs(inv(op(A)))*
00294 *           ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X)
00295 *
00296 *        where
00297 *          norm(Z) is the magnitude of the largest component of Z
00298 *          inv(op(A)) is the inverse of op(A)
00299 *          abs(Z) is the componentwise absolute value of the matrix or
00300 *             vector Z
00301 *          NZ is the maximum number of nonzeros in any row of A, plus 1
00302 *          EPS is machine epsilon
00303 *
00304 *        The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B))
00305 *        is incremented by SAFE1 if the i-th component of
00306 *        abs(op(A))*abs(X) + abs(B) is less than SAFE2.
00307 *
00308 *        Use SLACN2 to estimate the infinity-norm of the matrix
00309 *           inv(op(A)) * diag(W),
00310 *        where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) )))
00311 *
00312          DO 60 I = 1, N
00313             IF( WORK( I ).GT.SAFE2 ) THEN
00314                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I )
00315             ELSE
00316                WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I ) + SAFE1
00317             END IF
00318    60    CONTINUE
00319 *
00320          KASE = 0
00321    70    CONTINUE
00322          CALL SLACN2( N, WORK( 2*N+1 ), WORK( N+1 ), IWORK, FERR( J ),
00323      $                KASE, ISAVE )
00324          IF( KASE.NE.0 ) THEN
00325             IF( KASE.EQ.1 ) THEN
00326 *
00327 *              Multiply by diag(W)*inv(op(A)**T).
00328 *
00329                CALL SGTTRS( TRANST, N, 1, DLF, DF, DUF, DU2, IPIV,
00330      $                      WORK( N+1 ), N, INFO )
00331                DO 80 I = 1, N
00332                   WORK( N+I ) = WORK( I )*WORK( N+I )
00333    80          CONTINUE
00334             ELSE
00335 *
00336 *              Multiply by inv(op(A))*diag(W).
00337 *
00338                DO 90 I = 1, N
00339                   WORK( N+I ) = WORK( I )*WORK( N+I )
00340    90          CONTINUE
00341                CALL SGTTRS( TRANSN, N, 1, DLF, DF, DUF, DU2, IPIV,
00342      $                      WORK( N+1 ), N, INFO )
00343             END IF
00344             GO TO 70
00345          END IF
00346 *
00347 *        Normalize error.
00348 *
00349          LSTRES = ZERO
00350          DO 100 I = 1, N
00351             LSTRES = MAX( LSTRES, ABS( X( I, J ) ) )
00352   100    CONTINUE
00353          IF( LSTRES.NE.ZERO )
00354      $      FERR( J ) = FERR( J ) / LSTRES
00355 *
00356   110 CONTINUE
00357 *
00358       RETURN
00359 *
00360 *     End of SGTRFS
00361 *
00362       END
 All Files Functions