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