Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/mathlibs/src/lapack/cgbsvx.f
5192 views
1
SUBROUTINE CGBSVX( 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.0) --
6
* Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
7
* Courant Institute, Argonne National Lab, and Rice University
8
* June 30, 1999
9
*
10
* .. Scalar Arguments ..
11
CHARACTER EQUED, FACT, TRANS
12
INTEGER INFO, KL, KU, LDAB, LDAFB, LDB, LDX, N, NRHS
13
REAL RCOND
14
* ..
15
* .. Array Arguments ..
16
INTEGER IPIV( * )
17
REAL BERR( * ), C( * ), FERR( * ), R( * ),
18
$ RWORK( * )
19
COMPLEX AB( LDAB, * ), AFB( LDAFB, * ), B( LDB, * ),
20
$ WORK( * ), X( LDX, * )
21
* ..
22
*
23
* Purpose
24
* =======
25
*
26
* CGBSVX 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 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 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 CGBTRF. 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 CGBTRF; 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) REAL 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) REAL 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 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 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) REAL
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) REAL 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) REAL 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 array, dimension (2*N)
234
*
235
* RWORK (workspace/output) REAL 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
*
264
* .. Parameters ..
265
REAL ZERO, ONE
266
PARAMETER ( ZERO = 0.0E+0, ONE = 1.0E+0 )
267
* ..
268
* .. Local Scalars ..
269
LOGICAL COLEQU, EQUIL, NOFACT, NOTRAN, ROWEQU
270
CHARACTER NORM
271
INTEGER I, INFEQU, J, J1, J2
272
REAL AMAX, ANORM, BIGNUM, COLCND, RCMAX, RCMIN,
273
$ ROWCND, RPVGRW, SMLNUM
274
* ..
275
* .. External Functions ..
276
LOGICAL LSAME
277
REAL CLANGB, CLANTB, SLAMCH
278
EXTERNAL LSAME, CLANGB, CLANTB, SLAMCH
279
* ..
280
* .. External Subroutines ..
281
EXTERNAL CCOPY, CGBCON, CGBEQU, CGBRFS, CGBTRF, CGBTRS,
282
$ CLACPY, CLAQGB, XERBLA
283
* ..
284
* .. Intrinsic Functions ..
285
INTRINSIC ABS, MAX, MIN
286
* ..
287
* .. Executable Statements ..
288
*
289
INFO = 0
290
NOFACT = LSAME( FACT, 'N' )
291
EQUIL = LSAME( FACT, 'E' )
292
NOTRAN = LSAME( TRANS, 'N' )
293
IF( NOFACT .OR. EQUIL ) THEN
294
EQUED = 'N'
295
ROWEQU = .FALSE.
296
COLEQU = .FALSE.
297
ELSE
298
ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
299
COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
300
SMLNUM = SLAMCH( 'Safe minimum' )
301
BIGNUM = ONE / SMLNUM
302
END IF
303
*
304
* Test the input parameters.
305
*
306
IF( .NOT.NOFACT .AND. .NOT.EQUIL .AND. .NOT.LSAME( FACT, 'F' ) )
307
$ THEN
308
INFO = -1
309
ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
310
$ LSAME( TRANS, 'C' ) ) THEN
311
INFO = -2
312
ELSE IF( N.LT.0 ) THEN
313
INFO = -3
314
ELSE IF( KL.LT.0 ) THEN
315
INFO = -4
316
ELSE IF( KU.LT.0 ) THEN
317
INFO = -5
318
ELSE IF( NRHS.LT.0 ) THEN
319
INFO = -6
320
ELSE IF( LDAB.LT.KL+KU+1 ) THEN
321
INFO = -8
322
ELSE IF( LDAFB.LT.2*KL+KU+1 ) THEN
323
INFO = -10
324
ELSE IF( LSAME( FACT, 'F' ) .AND. .NOT.
325
$ ( ROWEQU .OR. COLEQU .OR. LSAME( EQUED, 'N' ) ) ) THEN
326
INFO = -12
327
ELSE
328
IF( ROWEQU ) THEN
329
RCMIN = BIGNUM
330
RCMAX = ZERO
331
DO 10 J = 1, N
332
RCMIN = MIN( RCMIN, R( J ) )
333
RCMAX = MAX( RCMAX, R( J ) )
334
10 CONTINUE
335
IF( RCMIN.LE.ZERO ) THEN
336
INFO = -13
337
ELSE IF( N.GT.0 ) THEN
338
ROWCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
339
ELSE
340
ROWCND = ONE
341
END IF
342
END IF
343
IF( COLEQU .AND. INFO.EQ.0 ) THEN
344
RCMIN = BIGNUM
345
RCMAX = ZERO
346
DO 20 J = 1, N
347
RCMIN = MIN( RCMIN, C( J ) )
348
RCMAX = MAX( RCMAX, C( J ) )
349
20 CONTINUE
350
IF( RCMIN.LE.ZERO ) THEN
351
INFO = -14
352
ELSE IF( N.GT.0 ) THEN
353
COLCND = MAX( RCMIN, SMLNUM ) / MIN( RCMAX, BIGNUM )
354
ELSE
355
COLCND = ONE
356
END IF
357
END IF
358
IF( INFO.EQ.0 ) THEN
359
IF( LDB.LT.MAX( 1, N ) ) THEN
360
INFO = -16
361
ELSE IF( LDX.LT.MAX( 1, N ) ) THEN
362
INFO = -18
363
END IF
364
END IF
365
END IF
366
*
367
IF( INFO.NE.0 ) THEN
368
CALL XERBLA( 'CGBSVX', -INFO )
369
RETURN
370
END IF
371
*
372
IF( EQUIL ) THEN
373
*
374
* Compute row and column scalings to equilibrate the matrix A.
375
*
376
CALL CGBEQU( N, N, KL, KU, AB, LDAB, R, C, ROWCND, COLCND,
377
$ AMAX, INFEQU )
378
IF( INFEQU.EQ.0 ) THEN
379
*
380
* Equilibrate the matrix.
381
*
382
CALL CLAQGB( N, N, KL, KU, AB, LDAB, R, C, ROWCND, COLCND,
383
$ AMAX, EQUED )
384
ROWEQU = LSAME( EQUED, 'R' ) .OR. LSAME( EQUED, 'B' )
385
COLEQU = LSAME( EQUED, 'C' ) .OR. LSAME( EQUED, 'B' )
386
END IF
387
END IF
388
*
389
* Scale the right hand side.
390
*
391
IF( NOTRAN ) THEN
392
IF( ROWEQU ) THEN
393
DO 40 J = 1, NRHS
394
DO 30 I = 1, N
395
B( I, J ) = R( I )*B( I, J )
396
30 CONTINUE
397
40 CONTINUE
398
END IF
399
ELSE IF( COLEQU ) THEN
400
DO 60 J = 1, NRHS
401
DO 50 I = 1, N
402
B( I, J ) = C( I )*B( I, J )
403
50 CONTINUE
404
60 CONTINUE
405
END IF
406
*
407
IF( NOFACT .OR. EQUIL ) THEN
408
*
409
* Compute the LU factorization of the band matrix A.
410
*
411
DO 70 J = 1, N
412
J1 = MAX( J-KU, 1 )
413
J2 = MIN( J+KL, N )
414
CALL CCOPY( J2-J1+1, AB( KU+1-J+J1, J ), 1,
415
$ AFB( KL+KU+1-J+J1, J ), 1 )
416
70 CONTINUE
417
*
418
CALL CGBTRF( N, N, KL, KU, AFB, LDAFB, IPIV, INFO )
419
*
420
* Return if INFO is non-zero.
421
*
422
IF( INFO.NE.0 ) THEN
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 ),
431
$ MIN( N+KU+1-J, KL+KU+1 )
432
ANORM = MAX( ANORM, ABS( AB( I, J ) ) )
433
80 CONTINUE
434
90 CONTINUE
435
RPVGRW = CLANTB( 'M', 'U', 'N', INFO,
436
$ MIN( INFO-1, KL+KU ), AFB( MAX( 1,
437
$ KL+KU+2-INFO ), 1 ), LDAFB, RWORK )
438
IF( RPVGRW.EQ.ZERO ) THEN
439
RPVGRW = ONE
440
ELSE
441
RPVGRW = ANORM / RPVGRW
442
END IF
443
RWORK( 1 ) = RPVGRW
444
RCOND = ZERO
445
END IF
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 = CLANGB( NORM, N, KL, KU, AB, LDAB, RWORK )
459
RPVGRW = CLANTB( 'M', 'U', 'N', N, KL+KU, AFB, LDAFB, RWORK )
460
IF( RPVGRW.EQ.ZERO ) THEN
461
RPVGRW = ONE
462
ELSE
463
RPVGRW = CLANGB( '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 CGBCON( NORM, N, KL, KU, AFB, LDAFB, IPIV, ANORM, RCOND,
469
$ WORK, RWORK, INFO )
470
*
471
* Set INFO = N+1 if the matrix is singular to working precision.
472
*
473
IF( RCOND.LT.SLAMCH( 'Epsilon' ) )
474
$ INFO = N + 1
475
*
476
* Compute the solution matrix X.
477
*
478
CALL CLACPY( 'Full', N, NRHS, B, LDB, X, LDX )
479
CALL CGBTRS( TRANS, N, KL, KU, NRHS, AFB, LDAFB, IPIV, X, LDX,
480
$ INFO )
481
*
482
* Use iterative refinement to improve the computed solution and
483
* compute error bounds and backward error estimates for it.
484
*
485
CALL CGBRFS( TRANS, N, KL, KU, NRHS, AB, LDAB, AFB, LDAFB, IPIV,
486
$ B, LDB, X, LDX, FERR, BERR, WORK, RWORK, INFO )
487
*
488
* Transform the solution matrix X to a solution of the original
489
* system.
490
*
491
IF( NOTRAN ) THEN
492
IF( COLEQU ) THEN
493
DO 110 J = 1, NRHS
494
DO 100 I = 1, N
495
X( I, J ) = C( I )*X( I, J )
496
100 CONTINUE
497
110 CONTINUE
498
DO 120 J = 1, NRHS
499
FERR( J ) = FERR( J ) / COLCND
500
120 CONTINUE
501
END IF
502
ELSE IF( ROWEQU ) THEN
503
DO 140 J = 1, NRHS
504
DO 130 I = 1, N
505
X( I, J ) = R( I )*X( I, J )
506
130 CONTINUE
507
140 CONTINUE
508
DO 150 J = 1, NRHS
509
FERR( J ) = FERR( J ) / ROWCND
510
150 CONTINUE
511
END IF
512
*
513
RWORK( 1 ) = RPVGRW
514
RETURN
515
*
516
* End of CGBSVX
517
*
518
END
519
520