Professional Documents
Culture Documents
Tridiagonal Matrix Algorithm
Tridiagonal Matrix Algorithm
CONTENTS
1 Tridiagonal Matrix Algorithm 2
1.1 Tridiagonal Linear Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Tridiagonal Matrix Algorithm: Vector Implementation . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Tridiagonal Matrix Algorithm: Matrix Implementation . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.1 Naive Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.2 Better Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.3 Best Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
References 7
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the ”Software”), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED ”AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
LICENSE
2 Section 1 Tridiagonal Matrix Algorithm
and where A ∈ Rn×n and x, d ∈ Rn . Owing to the fact that it only has three nonzero diagonals, the matrix A is
referred to as a tridiagonal matrix1 . The tridiagonal vectors a ∈ Rn−1 , b ∈ Rn , and c ∈ Rn−1 are defined below
in Eq. (3).
a1 b1 c1
a = ... , b = ... , c = ... (3)
an−1 bn cn−1
These tridiagonal vectors form the tridiagonal matrix A, as shown in Eq. (1) [1].
The tridiagonal matrix algorithm (also known as the Thomas algorithm) is an algorithm that can efficiently
solve the tridiagonal linear system for x. There are two general implementations of this algorithm; one whose inputs
are the tridiagonal vectors a, b, and c, and the other which operates directly on the tridiagonal matrix A. We name
these algorithms accordingly:
Two additional algorithms (Algorithms 2 and 3) are also detailed, but these primarily serve as stepping stones towards
developing Algorithm 4.
1 In many references, a tridiagonal matrix is often defined with one of the following two convention:
a1 b1 b 1 c1
c1 a2 b2 a2 b2 c2
.. .. .. ..
c2 . . a3 . .
A= A=
.. .. .. ..
. . bn−2
. . cn−2
cn−2 an−1 bn−1 an−1 bn−1 cn−1
cn−1 an an bn
The first is typically used when defining a tridiagonal linear system [2], while the second is used almost exclusively when defining the tridiagonal
matrix algorithm [3, 4]. However, for the second convention above, the ai ’s range from a2 to an , which is inconvenient from a programming
standpoint; therefore, I defined them here as ranging from a1 to an−1 . This convention is also reflected in Algorithms 1, 2, and 3.
1.2 Tridiagonal Matrix Algorithm: Vector Implementation 3
Algorithm 1: tridiagonal_vector
Solves the tridiagonal linear system Ax = d for x using the vector
implementation of the tridiagonal matrix algorithm.
Inputs:
• a ∈ Rn−1 - tridiagonal vector
• b ∈ Rn - tridiagonal vector
• c ∈ Rn−1 - tridiagonal vector
• d ∈ Rn - vector
Procedure:
1. Determine n, given that d ∈ Rn .
2. Preallocate the vector x ∈ Rn .
3. Forward elimination.
for i = 2 to n
ai−1
w=
bi−1
bi = bi − wci−1
di = di − wdi−1
end
4. Backward substitution.
dn
xn =
bn
for i = n − 1 to 1 by −1
di − ci xi+1
xi =
bi
end
return x
Outputs:
• x ∈ Rn - solution of the tridiagonal linear system Ax = d
Note:
• The tridiagonal matrix (A) for the tridiagonal linear system (Ax = d) is defined in
terms of the
tridiagonal vectors (a, b, and c) as
b1 c1
a2 b2 c2
.. ..
a3 . .
A=
. .. . ..
cn−2
an−1 bn−1 cn−1
an bn
4 Section 1 Tridiagonal Matrix Algorithm
Algorithm 2: tridiagonal_matrix_naive
Solves the tridiagonal linear system Ax = d for x using the matrix
implementation of the tridiagonal matrix algorithm (naive version).
Inputs:
• A ∈ Rn×n - tridiagonal matrix
• d ∈ Rn - vector
Procedure:
1. Determine n, given that d ∈ Rn .
2. Preallocate the vectors a ∈ Rn−1 , b ∈ Rn , c ∈ Rn−1 , and x ∈ Rn .
3. Extract a from A.
for i = 2 to n
ai−1 = Ai,i−1
end
4. Extract b from A.
for i = 1 to n
bi = Ai,i
end
5. Extract c from A.
for i = 2 to n
ci−1 = Ai−1,i
end
6. Forward elimination.
for i = 2 to n
ai−1
w=
bi−1
bi = bi − wci−1
di = di − wdi−1
end
1.3 Tridiagonal Matrix Algorithm: Matrix Implementation 5
7. Backward substitution.
dn
xn =
bn
for i = n − 1 to 1 by −1
di − ci xi+1
xi =
bi
end
return x
Outputs:
• x ∈ Rn - solution of the tridiagonal linear system Ax = d
Algorithm 3: tridiagonal_matrix_better
Solves the tridiagonal linear system Ax = d for x using the matrix
implementation of the tridiagonal matrix algorithm (better version).
Inputs:
• A ∈ Rn×n - tridiagonal matrix
• d ∈ Rn - vector
Procedure:
1. Determine n, given that d ∈ Rn .
2. Preallocate the vectors a ∈ Rn−1 , b ∈ Rn , c ∈ Rn−1 , and x ∈ Rn .
3. Extract first element of b from A.
b1 = A1,1
4. Forward loop.
for i = 2 to n
6 Section 1 Tridiagonal Matrix Algorithm
ai−1 = Ai,i−1
bi = Ai,i
ci−1 = Ai−1,i
dn
xn =
bn
for i = n − 1 to 1 by −1
di − ci xi+1
xi =
bi
end
return x
Outputs:
• x ∈ Rn - solution of the tridiagonal linear system Ax = d
Algorithm 4: tridiagonal_matrix
Solves the tridiagonal linear system Ax = d for x using the matrix
implementation of the tridiagonal matrix algorithm.
Inputs:
• A ∈ Rn×n - tridiagonal matrix
• d ∈ Rn - vector
Procedure:
1. Determine n, given that d ∈ Rn .
2. Preallocate the vector x ∈ Rn .
3. Forward elimination.
1.3 Tridiagonal Matrix Algorithm: Matrix Implementation 7
for i = 2 to n
Ai,i−1
w=
Ai−1,i−1
Ai,i = Ai,i − wAi−1,i
di = di − wdi−1
end
4. Backward substitution.
dn
xn =
An,n
for i = n − 1 to 1 by −1
di − Ai,i+1 xi+1
xi =
Ai,i
end
return x
Outputs:
• x ∈ Rn - solution of the tridiagonal linear system Ax = d
8 REFERENCES
REFERENCES
[1] James Hateley. Linear Systems of Equations and Direct Solvers. MATH 3620 Course Reader (Vanderbilt Uni-
versity). 2019.
[2] Tridiagonal matrix algorithm. Wikipedia. Accessed: December 14, 2021. URL: https://en.wikipedia.
org/wiki/Tridiagonal_matrix.
[3] Tridiagonal matrix algorithm. Wikipedia. Accessed: January 9, 2021. URL: https://en.wikipedia.org/
wiki/Tridiagonal_matrix_algorithm.
[4] Tridiagonal matrix algorithm – TDMA (Thomas algorithm). CFD Online. Accessed: January 9, 2021. URL:
https : / / www . cfd - online . com / Wiki / Tridiagonal _ matrix _ algorithm_ - _TDMA _ (Thomas _
algorithm).