001:       SUBROUTINE CGTSV( N, NRHS, DL, D, DU, B, LDB, INFO )
002: *
003: *  -- LAPACK routine (version 3.2) --
004: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
005: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       INTEGER            INFO, LDB, N, NRHS
010: *     ..
011: *     .. Array Arguments ..
012:       COMPLEX            B( LDB, * ), D( * ), DL( * ), DU( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  CGTSV  solves the equation
019: *
020: *     A*X = B,
021: *
022: *  where A is an N-by-N tridiagonal matrix, by Gaussian elimination with
023: *  partial pivoting.
024: *
025: *  Note that the equation  A'*X = B  may be solved by interchanging the
026: *  order of the arguments DU and DL.
027: *
028: *  Arguments
029: *  =========
030: *
031: *  N       (input) INTEGER
032: *          The order of the matrix A.  N >= 0.
033: *
034: *  NRHS    (input) INTEGER
035: *          The number of right hand sides, i.e., the number of columns
036: *          of the matrix B.  NRHS >= 0.
037: *
038: *  DL      (input/output) COMPLEX array, dimension (N-1)
039: *          On entry, DL must contain the (n-1) subdiagonal elements of
040: *          A.
041: *          On exit, DL is overwritten by the (n-2) elements of the
042: *          second superdiagonal of the upper triangular matrix U from
043: *          the LU factorization of A, in DL(1), ..., DL(n-2).
044: *
045: *  D       (input/output) COMPLEX array, dimension (N)
046: *          On entry, D must contain the diagonal elements of A.
047: *          On exit, D is overwritten by the n diagonal elements of U.
048: *
049: *  DU      (input/output) COMPLEX array, dimension (N-1)
050: *          On entry, DU must contain the (n-1) superdiagonal elements
051: *          of A.
052: *          On exit, DU is overwritten by the (n-1) elements of the first
053: *          superdiagonal of U.
054: *
055: *  B       (input/output) COMPLEX array, dimension (LDB,NRHS)
056: *          On entry, the N-by-NRHS right hand side matrix B.
057: *          On exit, if INFO = 0, the N-by-NRHS solution matrix X.
058: *
059: *  LDB     (input) INTEGER
060: *          The leading dimension of the array B.  LDB >= max(1,N).
061: *
062: *  INFO    (output) INTEGER
063: *          = 0:  successful exit
064: *          < 0:  if INFO = -i, the i-th argument had an illegal value
065: *          > 0:  if INFO = i, U(i,i) is exactly zero, and the solution
066: *                has not been computed.  The factorization has not been
067: *                completed unless i = N.
068: *
069: *  =====================================================================
070: *
071: *     .. Parameters ..
072:       COMPLEX            ZERO
073:       PARAMETER          ( ZERO = ( 0.0E+0, 0.0E+0 ) )
074: *     ..
075: *     .. Local Scalars ..
076:       INTEGER            J, K
077:       COMPLEX            MULT, TEMP, ZDUM
078: *     ..
079: *     .. Intrinsic Functions ..
080:       INTRINSIC          ABS, AIMAG, MAX, REAL
081: *     ..
082: *     .. External Subroutines ..
083:       EXTERNAL           XERBLA
084: *     ..
085: *     .. Statement Functions ..
086:       REAL               CABS1
087: *     ..
088: *     .. Statement Function definitions ..
089:       CABS1( ZDUM ) = ABS( REAL( ZDUM ) ) + ABS( AIMAG( ZDUM ) )
090: *     ..
091: *     .. Executable Statements ..
092: *
093:       INFO = 0
094:       IF( N.LT.0 ) THEN
095:          INFO = -1
096:       ELSE IF( NRHS.LT.0 ) THEN
097:          INFO = -2
098:       ELSE IF( LDB.LT.MAX( 1, N ) ) THEN
099:          INFO = -7
100:       END IF
101:       IF( INFO.NE.0 ) THEN
102:          CALL XERBLA( 'CGTSV ', -INFO )
103:          RETURN
104:       END IF
105: *
106:       IF( N.EQ.0 )
107:      $   RETURN
108: *
109:       DO 30 K = 1, N - 1
110:          IF( DL( K ).EQ.ZERO ) THEN
111: *
112: *           Subdiagonal is zero, no elimination is required.
113: *
114:             IF( D( K ).EQ.ZERO ) THEN
115: *
116: *              Diagonal is zero: set INFO = K and return; a unique
117: *              solution can not be found.
118: *
119:                INFO = K
120:                RETURN
121:             END IF
122:          ELSE IF( CABS1( D( K ) ).GE.CABS1( DL( K ) ) ) THEN
123: *
124: *           No row interchange required
125: *
126:             MULT = DL( K ) / D( K )
127:             D( K+1 ) = D( K+1 ) - MULT*DU( K )
128:             DO 10 J = 1, NRHS
129:                B( K+1, J ) = B( K+1, J ) - MULT*B( K, J )
130:    10       CONTINUE
131:             IF( K.LT.( N-1 ) )
132:      $         DL( K ) = ZERO
133:          ELSE
134: *
135: *           Interchange rows K and K+1
136: *
137:             MULT = D( K ) / DL( K )
138:             D( K ) = DL( K )
139:             TEMP = D( K+1 )
140:             D( K+1 ) = DU( K ) - MULT*TEMP
141:             IF( K.LT.( N-1 ) ) THEN
142:                DL( K ) = DU( K+1 )
143:                DU( K+1 ) = -MULT*DL( K )
144:             END IF
145:             DU( K ) = TEMP
146:             DO 20 J = 1, NRHS
147:                TEMP = B( K, J )
148:                B( K, J ) = B( K+1, J )
149:                B( K+1, J ) = TEMP - MULT*B( K+1, J )
150:    20       CONTINUE
151:          END IF
152:    30 CONTINUE
153:       IF( D( N ).EQ.ZERO ) THEN
154:          INFO = N
155:          RETURN
156:       END IF
157: *
158: *     Back solve with the matrix U from the factorization.
159: *
160:       DO 50 J = 1, NRHS
161:          B( N, J ) = B( N, J ) / D( N )
162:          IF( N.GT.1 )
163:      $      B( N-1, J ) = ( B( N-1, J )-DU( N-1 )*B( N, J ) ) / D( N-1 )
164:          DO 40 K = N - 2, 1, -1
165:             B( K, J ) = ( B( K, J )-DU( K )*B( K+1, J )-DL( K )*
166:      $                  B( K+2, J ) ) / D( K )
167:    40    CONTINUE
168:    50 CONTINUE
169: *
170:       RETURN
171: *
172: *     End of CGTSV
173: *
174:       END
175: