1 *> \brief <b> DGESVX computes the solution to system of linear equations A * X = B for GE matrices</b>
3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download DGESVX + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dgesvx.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dgesvx.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dgesvx.f">
21 * SUBROUTINE DGESVX( FACT, TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV,
22 * EQUED, R, C, B, LDB, X, LDX, RCOND, FERR, BERR,
25 * .. Scalar Arguments ..
26 * CHARACTER EQUED, FACT, TRANS
27 * INTEGER INFO, LDA, LDAF, LDB, LDX, N, NRHS
28 * DOUBLE PRECISION RCOND
30 * .. Array Arguments ..
31 * INTEGER IPIV( * ), IWORK( * )
32 * DOUBLE PRECISION A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
33 * $ BERR( * ), C( * ), FERR( * ), R( * ),
34 * $ WORK( * ), X( LDX, * )
43 *> DGESVX uses the LU factorization to compute the solution to a real
44 *> system of linear equations
46 *> where A is an N-by-N matrix and X and B are N-by-NRHS matrices.
48 *> Error bounds on the solution and a condition estimate are also
57 *> The following steps are performed:
59 *> 1. If FACT = 'E', real scaling factors are computed to equilibrate
61 *> TRANS = 'N': diag(R)*A*diag(C) *inv(diag(C))*X = diag(R)*B
62 *> TRANS = 'T': (diag(R)*A*diag(C))**T *inv(diag(R))*X = diag(C)*B
63 *> TRANS = 'C': (diag(R)*A*diag(C))**H *inv(diag(R))*X = diag(C)*B
64 *> Whether or not the system will be equilibrated depends on the
65 *> scaling of the matrix A, but if equilibration is used, A is
66 *> overwritten by diag(R)*A*diag(C) and B by diag(R)*B (if TRANS='N')
67 *> or diag(C)*B (if TRANS = 'T' or 'C').
69 *> 2. If FACT = 'N' or 'E', the LU decomposition is used to factor the
70 *> matrix A (after equilibration if FACT = 'E') as
72 *> where P is a permutation matrix, L is a unit lower triangular
73 *> matrix, and U is upper triangular.
75 *> 3. If some U(i,i)=0, so that U is exactly singular, then the routine
76 *> returns with INFO = i. Otherwise, the factored form of A is used
77 *> to estimate the condition number of the matrix A. If the
78 *> reciprocal of the condition number is less than machine precision,
79 *> INFO = N+1 is returned as a warning, but the routine still goes on
80 *> to solve for X and compute error bounds as described below.
82 *> 4. The system of equations is solved for X using the factored form
85 *> 5. Iterative refinement is applied to improve the computed solution
86 *> matrix and calculate error bounds and backward error estimates
89 *> 6. If equilibration was used, the matrix X is premultiplied by
90 *> diag(C) (if TRANS = 'N') or diag(R) (if TRANS = 'T' or 'C') so
91 *> that it solves the original system before equilibration.
99 *> FACT is CHARACTER*1
100 *> Specifies whether or not the factored form of the matrix A is
101 *> supplied on entry, and if not, whether the matrix A should be
102 *> equilibrated before it is factored.
103 *> = 'F': On entry, AF and IPIV contain the factored form of A.
104 *> If EQUED is not 'N', the matrix A has been
105 *> equilibrated with scaling factors given by R and C.
106 *> A, AF, and IPIV are not modified.
107 *> = 'N': The matrix A will be copied to AF and factored.
108 *> = 'E': The matrix A will be equilibrated if necessary, then
109 *> copied to AF and factored.
114 *> TRANS is CHARACTER*1
115 *> Specifies the form of the system of equations:
116 *> = 'N': A * X = B (No transpose)
117 *> = 'T': A**T * X = B (Transpose)
118 *> = 'C': A**H * X = B (Transpose)
124 *> The number of linear equations, i.e., the order of the
131 *> The number of right hand sides, i.e., the number of columns
132 *> of the matrices B and X. NRHS >= 0.
137 *> A is DOUBLE PRECISION array, dimension (LDA,N)
138 *> On entry, the N-by-N matrix A. If FACT = 'F' and EQUED is
139 *> not 'N', then A must have been equilibrated by the scaling
140 *> factors in R and/or C. A is not modified if FACT = 'F' or
141 *> 'N', or if FACT = 'E' and EQUED = 'N' on exit.
143 *> On exit, if EQUED .ne. 'N', A is scaled as follows:
144 *> EQUED = 'R': A := diag(R) * A
145 *> EQUED = 'C': A := A * diag(C)
146 *> EQUED = 'B': A := diag(R) * A * diag(C).
152 *> The leading dimension of the array A. LDA >= max(1,N).
157 *> AF is DOUBLE PRECISION array, dimension (LDAF,N)
158 *> If FACT = 'F', then AF is an input argument and on entry
159 *> contains the factors L and U from the factorization
160 *> A = P*L*U as computed by DGETRF. If EQUED .ne. 'N', then
161 *> AF is the factored form of the equilibrated matrix A.
163 *> If FACT = 'N', then AF is an output argument and on exit
164 *> returns the factors L and U from the factorization A = P*L*U
165 *> of the original matrix A.
167 *> If FACT = 'E', then AF is an output argument and on exit
168 *> returns the factors L and U from the factorization A = P*L*U
169 *> of the equilibrated matrix A (see the description of A for
170 *> the form of the equilibrated matrix).
176 *> The leading dimension of the array AF. LDAF >= max(1,N).
179 *> \param[in,out] IPIV
181 *> IPIV is INTEGER array, dimension (N)
182 *> If FACT = 'F', then IPIV is an input argument and on entry
183 *> contains the pivot indices from the factorization A = P*L*U
184 *> as computed by DGETRF; row i of the matrix was interchanged
187 *> If FACT = 'N', then IPIV is an output argument and on exit
188 *> contains the pivot indices from the factorization A = P*L*U
189 *> of the original matrix A.
191 *> If FACT = 'E', then IPIV is an output argument and on exit
192 *> contains the pivot indices from the factorization A = P*L*U
193 *> of the equilibrated matrix A.
196 *> \param[in,out] EQUED
198 *> EQUED is CHARACTER*1
199 *> Specifies the form of equilibration that was done.
200 *> = 'N': No equilibration (always true if FACT = 'N').
201 *> = 'R': Row equilibration, i.e., A has been premultiplied by
203 *> = 'C': Column equilibration, i.e., A has been postmultiplied
205 *> = 'B': Both row and column equilibration, i.e., A has been
206 *> replaced by diag(R) * A * diag(C).
207 *> EQUED is an input argument if FACT = 'F'; otherwise, it is an
213 *> R is DOUBLE PRECISION array, dimension (N)
214 *> The row scale factors for A. If EQUED = 'R' or 'B', A is
215 *> multiplied on the left by diag(R); if EQUED = 'N' or 'C', R
216 *> is not accessed. R is an input argument if FACT = 'F';
217 *> otherwise, R is an output argument. If FACT = 'F' and
218 *> EQUED = 'R' or 'B', each element of R must be positive.
223 *> C is DOUBLE PRECISION array, dimension (N)
224 *> The column scale factors for A. If EQUED = 'C' or 'B', A is
225 *> multiplied on the right by diag(C); if EQUED = 'N' or 'R', C
226 *> is not accessed. C is an input argument if FACT = 'F';
227 *> otherwise, C is an output argument. If FACT = 'F' and
228 *> EQUED = 'C' or 'B', each element of C must be positive.
233 *> B is DOUBLE PRECISION array, dimension (LDB,NRHS)
234 *> On entry, the N-by-NRHS right hand side matrix B.
236 *> if EQUED = 'N', B is not modified;
237 *> if TRANS = 'N' and EQUED = 'R' or 'B', B is overwritten by
239 *> if TRANS = 'T' or 'C' and EQUED = 'C' or 'B', B is
240 *> overwritten by diag(C)*B.
246 *> The leading dimension of the array B. LDB >= max(1,N).
251 *> X is DOUBLE PRECISION array, dimension (LDX,NRHS)
252 *> If INFO = 0 or INFO = N+1, the N-by-NRHS solution matrix X
253 *> to the original system of equations. Note that A and B are
254 *> modified on exit if EQUED .ne. 'N', and the solution to the
255 *> equilibrated system is inv(diag(C))*X if TRANS = 'N' and
256 *> EQUED = 'C' or 'B', or inv(diag(R))*X if TRANS = 'T' or 'C'
257 *> and EQUED = 'R' or 'B'.
263 *> The leading dimension of the array X. LDX >= max(1,N).
268 *> RCOND is DOUBLE PRECISION
269 *> The estimate of the reciprocal condition number of the matrix
270 *> A after equilibration (if done). If RCOND is less than the
271 *> machine precision (in particular, if RCOND = 0), the matrix
272 *> is singular to working precision. This condition is
273 *> indicated by a return code of INFO > 0.
278 *> FERR is DOUBLE PRECISION array, dimension (NRHS)
279 *> The estimated forward error bound for each solution vector
280 *> X(j) (the j-th column of the solution matrix X).
281 *> If XTRUE is the true solution corresponding to X(j), FERR(j)
282 *> is an estimated upper bound for the magnitude of the largest
283 *> element in (X(j) - XTRUE) divided by the magnitude of the
284 *> largest element in X(j). The estimate is as reliable as
285 *> the estimate for RCOND, and is almost always a slight
286 *> overestimate of the true error.
291 *> BERR is DOUBLE PRECISION array, dimension (NRHS)
292 *> The componentwise relative backward error of each solution
293 *> vector X(j) (i.e., the smallest relative change in
294 *> any element of A or B that makes X(j) an exact solution).
299 *> WORK is DOUBLE PRECISION array, dimension (4*N)
300 *> On exit, WORK(1) contains the reciprocal pivot growth
301 *> factor norm(A)/norm(U). The "max absolute element" norm is
302 *> used. If WORK(1) is much less than 1, then the stability
303 *> of the LU factorization of the (equilibrated) matrix A
304 *> could be poor. This also means that the solution X, condition
305 *> estimator RCOND, and forward error bound FERR could be
306 *> unreliable. If factorization fails with 0<INFO<=N, then
307 *> WORK(1) contains the reciprocal pivot growth factor for the
308 *> leading INFO columns of A.
313 *> IWORK is INTEGER array, dimension (N)
319 *> = 0: successful exit
320 *> < 0: if INFO = -i, the i-th argument had an illegal value
321 *> > 0: if INFO = i, and i is
322 *> <= N: U(i,i) is exactly zero. The factorization has
323 *> been completed, but the factor U is exactly
324 *> singular, so the solution and error bounds
325 *> could not be computed. RCOND = 0 is returned.
326 *> = N+1: U is nonsingular, but RCOND is less than machine
327 *> precision, meaning that the matrix is singular
328 *> to working precision. Nevertheless, the
329 *> solution and error bounds are computed because
330 *> there are a number of situations where the
331 *> computed solution can be more accurate than the
332 *> value of RCOND would suggest.
338 *> \author Univ. of Tennessee
339 *> \author Univ. of California Berkeley
340 *> \author Univ. of Colorado Denver
345 *> \ingroup doubleGEsolve
347 * =====================================================================
348 SUBROUTINE DGESVX( FACT, TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV,
349 $ EQUED, R, C, B, LDB, X, LDX, RCOND, FERR, BERR,
350 $ WORK, IWORK, INFO )
352 * -- LAPACK driver routine (version 3.4.1) --
353 * -- LAPACK is a software package provided by Univ. of Tennessee, --
354 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
357 * .. Scalar Arguments ..
358 CHARACTER EQUED, FACT, TRANS
359 INTEGER INFO, LDA, LDAF, LDB, LDX, N, NRHS
360 DOUBLE PRECISION RCOND
362 * .. Array Arguments ..
363 INTEGER IPIV( * ), IWORK( * )
364 DOUBLE PRECISION A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
365 $ BERR( * ), C( * ), FERR( * ), R( * ),
366 $ WORK( * ), X( LDX, * )
369 * =====================================================================
372 DOUBLE PRECISION ZERO, ONE
373 PARAMETER ( ZERO = 0.0D+0, ONE = 1.0D+0 )
375 * .. Local Scalars ..
376 LOGICAL COLEQU, EQUIL, NOFACT, NOTRAN, ROWEQU
379 DOUBLE PRECISION AMAX, ANORM, BIGNUM, COLCND, RCMAX, RCMIN,
380 $ ROWCND, RPVGRW, SMLNUM
382 * .. External Functions ..
384 DOUBLE PRECISION DLAMCH, DLANGE, DLANTR
385 EXTERNAL LSAME, DLAMCH, DLANGE, DLANTR
387 * .. External Subroutines ..
388 EXTERNAL DGECON, DGEEQU, DGERFS, DGETRF, DGETRS, DLACPY,
391 * .. Intrinsic Functions ..
394 * .. Executable Statements ..
397 NOFACT = LSAME( FACT, 'N' )
398 EQUIL = LSAME( FACT, 'E' )
399 NOTRAN = LSAME( TRANS, 'N' )
400 IF( NOFACT .OR. EQUIL ) THEN
405 ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
406 COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
407 SMLNUM = DLAMCH( 'Safe minimum' )
408 BIGNUM = ONE / SMLNUM
411 * Test the input parameters.
413 IF( .NOT.NOFACT .AND. .NOT.EQUIL .AND. .NOT.LSAME( FACT, 'F' ) )
416 ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
417 $ LSAME( TRANS, 'C' ) ) THEN
419 ELSE IF( N.LT.0 ) THEN
421 ELSE IF( NRHS.LT.0 ) THEN
423 ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
425 ELSE IF( LDAF.LT.MAX( 1, N ) ) THEN
427 ELSE IF( LSAME( FACT, 'F' ) .AND. .NOT.
428 $ ( ROWEQU .OR. COLEQU .OR. LSAME( EQUED, 'N' ) ) ) THEN
435 RCMIN = MIN( RCMIN, R( J ) )
436 RCMAX = MAX( RCMAX, R( J ) )
438 IF( RCMIN.LE.ZERO ) THEN
440 ELSE IF( N.GT.0 ) THEN
441 ROWCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
446 IF( COLEQU .AND. INFO.EQ.0 ) THEN
450 RCMIN = MIN( RCMIN, C( J ) )
451 RCMAX = MAX( RCMAX, C( J ) )
453 IF( RCMIN.LE.ZERO ) THEN
455 ELSE IF( N.GT.0 ) THEN
456 COLCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
462 IF( LDB.LT.MAX( 1, N ) ) THEN
464 ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
471 CALL XERBLA( 'DGESVX', -INFO )
477 * Compute row and column scalings to equilibrate the matrix A.
479 CALL DGEEQU( N, N, A, LDA, R, C, ROWCND, COLCND, AMAX, INFEQU )
480 IF( INFEQU.EQ.0 ) THEN
482 * Equilibrate the matrix.
484 CALL DLAQGE( N, N, A, LDA, R, C, ROWCND, COLCND, AMAX,
486 ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
487 COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
491 * Scale the right hand side.
497 B( I, J ) = R( I )*B( I, J )
501 ELSE IF( COLEQU ) THEN
504 B( I, J ) = C( I )*B( I, J )
509 IF( NOFACT .OR. EQUIL ) THEN
511 * Compute the LU factorization of A.
513 CALL DLACPY( 'Full', N, N, A, LDA, AF, LDAF )
514 CALL DGETRF( N, N, AF, LDAF, IPIV, INFO )
516 * Return if INFO is non-zero.
520 * Compute the reciprocal pivot growth factor of the
521 * leading rank-deficient INFO columns of A.
523 RPVGRW = DLANTR( 'M', 'U', 'N', INFO, INFO, AF, LDAF,
525 IF( RPVGRW.EQ.ZERO ) THEN
528 RPVGRW = DLANGE( 'M', N, INFO, A, LDA, WORK ) / RPVGRW
536 * Compute the norm of the matrix A and the
537 * reciprocal pivot growth factor RPVGRW.
544 ANORM = DLANGE( NORM, N, N, A, LDA, WORK )
545 RPVGRW = DLANTR( 'M', 'U', 'N', N, N, AF, LDAF, WORK )
546 IF( RPVGRW.EQ.ZERO ) THEN
549 RPVGRW = DLANGE( 'M', N, N, A, LDA, WORK ) / RPVGRW
552 * Compute the reciprocal of the condition number of A.
554 CALL DGECON( NORM, N, AF, LDAF, ANORM, RCOND, WORK, IWORK, INFO )
556 * Compute the solution matrix X.
558 CALL DLACPY( 'Full', N, NRHS, B, LDB, X, LDX )
559 CALL DGETRS( TRANS, N, NRHS, AF, LDAF, IPIV, X, LDX, INFO )
561 * Use iterative refinement to improve the computed solution and
562 * compute error bounds and backward error estimates for it.
564 CALL DGERFS( TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV, B, LDB, X,
565 $ LDX, FERR, BERR, WORK, IWORK, INFO )
567 * Transform the solution matrix X to a solution of the original
574 X( I, J ) = C( I )*X( I, J )
578 FERR( J ) = FERR( J ) / COLCND
581 ELSE IF( ROWEQU ) THEN
584 X( I, J ) = R( I )*X( I, J )
588 FERR( J ) = FERR( J ) / ROWCND
594 * Set INFO = N+1 if the matrix is singular to working precision.
596 IF( RCOND.LT.DLAMCH( 'Epsilon' ) )