001:       SUBROUTINE DGBSVX( 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:       DOUBLE PRECISION   RCOND
013: *     ..
014: *     .. Array Arguments ..
015:       INTEGER            IPIV( * ), IWORK( * )
016:       DOUBLE PRECISION   AB( LDAB, * ), AFB( LDAFB, * ), B( LDB, * ),
017:      $                   BERR( * ), C( * ), FERR( * ), R( * ),
018:      $                   WORK( * ), X( LDX, * )
019: *     ..
020: *
021: *  Purpose
022: *  =======
023: *
024: *  DGBSVX 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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 DGBTRF.  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 DGBTRF; 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION
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) DOUBLE PRECISION 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) DOUBLE PRECISION 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) DOUBLE PRECISION 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: *                       value of RCOND would suggest.
259: *
260: *  =====================================================================
261: *
262: *     .. Parameters ..
263:       DOUBLE PRECISION   ZERO, ONE
264:       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+0 )
265: *     ..
266: *     .. Local Scalars ..
267:       LOGICAL            COLEQU, EQUIL, NOFACT, NOTRAN, ROWEQU
268:       CHARACTER          NORM
269:       INTEGER            I, INFEQU, J, J1, J2
270:       DOUBLE PRECISION   AMAX, ANORM, BIGNUM, COLCND, RCMAX, RCMIN,
271:      $                   ROWCND, RPVGRW, SMLNUM
272: *     ..
273: *     .. External Functions ..
274:       LOGICAL            LSAME
275:       DOUBLE PRECISION   DLAMCH, DLANGB, DLANTB
276:       EXTERNAL           LSAME, DLAMCH, DLANGB, DLANTB
277: *     ..
278: *     .. External Subroutines ..
279:       EXTERNAL           DCOPY, DGBCON, DGBEQU, DGBRFS, DGBTRF, DGBTRS,
280:      $                   DLACPY, DLAQGB, XERBLA
281: *     ..
282: *     .. Intrinsic Functions ..
283:       INTRINSIC          ABS, MAX, MIN
284: *     ..
285: *     .. Executable Statements ..
286: *
287:       INFO = 0
288:       NOFACT = LSAME( FACT, 'N' )
289:       EQUIL = LSAME( FACT, 'E' )
290:       NOTRAN = LSAME( TRANS, 'N' )
291:       IF( NOFACT .OR. EQUIL ) THEN
292:          EQUED = 'N'
293:          ROWEQU = .FALSE.
294:          COLEQU = .FALSE.
295:       ELSE
296:          ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
297:          COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
298:          SMLNUM = DLAMCH( 'Safe minimum' )
299:          BIGNUM = ONE / SMLNUM
300:       END IF
301: *
302: *     Test the input parameters.
303: *
304:       IF( .NOT.NOFACT .AND. .NOT.EQUIL .AND. .NOT.LSAME( FACT, 'F' ) )
305:      $     THEN
306:          INFO = -1
307:       ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
308:      $         LSAME( TRANS, 'C' ) ) THEN
309:          INFO = -2
310:       ELSE IF( N.LT.0 ) THEN
311:          INFO = -3
312:       ELSE IF( KL.LT.0 ) THEN
313:          INFO = -4
314:       ELSE IF( KU.LT.0 ) THEN
315:          INFO = -5
316:       ELSE IF( NRHS.LT.0 ) THEN
317:          INFO = -6
318:       ELSE IF( LDAB.LT.KL+KU+1 ) THEN
319:          INFO = -8
320:       ELSE IF( LDAFB.LT.2*KL+KU+1 ) THEN
321:          INFO = -10
322:       ELSE IF( LSAME( FACT, 'F' ) .AND. .NOT.
323:      $         ( ROWEQU .OR. COLEQU .OR. LSAME( EQUED, 'N' ) ) ) THEN
324:          INFO = -12
325:       ELSE
326:          IF( ROWEQU ) THEN
327:             RCMIN = BIGNUM
328:             RCMAX = ZERO
329:             DO 10 J = 1, N
330:                RCMIN = MIN( RCMIN, R( J ) )
331:                RCMAX = MAX( RCMAX, R( J ) )
332:    10       CONTINUE
333:             IF( RCMIN.LE.ZERO ) THEN
334:                INFO = -13
335:             ELSE IF( N.GT.0 ) THEN
336:                ROWCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
337:             ELSE
338:                ROWCND = ONE
339:             END IF
340:          END IF
341:          IF( COLEQU .AND. INFO.EQ.0 ) THEN
342:             RCMIN = BIGNUM
343:             RCMAX = ZERO
344:             DO 20 J = 1, N
345:                RCMIN = MIN( RCMIN, C( J ) )
346:                RCMAX = MAX( RCMAX, C( J ) )
347:    20       CONTINUE
348:             IF( RCMIN.LE.ZERO ) THEN
349:                INFO = -14
350:             ELSE IF( N.GT.0 ) THEN
351:                COLCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
352:             ELSE
353:                COLCND = ONE
354:             END IF
355:          END IF
356:          IF( INFO.EQ.0 ) THEN
357:             IF( LDB.LT.MAX( 1, N ) ) THEN
358:                INFO = -16
359:             ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
360:                INFO = -18
361:             END IF
362:          END IF
363:       END IF
364: *
365:       IF( INFO.NE.0 ) THEN
366:          CALL XERBLA( 'DGBSVX', -INFO )
367:          RETURN
368:       END IF
369: *
370:       IF( EQUIL ) THEN
371: *
372: *        Compute row and column scalings to equilibrate the matrix A.
373: *
374:          CALL DGBEQU( N, N, KL, KU, AB, LDAB, R, C, ROWCND, COLCND,
375:      $                AMAX, INFEQU )
376:          IF( INFEQU.EQ.0 ) THEN
377: *
378: *           Equilibrate the matrix.
379: *
380:             CALL DLAQGB( N, N, KL, KU, AB, LDAB, R, C, ROWCND, COLCND,
381:      $                   AMAX, EQUED )
382:             ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
383:             COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
384:          END IF
385:       END IF
386: *
387: *     Scale the right hand side.
388: *
389:       IF( NOTRAN ) THEN
390:          IF( ROWEQU ) THEN
391:             DO 40 J = 1, NRHS
392:                DO 30 I = 1, N
393:                   B( I, J ) = R( I )*B( I, J )
394:    30          CONTINUE
395:    40       CONTINUE
396:          END IF
397:       ELSE IF( COLEQU ) THEN
398:          DO 60 J = 1, NRHS
399:             DO 50 I = 1, N
400:                B( I, J ) = C( I )*B( I, J )
401:    50       CONTINUE
402:    60    CONTINUE
403:       END IF
404: *
405:       IF( NOFACT .OR. EQUIL ) THEN
406: *
407: *        Compute the LU factorization of the band matrix A.
408: *
409:          DO 70 J = 1, N
410:             J1 = MAX( J-KU, 1 )
411:             J2 = MIN( J+KL, N )
412:             CALL DCOPY( J2-J1+1, AB( KU+1-J+J1, J ), 1,
413:      $                  AFB( KL+KU+1-J+J1, J ), 1 )
414:    70    CONTINUE
415: *
416:          CALL DGBTRF( N, N, KL, KU, AFB, LDAFB, IPIV, INFO )
417: *
418: *        Return if INFO is non-zero.
419: *
420:          IF( INFO.GT.0 ) THEN
421: *
422: *           Compute the reciprocal pivot growth factor of the
423: *           leading rank-deficient INFO columns of A.
424: *
425:             ANORM = ZERO
426:             DO 90 J = 1, INFO
427:                DO 80 I = MAX( KU+2-J, 1 ), MIN( N+KU+1-J, KL+KU+1 )
428:                   ANORM = MAX( ANORM, ABS( AB( I, J ) ) )
429:    80          CONTINUE
430:    90       CONTINUE
431:             RPVGRW = DLANTB( 'M', 'U', 'N', INFO, MIN( INFO-1, KL+KU ),
432:      $                       AFB( MAX( 1, KL+KU+2-INFO ), 1 ), LDAFB,
433:      $                       WORK )
434:             IF( RPVGRW.EQ.ZERO ) THEN
435:                RPVGRW = ONE
436:             ELSE
437:                RPVGRW = ANORM / RPVGRW
438:             END IF
439:             WORK( 1 ) = RPVGRW
440:             RCOND = ZERO
441:             RETURN
442:          END IF
443:       END IF
444: *
445: *     Compute the norm of the matrix A and the
446: *     reciprocal pivot growth factor RPVGRW.
447: *
448:       IF( NOTRAN ) THEN
449:          NORM = '1'
450:       ELSE
451:          NORM = 'I'
452:       END IF
453:       ANORM = DLANGB( NORM, N, KL, KU, AB, LDAB, WORK )
454:       RPVGRW = DLANTB( 'M', 'U', 'N', N, KL+KU, AFB, LDAFB, WORK )
455:       IF( RPVGRW.EQ.ZERO ) THEN
456:          RPVGRW = ONE
457:       ELSE
458:          RPVGRW = DLANGB( 'M', N, KL, KU, AB, LDAB, WORK ) / RPVGRW
459:       END IF
460: *
461: *     Compute the reciprocal of the condition number of A.
462: *
463:       CALL DGBCON( NORM, N, KL, KU, AFB, LDAFB, IPIV, ANORM, RCOND,
464:      $             WORK, IWORK, INFO )
465: *
466: *     Compute the solution matrix X.
467: *
468:       CALL DLACPY( 'Full', N, NRHS, B, LDB, X, LDX )
469:       CALL DGBTRS( TRANS, N, KL, KU, NRHS, AFB, LDAFB, IPIV, X, LDX,
470:      $             INFO )
471: *
472: *     Use iterative refinement to improve the computed solution and
473: *     compute error bounds and backward error estimates for it.
474: *
475:       CALL DGBRFS( TRANS, N, KL, KU, NRHS, AB, LDAB, AFB, LDAFB, IPIV,
476:      $             B, LDB, X, LDX, FERR, BERR, WORK, IWORK, INFO )
477: *
478: *     Transform the solution matrix X to a solution of the original
479: *     system.
480: *
481:       IF( NOTRAN ) THEN
482:          IF( COLEQU ) THEN
483:             DO 110 J = 1, NRHS
484:                DO 100 I = 1, N
485:                   X( I, J ) = C( I )*X( I, J )
486:   100          CONTINUE
487:   110       CONTINUE
488:             DO 120 J = 1, NRHS
489:                FERR( J ) = FERR( J ) / COLCND
490:   120       CONTINUE
491:          END IF
492:       ELSE IF( ROWEQU ) THEN
493:          DO 140 J = 1, NRHS
494:             DO 130 I = 1, N
495:                X( I, J ) = R( I )*X( I, J )
496:   130       CONTINUE
497:   140    CONTINUE
498:          DO 150 J = 1, NRHS
499:             FERR( J ) = FERR( J ) / ROWCND
500:   150    CONTINUE
501:       END IF
502: *
503: *     Set INFO = N+1 if the matrix is singular to working precision.
504: *
505:       IF( RCOND.LT.DLAMCH( 'Epsilon' ) )
506:      $   INFO = N + 1
507: *
508:       WORK( 1 ) = RPVGRW
509:       RETURN
510: *
511: *     End of DGBSVX
512: *
513:       END
514: