```001:       SUBROUTINE CTBSV(UPLO,TRANS,DIAG,N,K,A,LDA,X,INCX)
002: *     .. Scalar Arguments ..
003:       INTEGER INCX,K,LDA,N
004:       CHARACTER DIAG,TRANS,UPLO
005: *     ..
006: *     .. Array Arguments ..
007:       COMPLEX A(LDA,*),X(*)
008: *     ..
009: *
010: *  Purpose
011: *  =======
012: *
013: *  CTBSV  solves one of the systems of equations
014: *
015: *     A*x = b,   or   A'*x = b,   or   conjg( A' )*x = b,
016: *
017: *  where b and x are n element vectors and A is an n by n unit, or
018: *  non-unit, upper or lower triangular band matrix, with ( k + 1 )
019: *  diagonals.
020: *
021: *  No test for singularity or near-singularity is included in this
022: *  routine. Such tests must be performed before calling this routine.
023: *
024: *  Arguments
025: *  ==========
026: *
027: *  UPLO   - CHARACTER*1.
028: *           On entry, UPLO specifies whether the matrix is an upper or
029: *           lower triangular matrix as follows:
030: *
031: *              UPLO = 'U' or 'u'   A is an upper triangular matrix.
032: *
033: *              UPLO = 'L' or 'l'   A is a lower triangular matrix.
034: *
035: *           Unchanged on exit.
036: *
037: *  TRANS  - CHARACTER*1.
038: *           On entry, TRANS specifies the equations to be solved as
039: *           follows:
040: *
041: *              TRANS = 'N' or 'n'   A*x = b.
042: *
043: *              TRANS = 'T' or 't'   A'*x = b.
044: *
045: *              TRANS = 'C' or 'c'   conjg( A' )*x = b.
046: *
047: *           Unchanged on exit.
048: *
049: *  DIAG   - CHARACTER*1.
050: *           On entry, DIAG specifies whether or not A is unit
051: *           triangular as follows:
052: *
053: *              DIAG = 'U' or 'u'   A is assumed to be unit triangular.
054: *
055: *              DIAG = 'N' or 'n'   A is not assumed to be unit
056: *                                  triangular.
057: *
058: *           Unchanged on exit.
059: *
060: *  N      - INTEGER.
061: *           On entry, N specifies the order of the matrix A.
062: *           N must be at least zero.
063: *           Unchanged on exit.
064: *
065: *  K      - INTEGER.
066: *           On entry with UPLO = 'U' or 'u', K specifies the number of
067: *           super-diagonals of the matrix A.
068: *           On entry with UPLO = 'L' or 'l', K specifies the number of
069: *           sub-diagonals of the matrix A.
070: *           K must satisfy  0 .le. K.
071: *           Unchanged on exit.
072: *
073: *  A      - COMPLEX          array of DIMENSION ( LDA, n ).
074: *           Before entry with UPLO = 'U' or 'u', the leading ( k + 1 )
075: *           by n part of the array A must contain the upper triangular
076: *           band part of the matrix of coefficients, supplied column by
077: *           column, with the leading diagonal of the matrix in row
078: *           ( k + 1 ) of the array, the first super-diagonal starting at
079: *           position 2 in row k, and so on. The top left k by k triangle
080: *           of the array A is not referenced.
081: *           The following program segment will transfer an upper
082: *           triangular band matrix from conventional full matrix storage
083: *           to band storage:
084: *
085: *                 DO 20, J = 1, N
086: *                    M = K + 1 - J
087: *                    DO 10, I = MAX( 1, J - K ), J
088: *                       A( M + I, J ) = matrix( I, J )
089: *              10    CONTINUE
090: *              20 CONTINUE
091: *
092: *           Before entry with UPLO = 'L' or 'l', the leading ( k + 1 )
093: *           by n part of the array A must contain the lower triangular
094: *           band part of the matrix of coefficients, supplied column by
095: *           column, with the leading diagonal of the matrix in row 1 of
096: *           the array, the first sub-diagonal starting at position 1 in
097: *           row 2, and so on. The bottom right k by k triangle of the
098: *           array A is not referenced.
099: *           The following program segment will transfer a lower
100: *           triangular band matrix from conventional full matrix storage
101: *           to band storage:
102: *
103: *                 DO 20, J = 1, N
104: *                    M = 1 - J
105: *                    DO 10, I = J, MIN( N, J + K )
106: *                       A( M + I, J ) = matrix( I, J )
107: *              10    CONTINUE
108: *              20 CONTINUE
109: *
110: *           Note that when DIAG = 'U' or 'u' the elements of the array A
111: *           corresponding to the diagonal elements of the matrix are not
112: *           referenced, but are assumed to be unity.
113: *           Unchanged on exit.
114: *
115: *  LDA    - INTEGER.
116: *           On entry, LDA specifies the first dimension of A as declared
117: *           in the calling (sub) program. LDA must be at least
118: *           ( k + 1 ).
119: *           Unchanged on exit.
120: *
121: *  X      - COMPLEX          array of dimension at least
122: *           ( 1 + ( n - 1 )*abs( INCX ) ).
123: *           Before entry, the incremented array X must contain the n
124: *           element right-hand side vector b. On exit, X is overwritten
125: *           with the solution vector x.
126: *
127: *  INCX   - INTEGER.
128: *           On entry, INCX specifies the increment for the elements of
129: *           X. INCX must not be zero.
130: *           Unchanged on exit.
131: *
132: *  Further Details
133: *  ===============
134: *
135: *  Level 2 Blas routine.
136: *
137: *  -- Written on 22-October-1986.
138: *     Jack Dongarra, Argonne National Lab.
139: *     Jeremy Du Croz, Nag Central Office.
140: *     Sven Hammarling, Nag Central Office.
141: *     Richard Hanson, Sandia National Labs.
142: *
143: *  =====================================================================
144: *
145: *     .. Parameters ..
146:       COMPLEX ZERO
147:       PARAMETER (ZERO= (0.0E+0,0.0E+0))
148: *     ..
149: *     .. Local Scalars ..
150:       COMPLEX TEMP
151:       INTEGER I,INFO,IX,J,JX,KPLUS1,KX,L
152:       LOGICAL NOCONJ,NOUNIT
153: *     ..
154: *     .. External Functions ..
155:       LOGICAL LSAME
156:       EXTERNAL LSAME
157: *     ..
158: *     .. External Subroutines ..
159:       EXTERNAL XERBLA
160: *     ..
161: *     .. Intrinsic Functions ..
162:       INTRINSIC CONJG,MAX,MIN
163: *     ..
164: *
165: *     Test the input parameters.
166: *
167:       INFO = 0
168:       IF (.NOT.LSAME(UPLO,'U') .AND. .NOT.LSAME(UPLO,'L')) THEN
169:           INFO = 1
170:       ELSE IF (.NOT.LSAME(TRANS,'N') .AND. .NOT.LSAME(TRANS,'T') .AND.
171:      +         .NOT.LSAME(TRANS,'C')) THEN
172:           INFO = 2
173:       ELSE IF (.NOT.LSAME(DIAG,'U') .AND. .NOT.LSAME(DIAG,'N')) THEN
174:           INFO = 3
175:       ELSE IF (N.LT.0) THEN
176:           INFO = 4
177:       ELSE IF (K.LT.0) THEN
178:           INFO = 5
179:       ELSE IF (LDA.LT. (K+1)) THEN
180:           INFO = 7
181:       ELSE IF (INCX.EQ.0) THEN
182:           INFO = 9
183:       END IF
184:       IF (INFO.NE.0) THEN
185:           CALL XERBLA('CTBSV ',INFO)
186:           RETURN
187:       END IF
188: *
189: *     Quick return if possible.
190: *
191:       IF (N.EQ.0) RETURN
192: *
193:       NOCONJ = LSAME(TRANS,'T')
194:       NOUNIT = LSAME(DIAG,'N')
195: *
196: *     Set up the start point in X if the increment is not unity. This
197: *     will be  ( N - 1 )*INCX  too small for descending loops.
198: *
199:       IF (INCX.LE.0) THEN
200:           KX = 1 - (N-1)*INCX
201:       ELSE IF (INCX.NE.1) THEN
202:           KX = 1
203:       END IF
204: *
205: *     Start the operations. In this version the elements of A are
206: *     accessed by sequentially with one pass through A.
207: *
208:       IF (LSAME(TRANS,'N')) THEN
209: *
210: *        Form  x := inv( A )*x.
211: *
212:           IF (LSAME(UPLO,'U')) THEN
213:               KPLUS1 = K + 1
214:               IF (INCX.EQ.1) THEN
215:                   DO 20 J = N,1,-1
216:                       IF (X(J).NE.ZERO) THEN
217:                           L = KPLUS1 - J
218:                           IF (NOUNIT) X(J) = X(J)/A(KPLUS1,J)
219:                           TEMP = X(J)
220:                           DO 10 I = J - 1,MAX(1,J-K),-1
221:                               X(I) = X(I) - TEMP*A(L+I,J)
222:    10                     CONTINUE
223:                       END IF
224:    20             CONTINUE
225:               ELSE
226:                   KX = KX + (N-1)*INCX
227:                   JX = KX
228:                   DO 40 J = N,1,-1
229:                       KX = KX - INCX
230:                       IF (X(JX).NE.ZERO) THEN
231:                           IX = KX
232:                           L = KPLUS1 - J
233:                           IF (NOUNIT) X(JX) = X(JX)/A(KPLUS1,J)
234:                           TEMP = X(JX)
235:                           DO 30 I = J - 1,MAX(1,J-K),-1
236:                               X(IX) = X(IX) - TEMP*A(L+I,J)
237:                               IX = IX - INCX
238:    30                     CONTINUE
239:                       END IF
240:                       JX = JX - INCX
241:    40             CONTINUE
242:               END IF
243:           ELSE
244:               IF (INCX.EQ.1) THEN
245:                   DO 60 J = 1,N
246:                       IF (X(J).NE.ZERO) THEN
247:                           L = 1 - J
248:                           IF (NOUNIT) X(J) = X(J)/A(1,J)
249:                           TEMP = X(J)
250:                           DO 50 I = J + 1,MIN(N,J+K)
251:                               X(I) = X(I) - TEMP*A(L+I,J)
252:    50                     CONTINUE
253:                       END IF
254:    60             CONTINUE
255:               ELSE
256:                   JX = KX
257:                   DO 80 J = 1,N
258:                       KX = KX + INCX
259:                       IF (X(JX).NE.ZERO) THEN
260:                           IX = KX
261:                           L = 1 - J
262:                           IF (NOUNIT) X(JX) = X(JX)/A(1,J)
263:                           TEMP = X(JX)
264:                           DO 70 I = J + 1,MIN(N,J+K)
265:                               X(IX) = X(IX) - TEMP*A(L+I,J)
266:                               IX = IX + INCX
267:    70                     CONTINUE
268:                       END IF
269:                       JX = JX + INCX
270:    80             CONTINUE
271:               END IF
272:           END IF
273:       ELSE
274: *
275: *        Form  x := inv( A' )*x  or  x := inv( conjg( A') )*x.
276: *
277:           IF (LSAME(UPLO,'U')) THEN
278:               KPLUS1 = K + 1
279:               IF (INCX.EQ.1) THEN
280:                   DO 110 J = 1,N
281:                       TEMP = X(J)
282:                       L = KPLUS1 - J
283:                       IF (NOCONJ) THEN
284:                           DO 90 I = MAX(1,J-K),J - 1
285:                               TEMP = TEMP - A(L+I,J)*X(I)
286:    90                     CONTINUE
287:                           IF (NOUNIT) TEMP = TEMP/A(KPLUS1,J)
288:                       ELSE
289:                           DO 100 I = MAX(1,J-K),J - 1
290:                               TEMP = TEMP - CONJG(A(L+I,J))*X(I)
291:   100                     CONTINUE
292:                           IF (NOUNIT) TEMP = TEMP/CONJG(A(KPLUS1,J))
293:                       END IF
294:                       X(J) = TEMP
295:   110             CONTINUE
296:               ELSE
297:                   JX = KX
298:                   DO 140 J = 1,N
299:                       TEMP = X(JX)
300:                       IX = KX
301:                       L = KPLUS1 - J
302:                       IF (NOCONJ) THEN
303:                           DO 120 I = MAX(1,J-K),J - 1
304:                               TEMP = TEMP - A(L+I,J)*X(IX)
305:                               IX = IX + INCX
306:   120                     CONTINUE
307:                           IF (NOUNIT) TEMP = TEMP/A(KPLUS1,J)
308:                       ELSE
309:                           DO 130 I = MAX(1,J-K),J - 1
310:                               TEMP = TEMP - CONJG(A(L+I,J))*X(IX)
311:                               IX = IX + INCX
312:   130                     CONTINUE
313:                           IF (NOUNIT) TEMP = TEMP/CONJG(A(KPLUS1,J))
314:                       END IF
315:                       X(JX) = TEMP
316:                       JX = JX + INCX
317:                       IF (J.GT.K) KX = KX + INCX
318:   140             CONTINUE
319:               END IF
320:           ELSE
321:               IF (INCX.EQ.1) THEN
322:                   DO 170 J = N,1,-1
323:                       TEMP = X(J)
324:                       L = 1 - J
325:                       IF (NOCONJ) THEN
326:                           DO 150 I = MIN(N,J+K),J + 1,-1
327:                               TEMP = TEMP - A(L+I,J)*X(I)
328:   150                     CONTINUE
329:                           IF (NOUNIT) TEMP = TEMP/A(1,J)
330:                       ELSE
331:                           DO 160 I = MIN(N,J+K),J + 1,-1
332:                               TEMP = TEMP - CONJG(A(L+I,J))*X(I)
333:   160                     CONTINUE
334:                           IF (NOUNIT) TEMP = TEMP/CONJG(A(1,J))
335:                       END IF
336:                       X(J) = TEMP
337:   170             CONTINUE
338:               ELSE
339:                   KX = KX + (N-1)*INCX
340:                   JX = KX
341:                   DO 200 J = N,1,-1
342:                       TEMP = X(JX)
343:                       IX = KX
344:                       L = 1 - J
345:                       IF (NOCONJ) THEN
346:                           DO 180 I = MIN(N,J+K),J + 1,-1
347:                               TEMP = TEMP - A(L+I,J)*X(IX)
348:                               IX = IX - INCX
349:   180                     CONTINUE
350:                           IF (NOUNIT) TEMP = TEMP/A(1,J)
351:                       ELSE
352:                           DO 190 I = MIN(N,J+K),J + 1,-1
353:                               TEMP = TEMP - CONJG(A(L+I,J))*X(IX)
354:                               IX = IX - INCX
355:   190                     CONTINUE
356:                           IF (NOUNIT) TEMP = TEMP/CONJG(A(1,J))
357:                       END IF
358:                       X(JX) = TEMP
359:                       JX = JX - INCX
360:                       IF ((N-J).GE.K) KX = KX - INCX
361:   200             CONTINUE
362:               END IF
363:           END IF
364:       END IF
365: *
366:       RETURN
367: *
368: *     End of CTBSV .
369: *
370:       END
371: ```