1 *> \brief <b> CGESVX 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 CGESVX + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgesvx.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgesvx.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgesvx.f">
21 * SUBROUTINE CGESVX( 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
30 * .. Array Arguments ..
32 * REAL BERR( * ), C( * ), FERR( * ), R( * ),
34 * COMPLEX A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
35 * $ WORK( * ), X( LDX, * )
44 *> CGESVX uses the LU factorization to compute the solution to a complex
45 *> system of linear equations
47 *> where A is an N-by-N matrix and X and B are N-by-NRHS matrices.
49 *> Error bounds on the solution and a condition estimate are also
58 *> The following steps are performed:
60 *> 1. If FACT = 'E', real scaling factors are computed to equilibrate
62 *> TRANS = 'N': diag(R)*A*diag(C) *inv(diag(C))*X = diag(R)*B
63 *> TRANS = 'T': (diag(R)*A*diag(C))**T *inv(diag(R))*X = diag(C)*B
64 *> TRANS = 'C': (diag(R)*A*diag(C))**H *inv(diag(R))*X = diag(C)*B
65 *> Whether or not the system will be equilibrated depends on the
66 *> scaling of the matrix A, but if equilibration is used, A is
67 *> overwritten by diag(R)*A*diag(C) and B by diag(R)*B (if TRANS='N')
68 *> or diag(C)*B (if TRANS = 'T' or 'C').
70 *> 2. If FACT = 'N' or 'E', the LU decomposition is used to factor the
71 *> matrix A (after equilibration if FACT = 'E') as
73 *> where P is a permutation matrix, L is a unit lower triangular
74 *> matrix, and U is upper triangular.
76 *> 3. If some U(i,i)=0, so that U is exactly singular, then the routine
77 *> returns with INFO = i. Otherwise, the factored form of A is used
78 *> to estimate the condition number of the matrix A. If the
79 *> reciprocal of the condition number is less than machine precision,
80 *> INFO = N+1 is returned as a warning, but the routine still goes on
81 *> to solve for X and compute error bounds as described below.
83 *> 4. The system of equations is solved for X using the factored form
86 *> 5. Iterative refinement is applied to improve the computed solution
87 *> matrix and calculate error bounds and backward error estimates
90 *> 6. If equilibration was used, the matrix X is premultiplied by
91 *> diag(C) (if TRANS = 'N') or diag(R) (if TRANS = 'T' or 'C') so
92 *> that it solves the original system before equilibration.
100 *> FACT is CHARACTER*1
101 *> Specifies whether or not the factored form of the matrix A is
102 *> supplied on entry, and if not, whether the matrix A should be
103 *> equilibrated before it is factored.
104 *> = 'F': On entry, AF and IPIV contain the factored form of A.
105 *> If EQUED is not 'N', the matrix A has been
106 *> equilibrated with scaling factors given by R and C.
107 *> A, AF, and IPIV are not modified.
108 *> = 'N': The matrix A will be copied to AF and factored.
109 *> = 'E': The matrix A will be equilibrated if necessary, then
110 *> copied to AF and factored.
115 *> TRANS is CHARACTER*1
116 *> Specifies the form of the system of equations:
117 *> = 'N': A * X = B (No transpose)
118 *> = 'T': A**T * X = B (Transpose)
119 *> = 'C': A**H * X = B (Conjugate transpose)
125 *> The number of linear equations, i.e., the order of the
132 *> The number of right hand sides, i.e., the number of columns
133 *> of the matrices B and X. NRHS >= 0.
138 *> A is COMPLEX array, dimension (LDA,N)
139 *> On entry, the N-by-N matrix A. If FACT = 'F' and EQUED is
140 *> not 'N', then A must have been equilibrated by the scaling
141 *> factors in R and/or C. A is not modified if FACT = 'F' or
142 *> 'N', or if FACT = 'E' and EQUED = 'N' on exit.
144 *> On exit, if EQUED .ne. 'N', A is scaled as follows:
145 *> EQUED = 'R': A := diag(R) * A
146 *> EQUED = 'C': A := A * diag(C)
147 *> EQUED = 'B': A := diag(R) * A * diag(C).
153 *> The leading dimension of the array A. LDA >= max(1,N).
158 *> AF is COMPLEX array, dimension (LDAF,N)
159 *> If FACT = 'F', then AF is an input argument and on entry
160 *> contains the factors L and U from the factorization
161 *> A = P*L*U as computed by CGETRF. If EQUED .ne. 'N', then
162 *> AF is the factored form of the equilibrated matrix A.
164 *> If FACT = 'N', then AF is an output argument and on exit
165 *> returns the factors L and U from the factorization A = P*L*U
166 *> of the original matrix A.
168 *> If FACT = 'E', then AF is an output argument and on exit
169 *> returns the factors L and U from the factorization A = P*L*U
170 *> of the equilibrated matrix A (see the description of A for
171 *> the form of the equilibrated matrix).
177 *> The leading dimension of the array AF. LDAF >= max(1,N).
180 *> \param[in,out] IPIV
182 *> IPIV is INTEGER array, dimension (N)
183 *> If FACT = 'F', then IPIV is an input argument and on entry
184 *> contains the pivot indices from the factorization A = P*L*U
185 *> as computed by CGETRF; row i of the matrix was interchanged
188 *> If FACT = 'N', then IPIV is an output argument and on exit
189 *> contains the pivot indices from the factorization A = P*L*U
190 *> of the original matrix A.
192 *> If FACT = 'E', then IPIV is an output argument and on exit
193 *> contains the pivot indices from the factorization A = P*L*U
194 *> of the equilibrated matrix A.
197 *> \param[in,out] EQUED
199 *> EQUED is CHARACTER*1
200 *> Specifies the form of equilibration that was done.
201 *> = 'N': No equilibration (always true if FACT = 'N').
202 *> = 'R': Row equilibration, i.e., A has been premultiplied by
204 *> = 'C': Column equilibration, i.e., A has been postmultiplied
206 *> = 'B': Both row and column equilibration, i.e., A has been
207 *> replaced by diag(R) * A * diag(C).
208 *> EQUED is an input argument if FACT = 'F'; otherwise, it is an
214 *> R is REAL array, dimension (N)
215 *> The row scale factors for A. If EQUED = 'R' or 'B', A is
216 *> multiplied on the left by diag(R); if EQUED = 'N' or 'C', R
217 *> is not accessed. R is an input argument if FACT = 'F';
218 *> otherwise, R is an output argument. If FACT = 'F' and
219 *> EQUED = 'R' or 'B', each element of R must be positive.
224 *> C is REAL array, dimension (N)
225 *> The column scale factors for A. If EQUED = 'C' or 'B', A is
226 *> multiplied on the right by diag(C); if EQUED = 'N' or 'R', C
227 *> is not accessed. C is an input argument if FACT = 'F';
228 *> otherwise, C is an output argument. If FACT = 'F' and
229 *> EQUED = 'C' or 'B', each element of C must be positive.
234 *> B is COMPLEX array, dimension (LDB,NRHS)
235 *> On entry, the N-by-NRHS right hand side matrix B.
237 *> if EQUED = 'N', B is not modified;
238 *> if TRANS = 'N' and EQUED = 'R' or 'B', B is overwritten by
240 *> if TRANS = 'T' or 'C' and EQUED = 'C' or 'B', B is
241 *> overwritten by diag(C)*B.
247 *> The leading dimension of the array B. LDB >= max(1,N).
252 *> X is COMPLEX array, dimension (LDX,NRHS)
253 *> If INFO = 0 or INFO = N+1, the N-by-NRHS solution matrix X
254 *> to the original system of equations. Note that A and B are
255 *> modified on exit if EQUED .ne. 'N', and the solution to the
256 *> equilibrated system is inv(diag(C))*X if TRANS = 'N' and
257 *> EQUED = 'C' or 'B', or inv(diag(R))*X if TRANS = 'T' or 'C'
258 *> and EQUED = 'R' or 'B'.
264 *> The leading dimension of the array X. LDX >= max(1,N).
270 *> The estimate of the reciprocal condition number of the matrix
271 *> A after equilibration (if done). If RCOND is less than the
272 *> machine precision (in particular, if RCOND = 0), the matrix
273 *> is singular to working precision. This condition is
274 *> indicated by a return code of INFO > 0.
279 *> FERR is REAL array, dimension (NRHS)
280 *> The estimated forward error bound for each solution vector
281 *> X(j) (the j-th column of the solution matrix X).
282 *> If XTRUE is the true solution corresponding to X(j), FERR(j)
283 *> is an estimated upper bound for the magnitude of the largest
284 *> element in (X(j) - XTRUE) divided by the magnitude of the
285 *> largest element in X(j). The estimate is as reliable as
286 *> the estimate for RCOND, and is almost always a slight
287 *> overestimate of the true error.
292 *> BERR is REAL array, dimension (NRHS)
293 *> The componentwise relative backward error of each solution
294 *> vector X(j) (i.e., the smallest relative change in
295 *> any element of A or B that makes X(j) an exact solution).
300 *> WORK is COMPLEX array, dimension (2*N)
305 *> RWORK is REAL array, dimension (2*N)
306 *> On exit, RWORK(1) contains the reciprocal pivot growth
307 *> factor norm(A)/norm(U). The "max absolute element" norm is
308 *> used. If RWORK(1) is much less than 1, then the stability
309 *> of the LU factorization of the (equilibrated) matrix A
310 *> could be poor. This also means that the solution X, condition
311 *> estimator RCOND, and forward error bound FERR could be
312 *> unreliable. If factorization fails with 0<INFO<=N, then
313 *> RWORK(1) contains the reciprocal pivot growth factor for the
314 *> leading INFO columns of A.
320 *> = 0: successful exit
321 *> < 0: if INFO = -i, the i-th argument had an illegal value
322 *> > 0: if INFO = i, and i is
323 *> <= N: U(i,i) is exactly zero. The factorization has
324 *> been completed, but the factor U is exactly
325 *> singular, so the solution and error bounds
326 *> could not be computed. RCOND = 0 is returned.
327 *> = N+1: U is nonsingular, but RCOND is less than machine
328 *> precision, meaning that the matrix is singular
329 *> to working precision. Nevertheless, the
330 *> solution and error bounds are computed because
331 *> there are a number of situations where the
332 *> computed solution can be more accurate than the
333 *> value of RCOND would suggest.
339 *> \author Univ. of Tennessee
340 *> \author Univ. of California Berkeley
341 *> \author Univ. of Colorado Denver
346 *> \ingroup complexGEsolve
348 * =====================================================================
349 SUBROUTINE CGESVX( FACT, TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV,
350 $ EQUED, R, C, B, LDB, X, LDX, RCOND, FERR, BERR,
351 $ WORK, RWORK, INFO )
353 * -- LAPACK driver routine (version 3.4.1) --
354 * -- LAPACK is a software package provided by Univ. of Tennessee, --
355 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
358 * .. Scalar Arguments ..
359 CHARACTER EQUED, FACT, TRANS
360 INTEGER INFO, LDA, LDAF, LDB, LDX, N, NRHS
363 * .. Array Arguments ..
365 REAL BERR( * ), C( * ), FERR( * ), R( * ),
367 COMPLEX A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
368 $ WORK( * ), X( LDX, * )
371 * =====================================================================
375 PARAMETER ( ZERO = 0.0E+0, ONE = 1.0E+0 )
377 * .. Local Scalars ..
378 LOGICAL COLEQU, EQUIL, NOFACT, NOTRAN, ROWEQU
381 REAL AMAX, ANORM, BIGNUM, COLCND, RCMAX, RCMIN,
382 $ ROWCND, RPVGRW, SMLNUM
384 * .. External Functions ..
386 REAL CLANGE, CLANTR, SLAMCH
387 EXTERNAL LSAME, CLANGE, CLANTR, SLAMCH
389 * .. External Subroutines ..
390 EXTERNAL CGECON, CGEEQU, CGERFS, CGETRF, CGETRS, CLACPY,
393 * .. Intrinsic Functions ..
396 * .. Executable Statements ..
399 NOFACT = LSAME( FACT, 'N' )
400 EQUIL = LSAME( FACT, 'E' )
401 NOTRAN = LSAME( TRANS, 'N' )
402 IF( NOFACT .OR. EQUIL ) THEN
407 ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
408 COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
409 SMLNUM = SLAMCH( 'Safe minimum' )
410 BIGNUM = ONE / SMLNUM
413 * Test the input parameters.
415 IF( .NOT.NOFACT .AND. .NOT.EQUIL .AND. .NOT.LSAME( FACT, 'F' ) )
418 ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
419 $ LSAME( TRANS, 'C' ) ) THEN
421 ELSE IF( N.LT.0 ) THEN
423 ELSE IF( NRHS.LT.0 ) THEN
425 ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
427 ELSE IF( LDAF.LT.MAX( 1, N ) ) THEN
429 ELSE IF( LSAME( FACT, 'F' ) .AND. .NOT.
430 $ ( ROWEQU .OR. COLEQU .OR. LSAME( EQUED, 'N' ) ) ) THEN
437 RCMIN = MIN( RCMIN, R( J ) )
438 RCMAX = MAX( RCMAX, R( J ) )
440 IF( RCMIN.LE.ZERO ) THEN
442 ELSE IF( N.GT.0 ) THEN
443 ROWCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
448 IF( COLEQU .AND. INFO.EQ.0 ) THEN
452 RCMIN = MIN( RCMIN, C( J ) )
453 RCMAX = MAX( RCMAX, C( J ) )
455 IF( RCMIN.LE.ZERO ) THEN
457 ELSE IF( N.GT.0 ) THEN
458 COLCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
464 IF( LDB.LT.MAX( 1, N ) ) THEN
466 ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
473 CALL XERBLA( 'CGESVX', -INFO )
479 * Compute row and column scalings to equilibrate the matrix A.
481 CALL CGEEQU( N, N, A, LDA, R, C, ROWCND, COLCND, AMAX, INFEQU )
482 IF( INFEQU.EQ.0 ) THEN
484 * Equilibrate the matrix.
486 CALL CLAQGE( N, N, A, LDA, R, C, ROWCND, COLCND, AMAX,
488 ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
489 COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
493 * Scale the right hand side.
499 B( I, J ) = R( I )*B( I, J )
503 ELSE IF( COLEQU ) THEN
506 B( I, J ) = C( I )*B( I, J )
511 IF( NOFACT .OR. EQUIL ) THEN
513 * Compute the LU factorization of A.
515 CALL CLACPY( 'Full', N, N, A, LDA, AF, LDAF )
516 CALL CGETRF( N, N, AF, LDAF, IPIV, INFO )
518 * Return if INFO is non-zero.
522 * Compute the reciprocal pivot growth factor of the
523 * leading rank-deficient INFO columns of A.
525 RPVGRW = CLANTR( 'M', 'U', 'N', INFO, INFO, AF, LDAF,
527 IF( RPVGRW.EQ.ZERO ) THEN
530 RPVGRW = CLANGE( 'M', N, INFO, A, LDA, RWORK ) /
539 * Compute the norm of the matrix A and the
540 * reciprocal pivot growth factor RPVGRW.
547 ANORM = CLANGE( NORM, N, N, A, LDA, RWORK )
548 RPVGRW = CLANTR( 'M', 'U', 'N', N, N, AF, LDAF, RWORK )
549 IF( RPVGRW.EQ.ZERO ) THEN
552 RPVGRW = CLANGE( 'M', N, N, A, LDA, RWORK ) / RPVGRW
555 * Compute the reciprocal of the condition number of A.
557 CALL CGECON( NORM, N, AF, LDAF, ANORM, RCOND, WORK, RWORK, INFO )
559 * Compute the solution matrix X.
561 CALL CLACPY( 'Full', N, NRHS, B, LDB, X, LDX )
562 CALL CGETRS( TRANS, N, NRHS, AF, LDAF, IPIV, X, LDX, INFO )
564 * Use iterative refinement to improve the computed solution and
565 * compute error bounds and backward error estimates for it.
567 CALL CGERFS( TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV, B, LDB, X,
568 $ LDX, FERR, BERR, WORK, RWORK, INFO )
570 * Transform the solution matrix X to a solution of the original
577 X( I, J ) = C( I )*X( I, J )
581 FERR( J ) = FERR( J ) / COLCND
584 ELSE IF( ROWEQU ) THEN
587 X( I, J ) = R( I )*X( I, J )
591 FERR( J ) = FERR( J ) / ROWCND
595 * Set INFO = N+1 if the matrix is singular to working precision.
597 IF( RCOND.LT.SLAMCH( 'Epsilon' ) )