001:       SUBROUTINE CSYSVX( FACT, UPLO, N, NRHS, A, LDA, AF, LDAF, IPIV, B,
002:      $                   LDB, X, LDX, RCOND, FERR, BERR, WORK, LWORK,
003:      $                   RWORK, INFO )
004: *
005: *  -- LAPACK driver routine (version 3.2) --
006: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
007: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
008: *     November 2006
009: *
010: *     .. Scalar Arguments ..
011:       CHARACTER          FACT, UPLO
012:       INTEGER            INFO, LDA, LDAF, LDB, LDX, LWORK, N, NRHS
013:       REAL               RCOND
014: *     ..
015: *     .. Array Arguments ..
016:       INTEGER            IPIV( * )
017:       REAL               BERR( * ), FERR( * ), RWORK( * )
018:       COMPLEX            A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
019:      $                   WORK( * ), X( LDX, * )
020: *     ..
021: *
022: *  Purpose
023: *  =======
024: *
025: *  CSYSVX uses the diagonal pivoting factorization to compute the
026: *  solution to a complex system of linear equations A * X = B,
027: *  where A is an N-by-N symmetric matrix and X and B are N-by-NRHS
028: *  matrices.
029: *
030: *  Error bounds on the solution and a condition estimate are also
031: *  provided.
032: *
033: *  Description
034: *  ===========
035: *
036: *  The following steps are performed:
037: *
038: *  1. If FACT = 'N', the diagonal pivoting method is used to factor A.
039: *     The form of the factorization is
040: *        A = U * D * U**T,  if UPLO = 'U', or
041: *        A = L * D * L**T,  if UPLO = 'L',
042: *     where U (or L) is a product of permutation and unit upper (lower)
043: *     triangular matrices, and D is symmetric and block diagonal with
044: *     1-by-1 and 2-by-2 diagonal blocks.
045: *
046: *  2. If some D(i,i)=0, so that D is exactly singular, then the routine
047: *     returns with INFO = i. Otherwise, the factored form of A is used
048: *     to estimate the condition number of the matrix A.  If the
049: *     reciprocal of the condition number is less than machine precision,
050: *     INFO = N+1 is returned as a warning, but the routine still goes on
051: *     to solve for X and compute error bounds as described below.
052: *
053: *  3. The system of equations is solved for X using the factored form
054: *     of A.
055: *
056: *  4. Iterative refinement is applied to improve the computed solution
057: *     matrix and calculate error bounds and backward error estimates
058: *     for it.
059: *
060: *  Arguments
061: *  =========
062: *
063: *  FACT    (input) CHARACTER*1
064: *          Specifies whether or not the factored form of A has been
065: *          supplied on entry.
066: *          = 'F':  On entry, AF and IPIV contain the factored form
067: *                  of A.  A, AF and IPIV will not be modified.
068: *          = 'N':  The matrix A will be copied to AF and factored.
069: *
070: *  UPLO    (input) CHARACTER*1
071: *          = 'U':  Upper triangle of A is stored;
072: *          = 'L':  Lower triangle of A is stored.
073: *
074: *  N       (input) INTEGER
075: *          The number of linear equations, i.e., the order of the
076: *          matrix A.  N >= 0.
077: *
078: *  NRHS    (input) INTEGER
079: *          The number of right hand sides, i.e., the number of columns
080: *          of the matrices B and X.  NRHS >= 0.
081: *
082: *  A       (input) COMPLEX array, dimension (LDA,N)
083: *          The symmetric matrix A.  If UPLO = 'U', the leading N-by-N
084: *          upper triangular part of A contains the upper triangular part
085: *          of the matrix A, and the strictly lower triangular part of A
086: *          is not referenced.  If UPLO = 'L', the leading N-by-N lower
087: *          triangular part of A contains the lower triangular part of
088: *          the matrix A, and the strictly upper triangular part of A is
089: *          not referenced.
090: *
091: *  LDA     (input) INTEGER
092: *          The leading dimension of the array A.  LDA >= max(1,N).
093: *
094: *  AF      (input or output) COMPLEX array, dimension (LDAF,N)
095: *          If FACT = 'F', then AF is an input argument and on entry
096: *          contains the block diagonal matrix D and the multipliers used
097: *          to obtain the factor U or L from the factorization
098: *          A = U*D*U**T or A = L*D*L**T as computed by CSYTRF.
099: *
100: *          If FACT = 'N', then AF is an output argument and on exit
101: *          returns the block diagonal matrix D and the multipliers used
102: *          to obtain the factor U or L from the factorization
103: *          A = U*D*U**T or A = L*D*L**T.
104: *
105: *  LDAF    (input) INTEGER
106: *          The leading dimension of the array AF.  LDAF >= max(1,N).
107: *
108: *  IPIV    (input or output) INTEGER array, dimension (N)
109: *          If FACT = 'F', then IPIV is an input argument and on entry
110: *          contains details of the interchanges and the block structure
111: *          of D, as determined by CSYTRF.
112: *          If IPIV(k) > 0, then rows and columns k and IPIV(k) were
113: *          interchanged and D(k,k) is a 1-by-1 diagonal block.
114: *          If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and
115: *          columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k)
116: *          is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) =
117: *          IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were
118: *          interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block.
119: *
120: *          If FACT = 'N', then IPIV is an output argument and on exit
121: *          contains details of the interchanges and the block structure
122: *          of D, as determined by CSYTRF.
123: *
124: *  B       (input) COMPLEX array, dimension (LDB,NRHS)
125: *          The N-by-NRHS right hand side matrix B.
126: *
127: *  LDB     (input) INTEGER
128: *          The leading dimension of the array B.  LDB >= max(1,N).
129: *
130: *  X       (output) COMPLEX array, dimension (LDX,NRHS)
131: *          If INFO = 0 or INFO = N+1, the N-by-NRHS solution matrix X.
132: *
133: *  LDX     (input) INTEGER
134: *          The leading dimension of the array X.  LDX >= max(1,N).
135: *
136: *  RCOND   (output) REAL
137: *          The estimate of the reciprocal condition number of the matrix
138: *          A.  If RCOND is less than the machine precision (in
139: *          particular, if RCOND = 0), the matrix is singular to working
140: *          precision.  This condition is indicated by a return code of
141: *          INFO > 0.
142: *
143: *  FERR    (output) REAL array, dimension (NRHS)
144: *          The estimated forward error bound for each solution vector
145: *          X(j) (the j-th column of the solution matrix X).
146: *          If XTRUE is the true solution corresponding to X(j), FERR(j)
147: *          is an estimated upper bound for the magnitude of the largest
148: *          element in (X(j) - XTRUE) divided by the magnitude of the
149: *          largest element in X(j).  The estimate is as reliable as
150: *          the estimate for RCOND, and is almost always a slight
151: *          overestimate of the true error.
152: *
153: *  BERR    (output) REAL array, dimension (NRHS)
154: *          The componentwise relative backward error of each solution
155: *          vector X(j) (i.e., the smallest relative change in
156: *          any element of A or B that makes X(j) an exact solution).
157: *
158: *  WORK    (workspace/output) COMPLEX array, dimension (MAX(1,LWORK))
159: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
160: *
161: *  LWORK   (input) INTEGER
162: *          The length of WORK.  LWORK >= max(1,2*N), and for best
163: *          performance, when FACT = 'N', LWORK >= max(1,2*N,N*NB), where
164: *          NB is the optimal blocksize for CSYTRF.
165: *
166: *          If LWORK = -1, then a workspace query is assumed; the routine
167: *          only calculates the optimal size of the WORK array, returns
168: *          this value as the first entry of the WORK array, and no error
169: *          message related to LWORK is issued by XERBLA.
170: *
171: *  RWORK   (workspace) REAL array, dimension (N)
172: *
173: *  INFO    (output) INTEGER
174: *          = 0: successful exit
175: *          < 0: if INFO = -i, the i-th argument had an illegal value
176: *          > 0: if INFO = i, and i is
177: *                <= N:  D(i,i) is exactly zero.  The factorization
178: *                       has been completed but the factor D is exactly
179: *                       singular, so the solution and error bounds could
180: *                       not be computed. RCOND = 0 is returned.
181: *                = N+1: D is nonsingular, but RCOND is less than machine
182: *                       precision, meaning that the matrix is singular
183: *                       to working precision.  Nevertheless, the
184: *                       solution and error bounds are computed because
185: *                       there are a number of situations where the
186: *                       computed solution can be more accurate than the
187: *                       value of RCOND would suggest.
188: *
189: *  =====================================================================
190: *
191: *     .. Parameters ..
192:       REAL               ZERO
193:       PARAMETER          ( ZERO = 0.0E+0 )
194: *     ..
195: *     .. Local Scalars ..
196:       LOGICAL            LQUERY, NOFACT
197:       INTEGER            LWKOPT, NB
198:       REAL               ANORM
199: *     ..
200: *     .. External Functions ..
201:       LOGICAL            LSAME
202:       INTEGER            ILAENV
203:       REAL               CLANSY, SLAMCH
204:       EXTERNAL           ILAENV, LSAME, CLANSY, SLAMCH
205: *     ..
206: *     .. External Subroutines ..
207:       EXTERNAL           CLACPY, CSYCON, CSYRFS, CSYTRF, CSYTRS, XERBLA
208: *     ..
209: *     .. Intrinsic Functions ..
210:       INTRINSIC          MAX
211: *     ..
212: *     .. Executable Statements ..
213: *
214: *     Test the input parameters.
215: *
216:       INFO = 0
217:       NOFACT = LSAME( FACT, 'N' )
218:       LQUERY = ( LWORK.EQ.-1 )
219:       IF( .NOT.NOFACT .AND. .NOT.LSAME( FACT, 'F' ) ) THEN
220:          INFO = -1
221:       ELSE IF( .NOT.LSAME( UPLO, 'U' ) .AND. .NOT.LSAME( UPLO, 'L' ) )
222:      $          THEN
223:          INFO = -2
224:       ELSE IF( N.LT.0 ) THEN
225:          INFO = -3
226:       ELSE IF( NRHS.LT.0 ) THEN
227:          INFO = -4
228:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
229:          INFO = -6
230:       ELSE IF( LDAF.LT.MAX( 1, N ) ) THEN
231:          INFO = -8
232:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
233:          INFO = -11
234:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
235:          INFO = -13
236:       ELSE IF( LWORK.LT.MAX( 1, 2*N ) .AND. .NOT.LQUERY ) THEN
237:          INFO = -18
238:       END IF
239: *
240:       IF( INFO.EQ.0 ) THEN
241:          LWKOPT = MAX( 1, 2*N )
242:          IF( NOFACT ) THEN
243:             NB = ILAENV( 1, 'CSYTRF', UPLO, N, -1, -1, -1 )
244:             LWKOPT = MAX( LWKOPT, N*NB )
245:          END IF
246:          WORK( 1 ) = LWKOPT
247:       END IF
248: *
249:       IF( INFO.NE.0 ) THEN
250:          CALL XERBLA( 'CSYSVX', -INFO )
251:          RETURN
252:       ELSE IF( LQUERY ) THEN
253:          RETURN
254:       END IF
255: *
256:       IF( NOFACT ) THEN
257: *
258: *        Compute the factorization A = U*D*U' or A = L*D*L'.
259: *
260:          CALL CLACPY( UPLO, N, N, A, LDA, AF, LDAF )
261:          CALL CSYTRF( UPLO, N, AF, LDAF, IPIV, WORK, LWORK, INFO )
262: *
263: *        Return if INFO is non-zero.
264: *
265:          IF( INFO.GT.0 )THEN
266:             RCOND = ZERO
267:             RETURN
268:          END IF
269:       END IF
270: *
271: *     Compute the norm of the matrix A.
272: *
273:       ANORM = CLANSY( 'I', UPLO, N, A, LDA, RWORK )
274: *
275: *     Compute the reciprocal of the condition number of A.
276: *
277:       CALL CSYCON( UPLO, N, AF, LDAF, IPIV, ANORM, RCOND, WORK, INFO )
278: *
279: *     Compute the solution vectors X.
280: *
281:       CALL CLACPY( 'Full', N, NRHS, B, LDB, X, LDX )
282:       CALL CSYTRS( UPLO, N, NRHS, AF, LDAF, IPIV, X, LDX, INFO )
283: *
284: *     Use iterative refinement to improve the computed solutions and
285: *     compute error bounds and backward error estimates for them.
286: *
287:       CALL CSYRFS( UPLO, N, NRHS, A, LDA, AF, LDAF, IPIV, B, LDB, X,
288:      $             LDX, FERR, BERR, WORK, RWORK, INFO )
289: *
290: *     Set INFO = N+1 if the matrix is singular to working precision.
291: *
292:       IF( RCOND.LT.SLAMCH( 'Epsilon' ) )
293:      $   INFO = N + 1
294: *
295:       WORK( 1 ) = LWKOPT
296: *
297:       RETURN
298: *
299: *     End of CSYSVX
300: *
301:       END
302: