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