1       SUBROUTINE ZGBSVX( FACT, TRANS, N, KL, KU, NRHS, AB, LDAB, AFB,
  2      $                   LDAFB, IPIV, EQUED, R, C, B, LDB, X, LDX,
  3      $                   RCOND, FERR, BERR, WORK, RWORK, INFO )
  4 *
  5 *  -- LAPACK driver routine (version 3.2) --
  6 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
  7 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  8 *     November 2006
  9 *
 10 *     .. Scalar Arguments ..
 11       CHARACTER          EQUED, FACT, TRANS
 12       INTEGER            INFO, KL, KU, LDAB, LDAFB, LDB, LDX, N, NRHS
 13       DOUBLE PRECISION   RCOND
 14 *     ..
 15 *     .. Array Arguments ..
 16       INTEGER            IPIV( * )
 17       DOUBLE PRECISION   BERR( * ), C( * ), FERR( * ), R( * ),
 18      $                   RWORK( * )
 19       COMPLEX*16         AB( LDAB, * ), AFB( LDAFB, * ), B( LDB, * ),
 20      $                   WORK( * ), X( LDX, * )
 21 *     ..
 22 *
 23 *  Purpose
 24 *  =======
 25 *
 26 *  ZGBSVX uses the LU factorization to compute the solution to a complex
 27 *  system of linear equations A * X = B, A**T * X = B, or A**H * X = B,
 28 *  where A is a band matrix of order N with KL subdiagonals and KU
 29 *  superdiagonals, and X and B are N-by-NRHS matrices.
 30 *
 31 *  Error bounds on the solution and a condition estimate are also
 32 *  provided.
 33 *
 34 *  Description
 35 *  ===========
 36 *
 37 *  The following steps are performed by this subroutine:
 38 *
 39 *  1. If FACT = 'E', real scaling factors are computed to equilibrate
 40 *     the system:
 41 *        TRANS = 'N':  diag(R)*A*diag(C)     *inv(diag(C))*X = diag(R)*B
 42 *        TRANS = 'T': (diag(R)*A*diag(C))**T *inv(diag(R))*X = diag(C)*B
 43 *        TRANS = 'C': (diag(R)*A*diag(C))**H *inv(diag(R))*X = diag(C)*B
 44 *     Whether or not the system will be equilibrated depends on the
 45 *     scaling of the matrix A, but if equilibration is used, A is
 46 *     overwritten by diag(R)*A*diag(C) and B by diag(R)*B (if TRANS='N')
 47 *     or diag(C)*B (if TRANS = 'T' or 'C').
 48 *
 49 *  2. If FACT = 'N' or 'E', the LU decomposition is used to factor the
 50 *     matrix A (after equilibration if FACT = 'E') as
 51 *        A = L * U,
 52 *     where L is a product of permutation and unit lower triangular
 53 *     matrices with KL subdiagonals, and U is upper triangular with
 54 *     KL+KU superdiagonals.
 55 *
 56 *  3. If some U(i,i)=0, so that U is exactly singular, then the routine
 57 *     returns with INFO = i. Otherwise, the factored form of A is used
 58 *     to estimate the condition number of the matrix A.  If the
 59 *     reciprocal of the condition number is less than machine precision,
 60 *     INFO = N+1 is returned as a warning, but the routine still goes on
 61 *     to solve for X and compute error bounds as described below.
 62 *
 63 *  4. The system of equations is solved for X using the factored form
 64 *     of A.
 65 *
 66 *  5. Iterative refinement is applied to improve the computed solution
 67 *     matrix and calculate error bounds and backward error estimates
 68 *     for it.
 69 *
 70 *  6. If equilibration was used, the matrix X is premultiplied by
 71 *     diag(C) (if TRANS = 'N') or diag(R) (if TRANS = 'T' or 'C') so
 72 *     that it solves the original system before equilibration.
 73 *
 74 *  Arguments
 75 *  =========
 76 *
 77 *  FACT    (input) CHARACTER*1
 78 *          Specifies whether or not the factored form of the matrix A is
 79 *          supplied on entry, and if not, whether the matrix A should be
 80 *          equilibrated before it is factored.
 81 *          = 'F':  On entry, AFB and IPIV contain the factored form of
 82 *                  A.  If EQUED is not 'N', the matrix A has been
 83 *                  equilibrated with scaling factors given by R and C.
 84 *                  AB, AFB, and IPIV are not modified.
 85 *          = 'N':  The matrix A will be copied to AFB and factored.
 86 *          = 'E':  The matrix A will be equilibrated if necessary, then
 87 *                  copied to AFB and factored.
 88 *
 89 *  TRANS   (input) CHARACTER*1
 90 *          Specifies the form of the system of equations.
 91 *          = 'N':  A * X = B     (No transpose)
 92 *          = 'T':  A**T * X = B  (Transpose)
 93 *          = 'C':  A**H * X = B  (Conjugate transpose)
 94 *
 95 *  N       (input) INTEGER
 96 *          The number of linear equations, i.e., the order of the
 97 *          matrix A.  N >= 0.
 98 *
 99 *  KL      (input) INTEGER
100 *          The number of subdiagonals within the band of A.  KL >= 0.
101 *
102 *  KU      (input) INTEGER
103 *          The number of superdiagonals within the band of A.  KU >= 0.
104 *
105 *  NRHS    (input) INTEGER
106 *          The number of right hand sides, i.e., the number of columns
107 *          of the matrices B and X.  NRHS >= 0.
108 *
109 *  AB      (input/output) COMPLEX*16 array, dimension (LDAB,N)
110 *          On entry, the matrix A in band storage, in rows 1 to KL+KU+1.
111 *          The j-th column of A is stored in the j-th column of the
112 *          array AB as follows:
113 *          AB(KU+1+i-j,j) = A(i,j) for max(1,j-KU)<=i<=min(N,j+kl)
114 *
115 *          If FACT = 'F' and EQUED is not 'N', then A must have been
116 *          equilibrated by the scaling factors in R and/or C.  AB is not
117 *          modified if FACT = 'F' or 'N', or if FACT = 'E' and
118 *          EQUED = 'N' on exit.
119 *
120 *          On exit, if EQUED .ne. 'N', A is scaled as follows:
121 *          EQUED = 'R':  A := diag(R) * A
122 *          EQUED = 'C':  A := A * diag(C)
123 *          EQUED = 'B':  A := diag(R) * A * diag(C).
124 *
125 *  LDAB    (input) INTEGER
126 *          The leading dimension of the array AB.  LDAB >= KL+KU+1.
127 *
128 *  AFB     (input or output) COMPLEX*16 array, dimension (LDAFB,N)
129 *          If FACT = 'F', then AFB is an input argument and on entry
130 *          contains details of the LU factorization of the band matrix
131 *          A, as computed by ZGBTRF.  U is stored as an upper triangular
132 *          band matrix with KL+KU superdiagonals in rows 1 to KL+KU+1,
133 *          and the multipliers used during the factorization are stored
134 *          in rows KL+KU+2 to 2*KL+KU+1.  If EQUED .ne. 'N', then AFB is
135 *          the factored form of the equilibrated matrix A.
136 *
137 *          If FACT = 'N', then AFB is an output argument and on exit
138 *          returns details of the LU factorization of A.
139 *
140 *          If FACT = 'E', then AFB is an output argument and on exit
141 *          returns details of the LU factorization of the equilibrated
142 *          matrix A (see the description of AB for the form of the
143 *          equilibrated matrix).
144 *
145 *  LDAFB   (input) INTEGER
146 *          The leading dimension of the array AFB.  LDAFB >= 2*KL+KU+1.
147 *
148 *  IPIV    (input or output) INTEGER array, dimension (N)
149 *          If FACT = 'F', then IPIV is an input argument and on entry
150 *          contains the pivot indices from the factorization A = L*U
151 *          as computed by ZGBTRF; row i of the matrix was interchanged
152 *          with row IPIV(i).
153 *
154 *          If FACT = 'N', then IPIV is an output argument and on exit
155 *          contains the pivot indices from the factorization A = L*U
156 *          of the original matrix A.
157 *
158 *          If FACT = 'E', then IPIV is an output argument and on exit
159 *          contains the pivot indices from the factorization A = L*U
160 *          of the equilibrated matrix A.
161 *
162 *  EQUED   (input or output) CHARACTER*1
163 *          Specifies the form of equilibration that was done.
164 *          = 'N':  No equilibration (always true if FACT = 'N').
165 *          = 'R':  Row equilibration, i.e., A has been premultiplied by
166 *                  diag(R).
167 *          = 'C':  Column equilibration, i.e., A has been postmultiplied
168 *                  by diag(C).
169 *          = 'B':  Both row and column equilibration, i.e., A has been
170 *                  replaced by diag(R) * A * diag(C).
171 *          EQUED is an input argument if FACT = 'F'; otherwise, it is an
172 *          output argument.
173 *
174 *  R       (input or output) DOUBLE PRECISION array, dimension (N)
175 *          The row scale factors for A.  If EQUED = 'R' or 'B', A is
176 *          multiplied on the left by diag(R); if EQUED = 'N' or 'C', R
177 *          is not accessed.  R is an input argument if FACT = 'F';
178 *          otherwise, R is an output argument.  If FACT = 'F' and
179 *          EQUED = 'R' or 'B', each element of R must be positive.
180 *
181 *  C       (input or output) DOUBLE PRECISION array, dimension (N)
182 *          The column scale factors for A.  If EQUED = 'C' or 'B', A is
183 *          multiplied on the right by diag(C); if EQUED = 'N' or 'R', C
184 *          is not accessed.  C is an input argument if FACT = 'F';
185 *          otherwise, C is an output argument.  If FACT = 'F' and
186 *          EQUED = 'C' or 'B', each element of C must be positive.
187 *
188 *  B       (input/output) COMPLEX*16 array, dimension (LDB,NRHS)
189 *          On entry, the right hand side matrix B.
190 *          On exit,
191 *          if EQUED = 'N', B is not modified;
192 *          if TRANS = 'N' and EQUED = 'R' or 'B', B is overwritten by
193 *          diag(R)*B;
194 *          if TRANS = 'T' or 'C' and EQUED = 'C' or 'B', B is
195 *          overwritten by diag(C)*B.
196 *
197 *  LDB     (input) INTEGER
198 *          The leading dimension of the array B.  LDB >= max(1,N).
199 *
200 *  X       (output) COMPLEX*16 array, dimension (LDX,NRHS)
201 *          If INFO = 0 or INFO = N+1, the N-by-NRHS solution matrix X
202 *          to the original system of equations.  Note that A and B are
203 *          modified on exit if EQUED .ne. 'N', and the solution to the
204 *          equilibrated system is inv(diag(C))*X if TRANS = 'N' and
205 *          EQUED = 'C' or 'B', or inv(diag(R))*X if TRANS = 'T' or 'C'
206 *          and EQUED = 'R' or 'B'.
207 *
208 *  LDX     (input) INTEGER
209 *          The leading dimension of the array X.  LDX >= max(1,N).
210 *
211 *  RCOND   (output) DOUBLE PRECISION
212 *          The estimate of the reciprocal condition number of the matrix
213 *          A after equilibration (if done).  If RCOND is less than the
214 *          machine precision (in particular, if RCOND = 0), the matrix
215 *          is singular to working precision.  This condition is
216 *          indicated by a return code of INFO > 0.
217 *
218 *  FERR    (output) DOUBLE PRECISION array, dimension (NRHS)
219 *          The estimated forward error bound for each solution vector
220 *          X(j) (the j-th column of the solution matrix X).
221 *          If XTRUE is the true solution corresponding to X(j), FERR(j)
222 *          is an estimated upper bound for the magnitude of the largest
223 *          element in (X(j) - XTRUE) divided by the magnitude of the
224 *          largest element in X(j).  The estimate is as reliable as
225 *          the estimate for RCOND, and is almost always a slight
226 *          overestimate of the true error.
227 *
228 *  BERR    (output) DOUBLE PRECISION array, dimension (NRHS)
229 *          The componentwise relative backward error of each solution
230 *          vector X(j) (i.e., the smallest relative change in
231 *          any element of A or B that makes X(j) an exact solution).
232 *
233 *  WORK    (workspace) COMPLEX*16 array, dimension (2*N)
234 *
235 *  RWORK   (workspace/output) DOUBLE PRECISION array, dimension (N)
236 *          On exit, RWORK(1) contains the reciprocal pivot growth
237 *          factor norm(A)/norm(U). The "max absolute element" norm is
238 *          used. If RWORK(1) is much less than 1, then the stability
239 *          of the LU factorization of the (equilibrated) matrix A
240 *          could be poor. This also means that the solution X, condition
241 *          estimator RCOND, and forward error bound FERR could be
242 *          unreliable. If factorization fails with 0<INFO<=N, then
243 *          RWORK(1) contains the reciprocal pivot growth factor for the
244 *          leading INFO columns of A.
245 *
246 *  INFO    (output) INTEGER
247 *          = 0:  successful exit
248 *          < 0:  if INFO = -i, the i-th argument had an illegal value
249 *          > 0:  if INFO = i, and i is
250 *                <= N:  U(i,i) is exactly zero.  The factorization
251 *                       has been completed, but the factor U is exactly
252 *                       singular, so the solution and error bounds
253 *                       could not be computed. RCOND = 0 is returned.
254 *                = N+1: U is nonsingular, but RCOND is less than machine
255 *                       precision, meaning that the matrix is singular
256 *                       to working precision.  Nevertheless, the
257 *                       solution and error bounds are computed because
258 *                       there are a number of situations where the
259 *                       computed solution can be more accurate than the
260 *                       value of RCOND would suggest.
261 *
262 *  =====================================================================
263 *  Moved setting of INFO = N+1 so INFO does not subsequently get
264 *  overwritten.  Sven, 17 Mar 05. 
265 *  =====================================================================
266 *
267 *     .. Parameters ..
268       DOUBLE PRECISION   ZERO, ONE
269       PARAMETER          ( ZERO = 0.0D+0, ONE = 1.0D+0 )
270 *     ..
271 *     .. Local Scalars ..
272       LOGICAL            COLEQU, EQUIL, NOFACT, NOTRAN, ROWEQU
273       CHARACTER          NORM
274       INTEGER            I, INFEQU, J, J1, J2
275       DOUBLE PRECISION   AMAX, ANORM, BIGNUM, COLCND, RCMAX, RCMIN,
276      $                   ROWCND, RPVGRW, SMLNUM
277 *     ..
278 *     .. External Functions ..
279       LOGICAL            LSAME
280       DOUBLE PRECISION   DLAMCH, ZLANGB, ZLANTB
281       EXTERNAL           LSAME, DLAMCH, ZLANGB, ZLANTB
282 *     ..
283 *     .. External Subroutines ..
284       EXTERNAL           XERBLA, ZCOPY, ZGBCON, ZGBEQU, ZGBRFS, ZGBTRF,
285      $                   ZGBTRS, ZLACPY, ZLAQGB
286 *     ..
287 *     .. Intrinsic Functions ..
288       INTRINSIC          ABSMAXMIN
289 *     ..
290 *     .. Executable Statements ..
291 *
292       INFO = 0
293       NOFACT = LSAME( FACT, 'N' )
294       EQUIL = LSAME( FACT, 'E' )
295       NOTRAN = LSAME( TRANS, 'N' )
296       IF( NOFACT .OR. EQUIL ) THEN
297          EQUED = 'N'
298          ROWEQU = .FALSE.
299          COLEQU = .FALSE.
300       ELSE
301          ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
302          COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
303          SMLNUM = DLAMCH( 'Safe minimum' )
304          BIGNUM = ONE / SMLNUM
305       END IF
306 *
307 *     Test the input parameters.
308 *
309       IF.NOT.NOFACT .AND. .NOT.EQUIL .AND. .NOT.LSAME( FACT, 'F' ) )
310      $     THEN
311          INFO = -1
312       ELSE IF.NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
313      $         LSAME( TRANS, 'C' ) ) THEN
314          INFO = -2
315       ELSE IF( N.LT.0 ) THEN
316          INFO = -3
317       ELSE IF( KL.LT.0 ) THEN
318          INFO = -4
319       ELSE IF( KU.LT.0 ) THEN
320          INFO = -5
321       ELSE IF( NRHS.LT.0 ) THEN
322          INFO = -6
323       ELSE IF( LDAB.LT.KL+KU+1 ) THEN
324          INFO = -8
325       ELSE IF( LDAFB.LT.2*KL+KU+1 ) THEN
326          INFO = -10
327       ELSE IF( LSAME( FACT, 'F' ) .AND. .NOT.
328      $         ( ROWEQU .OR. COLEQU .OR. LSAME( EQUED, 'N' ) ) ) THEN
329          INFO = -12
330       ELSE
331          IF( ROWEQU ) THEN
332             RCMIN = BIGNUM
333             RCMAX = ZERO
334             DO 10 J = 1, N
335                RCMIN = MIN( RCMIN, R( J ) )
336                RCMAX = MAX( RCMAX, R( J ) )
337    10       CONTINUE
338             IF( RCMIN.LE.ZERO ) THEN
339                INFO = -13
340             ELSE IF( N.GT.0 ) THEN
341                ROWCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
342             ELSE
343                ROWCND = ONE
344             END IF
345          END IF
346          IF( COLEQU .AND. INFO.EQ.0 ) THEN
347             RCMIN = BIGNUM
348             RCMAX = ZERO
349             DO 20 J = 1, N
350                RCMIN = MIN( RCMIN, C( J ) )
351                RCMAX = MAX( RCMAX, C( J ) )
352    20       CONTINUE
353             IF( RCMIN.LE.ZERO ) THEN
354                INFO = -14
355             ELSE IF( N.GT.0 ) THEN
356                COLCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
357             ELSE
358                COLCND = ONE
359             END IF
360          END IF
361          IF( INFO.EQ.0 ) THEN
362             IF( LDB.LT.MAX1, N ) ) THEN
363                INFO = -16
364             ELSE IF( LDX.LT.MAX1, N ) ) THEN
365                INFO = -18
366             END IF
367          END IF
368       END IF
369 *
370       IF( INFO.NE.0 ) THEN
371          CALL XERBLA( 'ZGBSVX'-INFO )
372          RETURN
373       END IF
374 *
375       IF( EQUIL ) THEN
376 *
377 *        Compute row and column scalings to equilibrate the matrix A.
378 *
379          CALL ZGBEQU( N, N, KL, KU, AB, LDAB, R, C, ROWCND, COLCND,
380      $                AMAX, INFEQU )
381          IF( INFEQU.EQ.0 ) THEN
382 *
383 *           Equilibrate the matrix.
384 *
385             CALL ZLAQGB( N, N, KL, KU, AB, LDAB, R, C, ROWCND, COLCND,
386      $                   AMAX, EQUED )
387             ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
388             COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
389          END IF
390       END IF
391 *
392 *     Scale the right hand side.
393 *
394       IF( NOTRAN ) THEN
395          IF( ROWEQU ) THEN
396             DO 40 J = 1, NRHS
397                DO 30 I = 1, N
398                   B( I, J ) = R( I )*B( I, J )
399    30          CONTINUE
400    40       CONTINUE
401          END IF
402       ELSE IF( COLEQU ) THEN
403          DO 60 J = 1, NRHS
404             DO 50 I = 1, N
405                B( I, J ) = C( I )*B( I, J )
406    50       CONTINUE
407    60    CONTINUE
408       END IF
409 *
410       IF( NOFACT .OR. EQUIL ) THEN
411 *
412 *        Compute the LU factorization of the band matrix A.
413 *
414          DO 70 J = 1, N
415             J1 = MAX( J-KU, 1 )
416             J2 = MIN( J+KL, N )
417             CALL ZCOPY( J2-J1+1, AB( KU+1-J+J1, J ), 1,
418      $                  AFB( KL+KU+1-J+J1, J ), 1 )
419    70    CONTINUE
420 *
421          CALL ZGBTRF( N, N, KL, KU, AFB, LDAFB, IPIV, INFO )
422 *
423 *        Return if INFO is non-zero.
424 *
425          IF( INFO.GT.0 ) THEN
426 *
427 *           Compute the reciprocal pivot growth factor of the
428 *           leading rank-deficient INFO columns of A.
429 *
430             ANORM = ZERO
431             DO 90 J = 1, INFO
432                DO 80 I = MAX( KU+2-J, 1 ), MIN( N+KU+1-J, KL+KU+1 )
433                   ANORM = MAX( ANORM, ABS( AB( I, J ) ) )
434    80          CONTINUE
435    90       CONTINUE
436             RPVGRW = ZLANTB( 'M''U''N', INFO, MIN( INFO-1, KL+KU ),
437      $                       AFB( MAX1, KL+KU+2-INFO ), 1 ), LDAFB,
438      $                       RWORK )
439             IF( RPVGRW.EQ.ZERO ) THEN
440                RPVGRW = ONE
441             ELSE
442                RPVGRW = ANORM / RPVGRW
443             END IF
444             RWORK( 1 ) = RPVGRW
445             RCOND = ZERO
446             RETURN
447          END IF
448       END IF
449 *
450 *     Compute the norm of the matrix A and the
451 *     reciprocal pivot growth factor RPVGRW.
452 *
453       IF( NOTRAN ) THEN
454          NORM = '1'
455       ELSE
456          NORM = 'I'
457       END IF
458       ANORM = ZLANGB( NORM, N, KL, KU, AB, LDAB, RWORK )
459       RPVGRW = ZLANTB( 'M''U''N', N, KL+KU, AFB, LDAFB, RWORK )
460       IF( RPVGRW.EQ.ZERO ) THEN
461          RPVGRW = ONE
462       ELSE
463          RPVGRW = ZLANGB( 'M', N, KL, KU, AB, LDAB, RWORK ) / RPVGRW
464       END IF
465 *
466 *     Compute the reciprocal of the condition number of A.
467 *
468       CALL ZGBCON( NORM, N, KL, KU, AFB, LDAFB, IPIV, ANORM, RCOND,
469      $             WORK, RWORK, INFO )
470 *
471 *     Compute the solution matrix X.
472 *
473       CALL ZLACPY( 'Full', N, NRHS, B, LDB, X, LDX )
474       CALL ZGBTRS( TRANS, N, KL, KU, NRHS, AFB, LDAFB, IPIV, X, LDX,
475      $             INFO )
476 *
477 *     Use iterative refinement to improve the computed solution and
478 *     compute error bounds and backward error estimates for it.
479 *
480       CALL ZGBRFS( TRANS, N, KL, KU, NRHS, AB, LDAB, AFB, LDAFB, IPIV,
481      $             B, LDB, X, LDX, FERR, BERR, WORK, RWORK, INFO )
482 *
483 *     Transform the solution matrix X to a solution of the original
484 *     system.
485 *
486       IF( NOTRAN ) THEN
487          IF( COLEQU ) THEN
488             DO 110 J = 1, NRHS
489                DO 100 I = 1, N
490                   X( I, J ) = C( I )*X( I, J )
491   100          CONTINUE
492   110       CONTINUE
493             DO 120 J = 1, NRHS
494                FERR( J ) = FERR( J ) / COLCND
495   120       CONTINUE
496          END IF
497       ELSE IF( ROWEQU ) THEN
498          DO 140 J = 1, NRHS
499             DO 130 I = 1, N
500                X( I, J ) = R( I )*X( I, J )
501   130       CONTINUE
502   140    CONTINUE
503          DO 150 J = 1, NRHS
504             FERR( J ) = FERR( J ) / ROWCND
505   150    CONTINUE
506       END IF
507 *
508 *     Set INFO = N+1 if the matrix is singular to working precision.
509 *
510       IF( RCOND.LT.DLAMCH( 'Epsilon' ) )
511      $   INFO = N + 1
512 *
513       RWORK( 1 ) = RPVGRW
514       RETURN
515 *
516 *     End of ZGBSVX
517 *
518       END