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