001:       SUBROUTINE SGTSVX( FACT, TRANS, N, NRHS, DL, D, DU, DLF, DF, DUF,
002:      $                   DU2, IPIV, B, LDB, X, LDX, RCOND, FERR, BERR,
003:      $                   WORK, IWORK, INFO )
004: *
005: *  -- LAPACK routine (version 3.2) --
006: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       CHARACTER          FACT, TRANS
011:       INTEGER            INFO, LDB, LDX, N, NRHS
012:       REAL               RCOND
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IPIV( * ), IWORK( * )
016:       REAL               B( LDB, * ), BERR( * ), D( * ), DF( * ),
017:      $                   DL( * ), DLF( * ), DU( * ), DU2( * ), DUF( * ),
018:      $                   FERR( * ), WORK( * ), X( LDX, * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  SGTSVX uses the LU factorization to compute the solution to a real
025: *  system of linear equations A * X = B or A**T * X = B,
026: *  where A is a tridiagonal matrix of order N and X and B are N-by-NRHS
027: *  matrices.
028: *
029: *  Error bounds on the solution and a condition estimate are also
030: *  provided.
031: *
032: *  Description
033: *  ===========
034: *
035: *  The following steps are performed:
036: *
037: *  1. If FACT = 'N', the LU decomposition is used to factor the matrix A
038: *     as A = L * U, where L is a product of permutation and unit lower
039: *     bidiagonal matrices and U is upper triangular with nonzeros in
040: *     only the main diagonal and first two superdiagonals.
041: *
042: *  2. If some U(i,i)=0, so that U is exactly singular, then the routine
043: *     returns with INFO = i. Otherwise, the factored form of A is used
044: *     to estimate the condition number of the matrix A.  If the
045: *     reciprocal of the condition number is less than machine precision,
046: *     INFO = N+1 is returned as a warning, but the routine still goes on
047: *     to solve for X and compute error bounds as described below.
048: *
049: *  3. The system of equations is solved for X using the factored form
050: *     of A.
051: *
052: *  4. Iterative refinement is applied to improve the computed solution
053: *     matrix and calculate error bounds and backward error estimates
054: *     for it.
055: *
056: *  Arguments
057: *  =========
058: *
059: *  FACT    (input) CHARACTER*1
060: *          Specifies whether or not the factored form of A has been
061: *          supplied on entry.
062: *          = 'F':  DLF, DF, DUF, DU2, and IPIV contain the factored
063: *                  form of A; DL, D, DU, DLF, DF, DUF, DU2 and IPIV
064: *                  will not be modified.
065: *          = 'N':  The matrix will be copied to DLF, DF, and DUF
066: *                  and factored.
067: *
068: *  TRANS   (input) CHARACTER*1
069: *          Specifies the form of the system of equations:
070: *          = 'N':  A * X = B     (No transpose)
071: *          = 'T':  A**T * X = B  (Transpose)
072: *          = 'C':  A**H * X = B  (Conjugate transpose = Transpose)
073: *
074: *  N       (input) INTEGER
075: *          The order of the matrix A.  N >= 0.
076: *
077: *  NRHS    (input) INTEGER
078: *          The number of right hand sides, i.e., the number of columns
079: *          of the matrix B.  NRHS >= 0.
080: *
081: *  DL      (input) REAL array, dimension (N-1)
082: *          The (n-1) subdiagonal elements of A.
083: *
084: *  D       (input) REAL array, dimension (N)
085: *          The n diagonal elements of A.
086: *
087: *  DU      (input) REAL array, dimension (N-1)
088: *          The (n-1) superdiagonal elements of A.
089: *
090: *  DLF     (input or output) REAL array, dimension (N-1)
091: *          If FACT = 'F', then DLF is an input argument and on entry
092: *          contains the (n-1) multipliers that define the matrix L from
093: *          the LU factorization of A as computed by SGTTRF.
094: *
095: *          If FACT = 'N', then DLF is an output argument and on exit
096: *          contains the (n-1) multipliers that define the matrix L from
097: *          the LU factorization of A.
098: *
099: *  DF      (input or output) REAL array, dimension (N)
100: *          If FACT = 'F', then DF is an input argument and on entry
101: *          contains the n diagonal elements of the upper triangular
102: *          matrix U from the LU factorization of A.
103: *
104: *          If FACT = 'N', then DF is an output argument and on exit
105: *          contains the n diagonal elements of the upper triangular
106: *          matrix U from the LU factorization of A.
107: *
108: *  DUF     (input or output) REAL array, dimension (N-1)
109: *          If FACT = 'F', then DUF is an input argument and on entry
110: *          contains the (n-1) elements of the first superdiagonal of U.
111: *
112: *          If FACT = 'N', then DUF is an output argument and on exit
113: *          contains the (n-1) elements of the first superdiagonal of U.
114: *
115: *  DU2     (input or output) REAL array, dimension (N-2)
116: *          If FACT = 'F', then DU2 is an input argument and on entry
117: *          contains the (n-2) elements of the second superdiagonal of
118: *          U.
119: *
120: *          If FACT = 'N', then DU2 is an output argument and on exit
121: *          contains the (n-2) elements of the second superdiagonal of
122: *          U.
123: *
124: *  IPIV    (input or output) INTEGER array, dimension (N)
125: *          If FACT = 'F', then IPIV is an input argument and on entry
126: *          contains the pivot indices from the LU factorization of A as
127: *          computed by SGTTRF.
128: *
129: *          If FACT = 'N', then IPIV is an output argument and on exit
130: *          contains the pivot indices from the LU factorization of A;
131: *          row i of the matrix was interchanged with row IPIV(i).
132: *          IPIV(i) will always be either i or i+1; IPIV(i) = i indicates
133: *          a row interchange was not required.
134: *
135: *  B       (input) REAL array, dimension (LDB,NRHS)
136: *          The N-by-NRHS right hand side matrix B.
137: *
138: *  LDB     (input) INTEGER
139: *          The leading dimension of the array B.  LDB >= max(1,N).
140: *
141: *  X       (output) REAL array, dimension (LDX,NRHS)
142: *          If INFO = 0 or INFO = N+1, the N-by-NRHS solution matrix X.
143: *
144: *  LDX     (input) INTEGER
145: *          The leading dimension of the array X.  LDX >= max(1,N).
146: *
147: *  RCOND   (output) REAL
148: *          The estimate of the reciprocal condition number of the matrix
149: *          A.  If RCOND is less than the machine precision (in
150: *          particular, if RCOND = 0), the matrix is singular to working
151: *          precision.  This condition is indicated by a return code of
152: *          INFO > 0.
153: *
154: *  FERR    (output) REAL array, dimension (NRHS)
155: *          The estimated forward error bound for each solution vector
156: *          X(j) (the j-th column of the solution matrix X).
157: *          If XTRUE is the true solution corresponding to X(j), FERR(j)
158: *          is an estimated upper bound for the magnitude of the largest
159: *          element in (X(j) - XTRUE) divided by the magnitude of the
160: *          largest element in X(j).  The estimate is as reliable as
161: *          the estimate for RCOND, and is almost always a slight
162: *          overestimate of the true error.
163: *
164: *  BERR    (output) REAL array, dimension (NRHS)
165: *          The componentwise relative backward error of each solution
166: *          vector X(j) (i.e., the smallest relative change in
167: *          any element of A or B that makes X(j) an exact solution).
168: *
169: *  WORK    (workspace) REAL array, dimension (3*N)
170: *
171: *  IWORK   (workspace) INTEGER 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:  U(i,i) is exactly zero.  The factorization
178: *                       has not been completed unless i = N, but the
179: *                       factor U is exactly singular, so the solution
180: *                       and error bounds could not be computed.
181: *                       RCOND = 0 is returned.
182: *                = N+1: U is nonsingular, but RCOND is less than machine
183: *                       precision, meaning that the matrix is singular
184: *                       to working precision.  Nevertheless, the
185: *                       solution and error bounds are computed because
186: *                       there are a number of situations where the
187: *                       computed solution can be more accurate than the
188: *                       value of RCOND would suggest.
189: *
190: *  =====================================================================
191: *
192: *     .. Parameters ..
193:       REAL               ZERO
194:       PARAMETER          ( ZERO = 0.0E+0 )
195: *     ..
196: *     .. Local Scalars ..
197:       LOGICAL            NOFACT, NOTRAN
198:       CHARACTER          NORM
199:       REAL               ANORM
200: *     ..
201: *     .. External Functions ..
202:       LOGICAL            LSAME
203:       REAL               SLAMCH, SLANGT
204:       EXTERNAL           LSAME, SLAMCH, SLANGT
205: *     ..
206: *     .. External Subroutines ..
207:       EXTERNAL           SCOPY, SGTCON, SGTRFS, SGTTRF, SGTTRS, SLACPY,
208:      $                   XERBLA
209: *     ..
210: *     .. Intrinsic Functions ..
211:       INTRINSIC          MAX
212: *     ..
213: *     .. Executable Statements ..
214: *
215:       INFO = 0
216:       NOFACT = LSAME( FACT, 'N' )
217:       NOTRAN = LSAME( TRANS, 'N' )
218:       IF( .NOT.NOFACT .AND. .NOT.LSAME( FACT, 'F' ) ) THEN
219:          INFO = -1
220:       ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
221:      $         LSAME( TRANS, 'C' ) ) THEN
222:          INFO = -2
223:       ELSE IF( N.LT.0 ) THEN
224:          INFO = -3
225:       ELSE IF( NRHS.LT.0 ) THEN
226:          INFO = -4
227:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
228:          INFO = -14
229:       ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
230:          INFO = -16
231:       END IF
232:       IF( INFO.NE.0 ) THEN
233:          CALL XERBLA( 'SGTSVX', -INFO )
234:          RETURN
235:       END IF
236: *
237:       IF( NOFACT ) THEN
238: *
239: *        Compute the LU factorization of A.
240: *
241:          CALL SCOPY( N, D, 1, DF, 1 )
242:          IF( N.GT.1 ) THEN
243:             CALL SCOPY( N-1, DL, 1, DLF, 1 )
244:             CALL SCOPY( N-1, DU, 1, DUF, 1 )
245:          END IF
246:          CALL SGTTRF( N, DLF, DF, DUF, DU2, IPIV, INFO )
247: *
248: *        Return if INFO is non-zero.
249: *
250:          IF( INFO.GT.0 )THEN
251:             RCOND = ZERO
252:             RETURN
253:          END IF
254:       END IF
255: *
256: *     Compute the norm of the matrix A.
257: *
258:       IF( NOTRAN ) THEN
259:          NORM = '1'
260:       ELSE
261:          NORM = 'I'
262:       END IF
263:       ANORM = SLANGT( NORM, N, DL, D, DU )
264: *
265: *     Compute the reciprocal of the condition number of A.
266: *
267:       CALL SGTCON( NORM, N, DLF, DF, DUF, DU2, IPIV, ANORM, RCOND, WORK,
268:      $             IWORK, INFO )
269: *
270: *     Compute the solution vectors X.
271: *
272:       CALL SLACPY( 'Full', N, NRHS, B, LDB, X, LDX )
273:       CALL SGTTRS( TRANS, N, NRHS, DLF, DF, DUF, DU2, IPIV, X, LDX,
274:      $             INFO )
275: *
276: *     Use iterative refinement to improve the computed solutions and
277: *     compute error bounds and backward error estimates for them.
278: *
279:       CALL SGTRFS( TRANS, N, NRHS, DL, D, DU, DLF, DF, DUF, DU2, IPIV,
280:      $             B, LDB, X, LDX, FERR, BERR, WORK, IWORK, INFO )
281: *
282: *     Set INFO = N+1 if the matrix is singular to working precision.
283: *
284:       IF( RCOND.LT.SLAMCH( 'Epsilon' ) )
285:      $   INFO = N + 1
286: *
287:       RETURN
288: *
289: *     End of SGTSVX
290: *
291:       END
292: