```001:       SUBROUTINE CPOSVX( FACT, UPLO, N, NRHS, A, LDA, AF, LDAF, EQUED,
002:      \$                   S, B, LDB, X, LDX, RCOND, FERR, BERR, WORK,
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          EQUED, FACT, UPLO
012:       INTEGER            INFO, LDA, LDAF, LDB, LDX, N, NRHS
013:       REAL               RCOND
014: *     ..
015: *     .. Array Arguments ..
016:       REAL               BERR( * ), FERR( * ), RWORK( * ), S( * )
017:       COMPLEX            A( LDA, * ), AF( LDAF, * ), B( LDB, * ),
018:      \$                   WORK( * ), X( LDX, * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  CPOSVX uses the Cholesky factorization A = U**H*U or A = L*L**H to
025: *  compute the solution to a complex system of linear equations
026: *     A * X = B,
027: *  where A is an N-by-N Hermitian positive definite matrix and X and B
028: *  are N-by-NRHS 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 = 'E', real scaling factors are computed to equilibrate
039: *     the system:
040: *        diag(S) * A * diag(S) * inv(diag(S)) * X = diag(S) * B
041: *     Whether or not the system will be equilibrated depends on the
042: *     scaling of the matrix A, but if equilibration is used, A is
043: *     overwritten by diag(S)*A*diag(S) and B by diag(S)*B.
044: *
045: *  2. If FACT = 'N' or 'E', the Cholesky decomposition is used to
046: *     factor the matrix A (after equilibration if FACT = 'E') as
047: *        A = U**H* U,  if UPLO = 'U', or
048: *        A = L * L**H,  if UPLO = 'L',
049: *     where U is an upper triangular matrix and L is a lower triangular
050: *     matrix.
051: *
052: *  3. If the leading i-by-i principal minor is not positive definite,
053: *     then the routine returns with INFO = i. Otherwise, the factored
054: *     form of A is used to estimate the condition number of the matrix
055: *     A.  If the reciprocal of the condition number is less than machine
056: *     precision, INFO = N+1 is returned as a warning, but the routine
057: *     still goes on to solve for X and compute error bounds as
058: *     described below.
059: *
060: *  4. The system of equations is solved for X using the factored form
061: *     of A.
062: *
063: *  5. Iterative refinement is applied to improve the computed solution
064: *     matrix and calculate error bounds and backward error estimates
065: *     for it.
066: *
067: *  6. If equilibration was used, the matrix X is premultiplied by
068: *     diag(S) so that it solves the original system before
069: *     equilibration.
070: *
071: *  Arguments
072: *  =========
073: *
074: *  FACT    (input) CHARACTER*1
075: *          Specifies whether or not the factored form of the matrix A is
076: *          supplied on entry, and if not, whether the matrix A should be
077: *          equilibrated before it is factored.
078: *          = 'F':  On entry, AF contains the factored form of A.
079: *                  If EQUED = 'Y', the matrix A has been equilibrated
080: *                  with scaling factors given by S.  A and AF will not
081: *                  be modified.
082: *          = 'N':  The matrix A will be copied to AF and factored.
083: *          = 'E':  The matrix A will be equilibrated if necessary, then
084: *                  copied to AF and factored.
085: *
086: *  UPLO    (input) CHARACTER*1
087: *          = 'U':  Upper triangle of A is stored;
088: *          = 'L':  Lower triangle of A is stored.
089: *
090: *  N       (input) INTEGER
091: *          The number of linear equations, i.e., the order of the
092: *          matrix A.  N >= 0.
093: *
094: *  NRHS    (input) INTEGER
095: *          The number of right hand sides, i.e., the number of columns
096: *          of the matrices B and X.  NRHS >= 0.
097: *
098: *  A       (input/output) COMPLEX array, dimension (LDA,N)
099: *          On entry, the Hermitian matrix A, except if FACT = 'F' and
100: *          EQUED = 'Y', then A must contain the equilibrated matrix
101: *          diag(S)*A*diag(S).  If UPLO = 'U', the leading
102: *          N-by-N upper triangular part of A contains the upper
103: *          triangular part of the matrix A, and the strictly lower
104: *          triangular part of A is not referenced.  If UPLO = 'L', the
105: *          leading N-by-N lower triangular part of A contains the lower
106: *          triangular part of the matrix A, and the strictly upper
107: *          triangular part of A is not referenced.  A is not modified if
108: *          FACT = 'F' or 'N', or if FACT = 'E' and EQUED = 'N' on exit.
109: *
110: *          On exit, if FACT = 'E' and EQUED = 'Y', A is overwritten by
111: *          diag(S)*A*diag(S).
112: *
113: *  LDA     (input) INTEGER
114: *          The leading dimension of the array A.  LDA >= max(1,N).
115: *
116: *  AF      (input or output) COMPLEX array, dimension (LDAF,N)
117: *          If FACT = 'F', then AF is an input argument and on entry
118: *          contains the triangular factor U or L from the Cholesky
119: *          factorization A = U**H*U or A = L*L**H, in the same storage
120: *          format as A.  If EQUED .ne. 'N', then AF is the factored form
121: *          of the equilibrated matrix diag(S)*A*diag(S).
122: *
123: *          If FACT = 'N', then AF is an output argument and on exit
124: *          returns the triangular factor U or L from the Cholesky
125: *          factorization A = U**H*U or A = L*L**H of the original
126: *          matrix A.
127: *
128: *          If FACT = 'E', then AF is an output argument and on exit
129: *          returns the triangular factor U or L from the Cholesky
130: *          factorization A = U**H*U or A = L*L**H of the equilibrated
131: *          matrix A (see the description of A for the form of the
132: *          equilibrated matrix).
133: *
134: *  LDAF    (input) INTEGER
135: *          The leading dimension of the array AF.  LDAF >= max(1,N).
136: *
137: *  EQUED   (input or output) CHARACTER*1
138: *          Specifies the form of equilibration that was done.
139: *          = 'N':  No equilibration (always true if FACT = 'N').
140: *          = 'Y':  Equilibration was done, i.e., A has been replaced by
141: *                  diag(S) * A * diag(S).
142: *          EQUED is an input argument if FACT = 'F'; otherwise, it is an
143: *          output argument.
144: *
145: *  S       (input or output) REAL array, dimension (N)
146: *          The scale factors for A; not accessed if EQUED = 'N'.  S is
147: *          an input argument if FACT = 'F'; otherwise, S is an output
148: *          argument.  If FACT = 'F' and EQUED = 'Y', each element of S
149: *          must be positive.
150: *
151: *  B       (input/output) COMPLEX array, dimension (LDB,NRHS)
152: *          On entry, the N-by-NRHS righthand side matrix B.
153: *          On exit, if EQUED = 'N', B is not modified; if EQUED = 'Y',
154: *          B is overwritten by diag(S) * B.
155: *
156: *  LDB     (input) INTEGER
157: *          The leading dimension of the array B.  LDB >= max(1,N).
158: *
159: *  X       (output) COMPLEX array, dimension (LDX,NRHS)
160: *          If INFO = 0 or INFO = N+1, the N-by-NRHS solution matrix X to
161: *          the original system of equations.  Note that if EQUED = 'Y',
162: *          A and B are modified on exit, and the solution to the
163: *          equilibrated system is inv(diag(S))*X.
164: *
165: *  LDX     (input) INTEGER
166: *          The leading dimension of the array X.  LDX >= max(1,N).
167: *
168: *  RCOND   (output) REAL
169: *          The estimate of the reciprocal condition number of the matrix
170: *          A after equilibration (if done).  If RCOND is less than the
171: *          machine precision (in particular, if RCOND = 0), the matrix
172: *          is singular to working precision.  This condition is
173: *          indicated by a return code of INFO > 0.
174: *
175: *  FERR    (output) REAL array, dimension (NRHS)
176: *          The estimated forward error bound for each solution vector
177: *          X(j) (the j-th column of the solution matrix X).
178: *          If XTRUE is the true solution corresponding to X(j), FERR(j)
179: *          is an estimated upper bound for the magnitude of the largest
180: *          element in (X(j) - XTRUE) divided by the magnitude of the
181: *          largest element in X(j).  The estimate is as reliable as
182: *          the estimate for RCOND, and is almost always a slight
183: *          overestimate of the true error.
184: *
185: *  BERR    (output) REAL array, dimension (NRHS)
186: *          The componentwise relative backward error of each solution
187: *          vector X(j) (i.e., the smallest relative change in
188: *          any element of A or B that makes X(j) an exact solution).
189: *
190: *  WORK    (workspace) COMPLEX array, dimension (2*N)
191: *
192: *  RWORK   (workspace) REAL array, dimension (N)
193: *
194: *  INFO    (output) INTEGER
195: *          = 0: successful exit
196: *          < 0: if INFO = -i, the i-th argument had an illegal value
197: *          > 0: if INFO = i, and i is
198: *                <= N:  the leading minor of order i of A is
199: *                       not positive definite, so the factorization
200: *                       could not be completed, and the solution has not
201: *                       been computed. RCOND = 0 is returned.
202: *                = N+1: U is nonsingular, but RCOND is less than machine
203: *                       precision, meaning that the matrix is singular
204: *                       to working precision.  Nevertheless, the
205: *                       solution and error bounds are computed because
206: *                       there are a number of situations where the
207: *                       computed solution can be more accurate than the
208: *                       value of RCOND would suggest.
209: *
210: *  =====================================================================
211: *
212: *     .. Parameters ..
213:       REAL               ZERO, ONE
214:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
215: *     ..
216: *     .. Local Scalars ..
217:       LOGICAL            EQUIL, NOFACT, RCEQU
218:       INTEGER            I, INFEQU, J
219:       REAL               AMAX, ANORM, BIGNUM, SCOND, SMAX, SMIN, SMLNUM
220: *     ..
221: *     .. External Functions ..
222:       LOGICAL            LSAME
223:       REAL               CLANHE, SLAMCH
224:       EXTERNAL           LSAME, CLANHE, SLAMCH
225: *     ..
226: *     .. External Subroutines ..
227:       EXTERNAL           CLACPY, CLAQHE, CPOCON, CPOEQU, CPORFS, CPOTRF,
228:      \$                   CPOTRS, XERBLA
229: *     ..
230: *     .. Intrinsic Functions ..
231:       INTRINSIC          MAX, MIN
232: *     ..
233: *     .. Executable Statements ..
234: *
235:       INFO = 0
236:       NOFACT = LSAME( FACT, 'N' )
237:       EQUIL = LSAME( FACT, 'E' )
238:       IF( NOFACT .OR. EQUIL ) THEN
239:          EQUED = 'N'
240:          RCEQU = .FALSE.
241:       ELSE
242:          RCEQU = LSAME( EQUED, 'Y' )
243:          SMLNUM = SLAMCH( 'Safe minimum' )
244:          BIGNUM = ONE / SMLNUM
245:       END IF
246: *
247: *     Test the input parameters.
248: *
249:       IF( .NOT.NOFACT .AND. .NOT.EQUIL .AND. .NOT.LSAME( FACT, 'F' ) )
250:      \$     THEN
251:          INFO = -1
252:       ELSE IF( .NOT.LSAME( UPLO, 'U' ) .AND. .NOT.LSAME( UPLO, 'L' ) )
253:      \$          THEN
254:          INFO = -2
255:       ELSE IF( N.LT.0 ) THEN
256:          INFO = -3
257:       ELSE IF( NRHS.LT.0 ) THEN
258:          INFO = -4
259:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
260:          INFO = -6
261:       ELSE IF( LDAF.LT.MAX( 1, N ) ) THEN
262:          INFO = -8
263:       ELSE IF( LSAME( FACT, 'F' ) .AND. .NOT.
264:      \$         ( RCEQU .OR. LSAME( EQUED, 'N' ) ) ) THEN
265:          INFO = -9
266:       ELSE
267:          IF( RCEQU ) THEN
268:             SMIN = BIGNUM
269:             SMAX = ZERO
270:             DO 10 J = 1, N
271:                SMIN = MIN( SMIN, S( J ) )
272:                SMAX = MAX( SMAX, S( J ) )
273:    10       CONTINUE
274:             IF( SMIN.LE.ZERO ) THEN
275:                INFO = -10
276:             ELSE IF( N.GT.0 ) THEN
277:                SCOND = MAX( SMIN, SMLNUM ) / MIN( SMAX, BIGNUM )
278:             ELSE
279:                SCOND = ONE
280:             END IF
281:          END IF
282:          IF( INFO.EQ.0 ) THEN
283:             IF( LDB.LT.MAX( 1, N ) ) THEN
284:                INFO = -12
285:             ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
286:                INFO = -14
287:             END IF
288:          END IF
289:       END IF
290: *
291:       IF( INFO.NE.0 ) THEN
292:          CALL XERBLA( 'CPOSVX', -INFO )
293:          RETURN
294:       END IF
295: *
296:       IF( EQUIL ) THEN
297: *
298: *        Compute row and column scalings to equilibrate the matrix A.
299: *
300:          CALL CPOEQU( N, A, LDA, S, SCOND, AMAX, INFEQU )
301:          IF( INFEQU.EQ.0 ) THEN
302: *
303: *           Equilibrate the matrix.
304: *
305:             CALL CLAQHE( UPLO, N, A, LDA, S, SCOND, AMAX, EQUED )
306:             RCEQU = LSAME( EQUED, 'Y' )
307:          END IF
308:       END IF
309: *
310: *     Scale the right hand side.
311: *
312:       IF( RCEQU ) THEN
313:          DO 30 J = 1, NRHS
314:             DO 20 I = 1, N
315:                B( I, J ) = S( I )*B( I, J )
316:    20       CONTINUE
317:    30    CONTINUE
318:       END IF
319: *
320:       IF( NOFACT .OR. EQUIL ) THEN
321: *
322: *        Compute the Cholesky factorization A = U'*U or A = L*L'.
323: *
324:          CALL CLACPY( UPLO, N, N, A, LDA, AF, LDAF )
325:          CALL CPOTRF( UPLO, N, AF, LDAF, INFO )
326: *
327: *        Return if INFO is non-zero.
328: *
329:          IF( INFO.GT.0 )THEN
330:             RCOND = ZERO
331:             RETURN
332:          END IF
333:       END IF
334: *
335: *     Compute the norm of the matrix A.
336: *
337:       ANORM = CLANHE( '1', UPLO, N, A, LDA, RWORK )
338: *
339: *     Compute the reciprocal of the condition number of A.
340: *
341:       CALL CPOCON( UPLO, N, AF, LDAF, ANORM, RCOND, WORK, RWORK, INFO )
342: *
343: *     Compute the solution matrix X.
344: *
345:       CALL CLACPY( 'Full', N, NRHS, B, LDB, X, LDX )
346:       CALL CPOTRS( UPLO, N, NRHS, AF, LDAF, X, LDX, INFO )
347: *
348: *     Use iterative refinement to improve the computed solution and
349: *     compute error bounds and backward error estimates for it.
350: *
351:       CALL CPORFS( UPLO, N, NRHS, A, LDA, AF, LDAF, B, LDB, X, LDX,
352:      \$             FERR, BERR, WORK, RWORK, INFO )
353: *
354: *     Transform the solution matrix X to a solution of the original
355: *     system.
356: *
357:       IF( RCEQU ) THEN
358:          DO 50 J = 1, NRHS
359:             DO 40 I = 1, N
360:                X( I, J ) = S( I )*X( I, J )
361:    40       CONTINUE
362:    50    CONTINUE
363:          DO 60 J = 1, NRHS
364:             FERR( J ) = FERR( J ) / SCOND
365:    60    CONTINUE
366:       END IF
367: *
368: *     Set INFO = N+1 if the matrix is singular to working precision.
369: *
370:       IF( RCOND.LT.SLAMCH( 'Epsilon' ) )
371:      \$   INFO = N + 1
372: *
373:       RETURN
374: *
375: *     End of CPOSVX
376: *
377:       END
378: ```