3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download SGERFS + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgerfs.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgerfs.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgerfs.f">
21 * SUBROUTINE SGERFS( TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV, B, LDB,
22 * X, LDX, FERR, BERR, WORK, IWORK, INFO )
24 * .. Scalar Arguments ..
26 * INTEGER INFO, LDA, LDAF, LDB, LDX, N, NRHS
28 * .. Array Arguments ..
29 * INTEGER IPIV( * ), IWORK( * )
30 * REAL A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
31 * $ BERR( * ), FERR( * ), WORK( * ), X( LDX, * )
40 *> SGERFS improves the computed solution to a system of linear
41 *> equations and provides error bounds and backward error estimates for
50 *> TRANS is CHARACTER*1
51 *> Specifies the form of the system of equations:
52 *> = 'N': A * X = B (No transpose)
53 *> = 'T': A**T * X = B (Transpose)
54 *> = 'C': A**H * X = B (Conjugate transpose = Transpose)
60 *> The order of the matrix A. N >= 0.
66 *> The number of right hand sides, i.e., the number of columns
67 *> of the matrices B and X. NRHS >= 0.
72 *> A is REAL array, dimension (LDA,N)
73 *> The original N-by-N matrix A.
79 *> The leading dimension of the array A. LDA >= max(1,N).
84 *> AF is REAL array, dimension (LDAF,N)
85 *> The factors L and U from the factorization A = P*L*U
86 *> as computed by SGETRF.
92 *> The leading dimension of the array AF. LDAF >= max(1,N).
97 *> IPIV is INTEGER array, dimension (N)
98 *> The pivot indices from SGETRF; for 1<=i<=N, row i of the
99 *> matrix was interchanged with row IPIV(i).
104 *> B is REAL array, dimension (LDB,NRHS)
105 *> The right hand side matrix B.
111 *> The leading dimension of the array B. LDB >= max(1,N).
116 *> X is REAL array, dimension (LDX,NRHS)
117 *> On entry, the solution matrix X, as computed by SGETRS.
118 *> On exit, the improved solution matrix X.
124 *> The leading dimension of the array X. LDX >= max(1,N).
129 *> FERR is REAL array, dimension (NRHS)
130 *> The estimated forward error bound for each solution vector
131 *> X(j) (the j-th column of the solution matrix X).
132 *> If XTRUE is the true solution corresponding to X(j), FERR(j)
133 *> is an estimated upper bound for the magnitude of the largest
134 *> element in (X(j) - XTRUE) divided by the magnitude of the
135 *> largest element in X(j). The estimate is as reliable as
136 *> the estimate for RCOND, and is almost always a slight
137 *> overestimate of the true error.
142 *> BERR is REAL array, dimension (NRHS)
143 *> The componentwise relative backward error of each solution
144 *> vector X(j) (i.e., the smallest relative change in
145 *> any element of A or B that makes X(j) an exact solution).
150 *> WORK is REAL array, dimension (3*N)
155 *> IWORK is INTEGER array, dimension (N)
161 *> = 0: successful exit
162 *> < 0: if INFO = -i, the i-th argument had an illegal value
165 *> \par Internal Parameters:
166 * =========================
169 *> ITMAX is the maximum number of steps of iterative refinement.
175 *> \author Univ. of Tennessee
176 *> \author Univ. of California Berkeley
177 *> \author Univ. of Colorado Denver
180 *> \date November 2011
182 *> \ingroup realGEcomputational
184 * =====================================================================
185 SUBROUTINE SGERFS( TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV, B, LDB,
186 $ X, LDX, FERR, BERR, WORK, IWORK, INFO )
188 * -- LAPACK computational routine (version 3.4.0) --
189 * -- LAPACK is a software package provided by Univ. of Tennessee, --
190 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
193 * .. Scalar Arguments ..
195 INTEGER INFO, LDA, LDAF, LDB, LDX, N, NRHS
197 * .. Array Arguments ..
198 INTEGER IPIV( * ), IWORK( * )
199 REAL A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
200 $ BERR( * ), FERR( * ), WORK( * ), X( LDX, * )
203 * =====================================================================
207 PARAMETER ( ITMAX = 5 )
209 PARAMETER ( ZERO = 0.0E+0 )
211 PARAMETER ( ONE = 1.0E+0 )
213 PARAMETER ( TWO = 2.0E+0 )
215 PARAMETER ( THREE = 3.0E+0 )
217 * .. Local Scalars ..
220 INTEGER COUNT, I, J, K, KASE, NZ
221 REAL EPS, LSTRES, S, SAFE1, SAFE2, SAFMIN, XK
226 * .. External Subroutines ..
227 EXTERNAL SAXPY, SCOPY, SGEMV, SGETRS, SLACN2, XERBLA
229 * .. Intrinsic Functions ..
232 * .. External Functions ..
235 EXTERNAL LSAME, SLAMCH
237 * .. Executable Statements ..
239 * Test the input parameters.
242 NOTRAN = LSAME( TRANS, 'N' )
243 IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
244 $ LSAME( TRANS, 'C' ) ) THEN
246 ELSE IF( N.LT.0 ) THEN
248 ELSE IF( NRHS.LT.0 ) THEN
250 ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
252 ELSE IF( LDAF.LT.MAX( 1, N ) ) THEN
254 ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
256 ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
260 CALL XERBLA( 'SGERFS', -INFO )
264 * Quick return if possible
266 IF( N.EQ.0 .OR. NRHS.EQ.0 ) THEN
280 * NZ = maximum number of nonzero elements in each row of A, plus 1
283 EPS = SLAMCH( 'Epsilon' )
284 SAFMIN = SLAMCH( 'Safe minimum' )
288 * Do for each right hand side
296 * Loop until stopping criterion is satisfied.
298 * Compute residual R = B - op(A) * X,
299 * where op(A) = A, A**T, or A**H, depending on TRANS.
301 CALL SCOPY( N, B( 1, J ), 1, WORK( N+1 ), 1 )
302 CALL SGEMV( TRANS, N, N, -ONE, A, LDA, X( 1, J ), 1, ONE,
305 * Compute componentwise relative backward error from formula
307 * max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) )
309 * where abs(Z) is the componentwise absolute value of the matrix
310 * or vector Z. If the i-th component of the denominator is less
311 * than SAFE2, then SAFE1 is added to the i-th components of the
312 * numerator and denominator before dividing.
315 WORK( I ) = ABS( B( I, J ) )
318 * Compute abs(op(A))*abs(X) + abs(B).
322 XK = ABS( X( K, J ) )
324 WORK( I ) = WORK( I ) + ABS( A( I, K ) )*XK
331 S = S + ABS( A( I, K ) )*ABS( X( I, J ) )
333 WORK( K ) = WORK( K ) + S
338 IF( WORK( I ).GT.SAFE2 ) THEN
339 S = MAX( S, ABS( WORK( N+I ) ) / WORK( I ) )
341 S = MAX( S, ( ABS( WORK( N+I ) )+SAFE1 ) /
342 $ ( WORK( I )+SAFE1 ) )
347 * Test stopping criterion. Continue iterating if
348 * 1) The residual BERR(J) is larger than machine epsilon, and
349 * 2) BERR(J) decreased by at least a factor of 2 during the
350 * last iteration, and
351 * 3) At most ITMAX iterations tried.
353 IF( BERR( J ).GT.EPS .AND. TWO*BERR( J ).LE.LSTRES .AND.
354 $ COUNT.LE.ITMAX ) THEN
356 * Update solution and try again.
358 CALL SGETRS( TRANS, N, 1, AF, LDAF, IPIV, WORK( N+1 ), N,
360 CALL SAXPY( N, ONE, WORK( N+1 ), 1, X( 1, J ), 1 )
366 * Bound error from formula
368 * norm(X - XTRUE) / norm(X) .le. FERR =
369 * norm( abs(inv(op(A)))*
370 * ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X)
373 * norm(Z) is the magnitude of the largest component of Z
374 * inv(op(A)) is the inverse of op(A)
375 * abs(Z) is the componentwise absolute value of the matrix or
377 * NZ is the maximum number of nonzeros in any row of A, plus 1
378 * EPS is machine epsilon
380 * The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B))
381 * is incremented by SAFE1 if the i-th component of
382 * abs(op(A))*abs(X) + abs(B) is less than SAFE2.
384 * Use SLACN2 to estimate the infinity-norm of the matrix
385 * inv(op(A)) * diag(W),
386 * where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) )))
389 IF( WORK( I ).GT.SAFE2 ) THEN
390 WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I )
392 WORK( I ) = ABS( WORK( N+I ) ) + NZ*EPS*WORK( I ) + SAFE1
398 CALL SLACN2( N, WORK( 2*N+1 ), WORK( N+1 ), IWORK, FERR( J ),
403 * Multiply by diag(W)*inv(op(A)**T).
405 CALL SGETRS( TRANST, N, 1, AF, LDAF, IPIV, WORK( N+1 ),
408 WORK( N+I ) = WORK( I )*WORK( N+I )
412 * Multiply by inv(op(A))*diag(W).
415 WORK( N+I ) = WORK( I )*WORK( N+I )
417 CALL SGETRS( TRANS, N, 1, AF, LDAF, IPIV, WORK( N+1 ), N,
427 LSTRES = MAX( LSTRES, ABS( X( I, J ) ) )
430 $ FERR( J ) = FERR( J ) / LSTRES