001:       SUBROUTINE SGBSVX( FACT, TRANS, N, KL, KU, NRHS, AB, LDAB, AFB,
002:      $                   LDAFB, IPIV, EQUED, R, C, B, LDB, X, LDX,
003:      $                   RCOND, FERR, BERR, WORK, 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, TRANS
012:       INTEGER            INFO, KL, KU, LDAB, LDAFB, LDB, LDX, N, NRHS
013:       REAL               RCOND
014: *     ..
015: *     .. Array Arguments ..
016:       INTEGER            IPIV( * ), IWORK( * )
017:       REAL               AB( LDAB, * ), AFB( LDAFB, * ), B( LDB, * ),
018:      $                   BERR( * ), C( * ), FERR( * ), R( * ),
019:      $                   WORK( * ), X( LDX, * )
020: *     ..
021: *
022: *  Purpose
023: *  =======
024: *
025: *  SGBSVX uses the LU factorization to compute the solution to a real
026: *  system of linear equations A * X = B, A**T * X = B, or A**H * X = B,
027: *  where A is a band matrix of order N with KL subdiagonals and KU
028: *  superdiagonals, and X and B 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 by this subroutine:
037: *
038: *  1. If FACT = 'E', real scaling factors are computed to equilibrate
039: *     the system:
040: *        TRANS = 'N':  diag(R)*A*diag(C)     *inv(diag(C))*X = diag(R)*B
041: *        TRANS = 'T': (diag(R)*A*diag(C))**T *inv(diag(R))*X = diag(C)*B
042: *        TRANS = 'C': (diag(R)*A*diag(C))**H *inv(diag(R))*X = diag(C)*B
043: *     Whether or not the system will be equilibrated depends on the
044: *     scaling of the matrix A, but if equilibration is used, A is
045: *     overwritten by diag(R)*A*diag(C) and B by diag(R)*B (if TRANS='N')
046: *     or diag(C)*B (if TRANS = 'T' or 'C').
047: *
048: *  2. If FACT = 'N' or 'E', the LU decomposition is used to factor the
049: *     matrix A (after equilibration if FACT = 'E') as
050: *        A = L * U,
051: *     where L is a product of permutation and unit lower triangular
052: *     matrices with KL subdiagonals, and U is upper triangular with
053: *     KL+KU superdiagonals.
054: *
055: *  3. If some U(i,i)=0, so that U is exactly singular, then the routine
056: *     returns with INFO = i. Otherwise, the factored form of A is used
057: *     to estimate the condition number of the matrix A.  If the
058: *     reciprocal of the condition number is less than machine precision,
059: *     INFO = N+1 is returned as a warning, but the routine still goes on
060: *     to solve for X and compute error bounds as described below.
061: *
062: *  4. The system of equations is solved for X using the factored form
063: *     of A.
064: *
065: *  5. Iterative refinement is applied to improve the computed solution
066: *     matrix and calculate error bounds and backward error estimates
067: *     for it.
068: *
069: *  6. If equilibration was used, the matrix X is premultiplied by
070: *     diag(C) (if TRANS = 'N') or diag(R) (if TRANS = 'T' or 'C') so
071: *     that it solves the original system before equilibration.
072: *
073: *  Arguments
074: *  =========
075: *
076: *  FACT    (input) CHARACTER*1
077: *          Specifies whether or not the factored form of the matrix A is
078: *          supplied on entry, and if not, whether the matrix A should be
079: *          equilibrated before it is factored.
080: *          = 'F':  On entry, AFB and IPIV contain the factored form of
081: *                  A.  If EQUED is not 'N', the matrix A has been
082: *                  equilibrated with scaling factors given by R and C.
083: *                  AB, AFB, and IPIV are not modified.
084: *          = 'N':  The matrix A will be copied to AFB and factored.
085: *          = 'E':  The matrix A will be equilibrated if necessary, then
086: *                  copied to AFB and factored.
087: *
088: *  TRANS   (input) CHARACTER*1
089: *          Specifies the form of the system of equations.
090: *          = 'N':  A * X = B     (No transpose)
091: *          = 'T':  A**T * X = B  (Transpose)
092: *          = 'C':  A**H * X = B  (Transpose)
093: *
094: *  N       (input) INTEGER
095: *          The number of linear equations, i.e., the order of the
096: *          matrix A.  N >= 0.
097: *
098: *  KL      (input) INTEGER
099: *          The number of subdiagonals within the band of A.  KL >= 0.
100: *
101: *  KU      (input) INTEGER
102: *          The number of superdiagonals within the band of A.  KU >= 0.
103: *
104: *  NRHS    (input) INTEGER
105: *          The number of right hand sides, i.e., the number of columns
106: *          of the matrices B and X.  NRHS >= 0.
107: *
108: *  AB      (input/output) REAL array, dimension (LDAB,N)
109: *          On entry, the matrix A in band storage, in rows 1 to KL+KU+1.
110: *          The j-th column of A is stored in the j-th column of the
111: *          array AB as follows:
112: *          AB(KU+1+i-j,j) = A(i,j) for max(1,j-KU)<=i<=min(N,j+kl)
113: *
114: *          If FACT = 'F' and EQUED is not 'N', then A must have been
115: *          equilibrated by the scaling factors in R and/or C.  AB is not
116: *          modified if FACT = 'F' or 'N', or if FACT = 'E' and
117: *          EQUED = 'N' on exit.
118: *
119: *          On exit, if EQUED .ne. 'N', A is scaled as follows:
120: *          EQUED = 'R':  A := diag(R) * A
121: *          EQUED = 'C':  A := A * diag(C)
122: *          EQUED = 'B':  A := diag(R) * A * diag(C).
123: *
124: *  LDAB    (input) INTEGER
125: *          The leading dimension of the array AB.  LDAB >= KL+KU+1.
126: *
127: *  AFB     (input or output) REAL array, dimension (LDAFB,N)
128: *          If FACT = 'F', then AFB is an input argument and on entry
129: *          contains details of the LU factorization of the band matrix
130: *          A, as computed by SGBTRF.  U is stored as an upper triangular
131: *          band matrix with KL+KU superdiagonals in rows 1 to KL+KU+1,
132: *          and the multipliers used during the factorization are stored
133: *          in rows KL+KU+2 to 2*KL+KU+1.  If EQUED .ne. 'N', then AFB is
134: *          the factored form of the equilibrated matrix A.
135: *
136: *          If FACT = 'N', then AFB is an output argument and on exit
137: *          returns details of the LU factorization of A.
138: *
139: *          If FACT = 'E', then AFB is an output argument and on exit
140: *          returns details of the LU factorization of the equilibrated
141: *          matrix A (see the description of AB for the form of the
142: *          equilibrated matrix).
143: *
144: *  LDAFB   (input) INTEGER
145: *          The leading dimension of the array AFB.  LDAFB >= 2*KL+KU+1.
146: *
147: *  IPIV    (input or output) INTEGER array, dimension (N)
148: *          If FACT = 'F', then IPIV is an input argument and on entry
149: *          contains the pivot indices from the factorization A = L*U
150: *          as computed by SGBTRF; row i of the matrix was interchanged
151: *          with row IPIV(i).
152: *
153: *          If FACT = 'N', then IPIV is an output argument and on exit
154: *          contains the pivot indices from the factorization A = L*U
155: *          of the original matrix A.
156: *
157: *          If FACT = 'E', then IPIV is an output argument and on exit
158: *          contains the pivot indices from the factorization A = L*U
159: *          of the equilibrated matrix A.
160: *
161: *  EQUED   (input or output) CHARACTER*1
162: *          Specifies the form of equilibration that was done.
163: *          = 'N':  No equilibration (always true if FACT = 'N').
164: *          = 'R':  Row equilibration, i.e., A has been premultiplied by
165: *                  diag(R).
166: *          = 'C':  Column equilibration, i.e., A has been postmultiplied
167: *                  by diag(C).
168: *          = 'B':  Both row and column equilibration, i.e., A has been
169: *                  replaced by diag(R) * A * diag(C).
170: *          EQUED is an input argument if FACT = 'F'; otherwise, it is an
171: *          output argument.
172: *
173: *  R       (input or output) REAL array, dimension (N)
174: *          The row scale factors for A.  If EQUED = 'R' or 'B', A is
175: *          multiplied on the left by diag(R); if EQUED = 'N' or 'C', R
176: *          is not accessed.  R is an input argument if FACT = 'F';
177: *          otherwise, R is an output argument.  If FACT = 'F' and
178: *          EQUED = 'R' or 'B', each element of R must be positive.
179: *
180: *  C       (input or output) REAL array, dimension (N)
181: *          The column scale factors for A.  If EQUED = 'C' or 'B', A is
182: *          multiplied on the right by diag(C); if EQUED = 'N' or 'R', C
183: *          is not accessed.  C is an input argument if FACT = 'F';
184: *          otherwise, C is an output argument.  If FACT = 'F' and
185: *          EQUED = 'C' or 'B', each element of C must be positive.
186: *
187: *  B       (input/output) REAL array, dimension (LDB,NRHS)
188: *          On entry, the right hand side matrix B.
189: *          On exit,
190: *          if EQUED = 'N', B is not modified;
191: *          if TRANS = 'N' and EQUED = 'R' or 'B', B is overwritten by
192: *          diag(R)*B;
193: *          if TRANS = 'T' or 'C' and EQUED = 'C' or 'B', B is
194: *          overwritten by diag(C)*B.
195: *
196: *  LDB     (input) INTEGER
197: *          The leading dimension of the array B.  LDB >= max(1,N).
198: *
199: *  X       (output) REAL array, dimension (LDX,NRHS)
200: *          If INFO = 0 or INFO = N+1, the N-by-NRHS solution matrix X
201: *          to the original system of equations.  Note that A and B are
202: *          modified on exit if EQUED .ne. 'N', and the solution to the
203: *          equilibrated system is inv(diag(C))*X if TRANS = 'N' and
204: *          EQUED = 'C' or 'B', or inv(diag(R))*X if TRANS = 'T' or 'C'
205: *          and EQUED = 'R' or 'B'.
206: *
207: *  LDX     (input) INTEGER
208: *          The leading dimension of the array X.  LDX >= max(1,N).
209: *
210: *  RCOND   (output) REAL
211: *          The estimate of the reciprocal condition number of the matrix
212: *          A after equilibration (if done).  If RCOND is less than the
213: *          machine precision (in particular, if RCOND = 0), the matrix
214: *          is singular to working precision.  This condition is
215: *          indicated by a return code of INFO > 0.
216: *
217: *  FERR    (output) REAL array, dimension (NRHS)
218: *          The estimated forward error bound for each solution vector
219: *          X(j) (the j-th column of the solution matrix X).
220: *          If XTRUE is the true solution corresponding to X(j), FERR(j)
221: *          is an estimated upper bound for the magnitude of the largest
222: *          element in (X(j) - XTRUE) divided by the magnitude of the
223: *          largest element in X(j).  The estimate is as reliable as
224: *          the estimate for RCOND, and is almost always a slight
225: *          overestimate of the true error.
226: *
227: *  BERR    (output) REAL array, dimension (NRHS)
228: *          The componentwise relative backward error of each solution
229: *          vector X(j) (i.e., the smallest relative change in
230: *          any element of A or B that makes X(j) an exact solution).
231: *
232: *  WORK    (workspace/output) REAL array, dimension (3*N)
233: *          On exit, WORK(1) contains the reciprocal pivot growth
234: *          factor norm(A)/norm(U). The "max absolute element" norm is
235: *          used. If WORK(1) is much less than 1, then the stability
236: *          of the LU factorization of the (equilibrated) matrix A
237: *          could be poor. This also means that the solution X, condition
238: *          estimator RCOND, and forward error bound FERR could be
239: *          unreliable. If factorization fails with 0<INFO<=N, then
240: *          WORK(1) contains the reciprocal pivot growth factor for the
241: *          leading INFO columns of A.
242: *
243: *  IWORK   (workspace) INTEGER array, dimension (N)
244: *
245: *  INFO    (output) INTEGER
246: *          = 0:  successful exit
247: *          < 0:  if INFO = -i, the i-th argument had an illegal value
248: *          > 0:  if INFO = i, and i is
249: *                <= N:  U(i,i) is exactly zero.  The factorization
250: *                       has been completed, but the factor U is exactly
251: *                       singular, so the solution and error bounds
252: *                       could not be computed. RCOND = 0 is returned.
253: *                = N+1: U is nonsingular, but RCOND is less than machine
254: *                       precision, meaning that the matrix is singular
255: *                       to working precision.  Nevertheless, the
256: *                       solution and error bounds are computed because
257: *                       there are a number of situations where the
258: *                       computed solution can be more accurate than the
259: *
260: *                       value of RCOND would suggest.
261: *  =====================================================================
262: *  Moved setting of INFO = N+1 so INFO does not subsequently get
263: *  overwritten.  Sven, 17 Mar 05. 
264: *  =====================================================================
265: *
266: *     .. Parameters ..
267:       REAL               ZERO, ONE
268:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
269: *     ..
270: *     .. Local Scalars ..
271:       LOGICAL            COLEQU, EQUIL, NOFACT, NOTRAN, ROWEQU
272:       CHARACTER          NORM
273:       INTEGER            I, INFEQU, J, J1, J2
274:       REAL               AMAX, ANORM, BIGNUM, COLCND, RCMAX, RCMIN,
275:      $                   ROWCND, RPVGRW, SMLNUM
276: *     ..
277: *     .. External Functions ..
278:       LOGICAL            LSAME
279:       REAL               SLAMCH, SLANGB, SLANTB
280:       EXTERNAL           LSAME, SLAMCH, SLANGB, SLANTB
281: *     ..
282: *     .. External Subroutines ..
283:       EXTERNAL           SCOPY, SGBCON, SGBEQU, SGBRFS, SGBTRF, SGBTRS,
284:      $                   SLACPY, SLAQGB, XERBLA
285: *     ..
286: *     .. Intrinsic Functions ..
287:       INTRINSIC          ABS, MAX, MIN
288: *     ..
289: *     .. Executable Statements ..
290: *
291:       INFO = 0
292:       NOFACT = LSAME( FACT, 'N' )
293:       EQUIL = LSAME( FACT, 'E' )
294:       NOTRAN = LSAME( TRANS, 'N' )
295:       IF( NOFACT .OR. EQUIL ) THEN
296:          EQUED = 'N'
297:          ROWEQU = .FALSE.
298:          COLEQU = .FALSE.
299:       ELSE
300:          ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
301:          COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
302:          SMLNUM = SLAMCH( 'Safe minimum' )
303:          BIGNUM = ONE / SMLNUM
304:       END IF
305: *
306: *     Test the input parameters.
307: *
308:       IF( .NOT.NOFACT .AND. .NOT.EQUIL .AND. .NOT.LSAME( FACT, 'F' ) )
309:      $     THEN
310:          INFO = -1
311:       ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
312:      $         LSAME( TRANS, 'C' ) ) THEN
313:          INFO = -2
314:       ELSE IF( N.LT.0 ) THEN
315:          INFO = -3
316:       ELSE IF( KL.LT.0 ) THEN
317:          INFO = -4
318:       ELSE IF( KU.LT.0 ) THEN
319:          INFO = -5
320:       ELSE IF( NRHS.LT.0 ) THEN
321:          INFO = -6
322:       ELSE IF( LDAB.LT.KL+KU+1 ) THEN
323:          INFO = -8
324:       ELSE IF( LDAFB.LT.2*KL+KU+1 ) THEN
325:          INFO = -10
326:       ELSE IF( LSAME( FACT, 'F' ) .AND. .NOT.
327:      $         ( ROWEQU .OR. COLEQU .OR. LSAME( EQUED, 'N' ) ) ) THEN
328:          INFO = -12
329:       ELSE
330:          IF( ROWEQU ) THEN
331:             RCMIN = BIGNUM
332:             RCMAX = ZERO
333:             DO 10 J = 1, N
334:                RCMIN = MIN( RCMIN, R( J ) )
335:                RCMAX = MAX( RCMAX, R( J ) )
336:    10       CONTINUE
337:             IF( RCMIN.LE.ZERO ) THEN
338:                INFO = -13
339:             ELSE IF( N.GT.0 ) THEN
340:                ROWCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
341:             ELSE
342:                ROWCND = ONE
343:             END IF
344:          END IF
345:          IF( COLEQU .AND. INFO.EQ.0 ) THEN
346:             RCMIN = BIGNUM
347:             RCMAX = ZERO
348:             DO 20 J = 1, N
349:                RCMIN = MIN( RCMIN, C( J ) )
350:                RCMAX = MAX( RCMAX, C( J ) )
351:    20       CONTINUE
352:             IF( RCMIN.LE.ZERO ) THEN
353:                INFO = -14
354:             ELSE IF( N.GT.0 ) THEN
355:                COLCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
356:             ELSE
357:                COLCND = ONE
358:             END IF
359:          END IF
360:          IF( INFO.EQ.0 ) THEN
361:             IF( LDB.LT.MAX( 1, N ) ) THEN
362:                INFO = -16
363:             ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
364:                INFO = -18
365:             END IF
366:          END IF
367:       END IF
368: *
369:       IF( INFO.NE.0 ) THEN
370:          CALL XERBLA( 'SGBSVX', -INFO )
371:          RETURN
372:       END IF
373: *
374:       IF( EQUIL ) THEN
375: *
376: *        Compute row and column scalings to equilibrate the matrix A.
377: *
378:          CALL SGBEQU( N, N, KL, KU, AB, LDAB, R, C, ROWCND, COLCND,
379:      $                AMAX, INFEQU )
380:          IF( INFEQU.EQ.0 ) THEN
381: *
382: *           Equilibrate the matrix.
383: *
384:             CALL SLAQGB( N, N, KL, KU, AB, LDAB, R, C, ROWCND, COLCND,
385:      $                   AMAX, EQUED )
386:             ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
387:             COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
388:          END IF
389:       END IF
390: *
391: *     Scale the right hand side.
392: *
393:       IF( NOTRAN ) THEN
394:          IF( ROWEQU ) THEN
395:             DO 40 J = 1, NRHS
396:                DO 30 I = 1, N
397:                   B( I, J ) = R( I )*B( I, J )
398:    30          CONTINUE
399:    40       CONTINUE
400:          END IF
401:       ELSE IF( COLEQU ) THEN
402:          DO 60 J = 1, NRHS
403:             DO 50 I = 1, N
404:                B( I, J ) = C( I )*B( I, J )
405:    50       CONTINUE
406:    60    CONTINUE
407:       END IF
408: *
409:       IF( NOFACT .OR. EQUIL ) THEN
410: *
411: *        Compute the LU factorization of the band matrix A.
412: *
413:          DO 70 J = 1, N
414:             J1 = MAX( J-KU, 1 )
415:             J2 = MIN( J+KL, N )
416:             CALL SCOPY( J2-J1+1, AB( KU+1-J+J1, J ), 1,
417:      $                  AFB( KL+KU+1-J+J1, J ), 1 )
418:    70    CONTINUE
419: *
420:          CALL SGBTRF( N, N, KL, KU, AFB, LDAFB, IPIV, INFO )
421: *
422: *        Return if INFO is non-zero.
423: *
424:          IF( INFO.GT.0 ) THEN
425: *
426: *           Compute the reciprocal pivot growth factor of the
427: *           leading rank-deficient INFO columns of A.
428: *
429:             ANORM = ZERO
430:             DO 90 J = 1, INFO
431:                DO 80 I = MAX( KU+2-J, 1 ), MIN( N+KU+1-J, KL+KU+1 )
432:                   ANORM = MAX( ANORM, ABS( AB( I, J ) ) )
433:    80          CONTINUE
434:    90       CONTINUE
435:             RPVGRW = SLANTB( 'M', 'U', 'N', INFO, MIN( INFO-1, KL+KU ),
436:      $                       AFB( MAX( 1, KL+KU+2-INFO ), 1 ), LDAFB,
437:      $                       WORK )
438:             IF( RPVGRW.EQ.ZERO ) THEN
439:                RPVGRW = ONE
440:             ELSE
441:                RPVGRW = ANORM / RPVGRW
442:             END IF
443:             WORK( 1 ) = RPVGRW
444:             RCOND = ZERO
445:             RETURN
446:          END IF
447:       END IF
448: *
449: *     Compute the norm of the matrix A and the
450: *     reciprocal pivot growth factor RPVGRW.
451: *
452:       IF( NOTRAN ) THEN
453:          NORM = '1'
454:       ELSE
455:          NORM = 'I'
456:       END IF
457:       ANORM = SLANGB( NORM, N, KL, KU, AB, LDAB, WORK )
458:       RPVGRW = SLANTB( 'M', 'U', 'N', N, KL+KU, AFB, LDAFB, WORK )
459:       IF( RPVGRW.EQ.ZERO ) THEN
460:          RPVGRW = ONE
461:       ELSE
462:          RPVGRW = SLANGB( 'M', N, KL, KU, AB, LDAB, WORK ) / RPVGRW
463:       END IF
464: *
465: *     Compute the reciprocal of the condition number of A.
466: *
467:       CALL SGBCON( NORM, N, KL, KU, AFB, LDAFB, IPIV, ANORM, RCOND,
468:      $             WORK, IWORK, INFO )
469: *
470: *     Compute the solution matrix X.
471: *
472:       CALL SLACPY( 'Full', N, NRHS, B, LDB, X, LDX )
473:       CALL SGBTRS( TRANS, N, KL, KU, NRHS, AFB, LDAFB, IPIV, X, LDX,
474:      $             INFO )
475: *
476: *     Use iterative refinement to improve the computed solution and
477: *     compute error bounds and backward error estimates for it.
478: *
479:       CALL SGBRFS( TRANS, N, KL, KU, NRHS, AB, LDAB, AFB, LDAFB, IPIV,
480:      $             B, LDB, X, LDX, FERR, BERR, WORK, IWORK, INFO )
481: *
482: *     Transform the solution matrix X to a solution of the original
483: *     system.
484: *
485:       IF( NOTRAN ) THEN
486:          IF( COLEQU ) THEN
487:             DO 110 J = 1, NRHS
488:                DO 100 I = 1, N
489:                   X( I, J ) = C( I )*X( I, J )
490:   100          CONTINUE
491:   110       CONTINUE
492:             DO 120 J = 1, NRHS
493:                FERR( J ) = FERR( J ) / COLCND
494:   120       CONTINUE
495:          END IF
496:       ELSE IF( ROWEQU ) THEN
497:          DO 140 J = 1, NRHS
498:             DO 130 I = 1, N
499:                X( I, J ) = R( I )*X( I, J )
500:   130       CONTINUE
501:   140    CONTINUE
502:          DO 150 J = 1, NRHS
503:             FERR( J ) = FERR( J ) / ROWCND
504:   150    CONTINUE
505:       END IF
506: *
507: *     Set INFO = N+1 if the matrix is singular to working precision.
508: *
509:       IF( RCOND.LT.SLAMCH( 'Epsilon' ) )
510:      $   INFO = N + 1
511: *
512:       WORK( 1 ) = RPVGRW
513:       RETURN
514: *
515: *     End of SGBSVX
516: *
517:       END
518: