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