You are on page 1of 3

Tridiagonal Matrices: Thomas Algorithm

W. T. Lee∗
MS6021, Scientific Computation, University of Limerick

The Thomas algorithm is an efficient way of solving tridiagonal matrix systems. It is based on LU decompo-
sition in which the matrix system M x = r is rewritten as LU x = r where L is a lower triangular matrix and
U is an upper triangular matrix. The system can be efficiently solved by setting U x = ρ and then solving first
Lρ = r for ρ and then U x = ρ for x. The Thomas algorithm consists of two steps. In Step 1 decomposing the
matrix into M = LU and solving Lρ = r are accomplished in a single downwards sweep, taking us straight
from M x = r to U x = ρ. In step 2 the equation U x = ρ is solved for x in an upwards sweep.

I. STAGE 1 Row 2 a2 x1 + b2 x2 +c2 x3 = r2


a2 × Row 1 a2 x1 + a2 γ1 x 2 = a2 ρ1
In the first stage the matrix equation M x = r is converted New Row 2 (b2 − a2 γ1 ) x2 +c2 x3 = r2 − a2 ρ1
to the form U x = ρ. Initially the matrix equation looks like:
Divide through by (b2 − a2 γ1 ) to get

b1 c1 0 0 0 0 x1
   
r1 c2 r2 − a2 ρ1
x1 + x2 =
a2 b2 c2 0 0 0  x2  r2  b2 − a 2 γ 1 b2 − a 2 γ 1
0 a3 b3 c3 0 0  x3  r3 
    
  =   We can rewrite this as
0 0 a4 b4 c4 0  x4  r4 

0 0 0 a5 b5 c5  x5  r5  c2 r2 − a2 ρ1
x2 + γ2 x3 = ρ2 , γ2 = , ρ2 = .
0 0 0 0 a6 b6 x6 r6 b2 − a 2 γ 1 b2 − a 2 γ 1
Row 1
    
1 γ1 0 0 0 0 x1 ρ1
b1 x1 + c1 x2 = r1 0
 1 γ2 0 0 0  x2  ρ2 
   

0 a3 b3 c3 0 0  x3   r3 
    
Divide through by b1    =  
0 0 a4 b4 c4 0  x4   r4 
   
c1 r1 
x1 + x2 = 0 0 0 a5 b5 c5 x5   r5 

b1 b1
0 0 0 0 a6 b6 x6 r6
Rewrite:
Row 3.
c1 r1
x1 + γ1 x2 = ρ1 , γ1 = , ρ1 =
b1 b1 a3 x2 + b3 x3 + c3 x4 = r3

Use a3 times row 2 of the matrix to eliminate the first term


1 γ1 0 0 0 0 x1 ρ1
    
a2 b2 c2 0 0 0  x2   r2  a3 (x2 + γ2 x3 = ρ2 )
0 a3 b3 c3 0 0  x3   r3 
    
  =  
0 0 a4 b4 c4 0  x4   r4 

0 Row 3 a3 x2 + b3 x3 +c3 x4 = r3
0 0 a5 b5 c5  x5   r5 
a3 × Row 2 a3 x2 + a3 γ2 x 3 = a3 ρ2
0 0 0 0 a6 b6 x6 r6
New Row 3 (b3 − a3 γ2 ) x3 +c3 x4 = r3 − a3 ρ2
Row 2.
Divide through by (b3 − a3 γ2 ) to get
a2 x1 + b2 x2 + c2 x3 = r2 c3 r3 − a3 ρ2
x2 + x3 =
Use a2 times row 1 of the matrix to eliminate the first term b3 − a 3 γ 2 b3 − a 3 γ 2

a2 (x1 + γ1 x2 = ρ1 ) We can rewrite this as


c3 r3 − a3 ρ2
x3 + γ3 x4 = ρ3 , γ3 = , ρ3 = .
b3 − a 3 γ 2 b3 − a 3 γ 2
∗ http://www3.ul.ie/wlee; william.lee@ul.ie
2
         
1 γ1 0 0 0 0 x1 ρ1 1 γ1 0 0 0 0 x1 ρ1
0 1 γ2 0 0 0 x  ρ  0 1 γ2 0 0 0 x  ρ 
   2  2    2  2
0 0 1 γ3 0 0  x3  ρ3  0 0 1 γ3 0 0  x3  ρ3 
         
   =      =  
0 0 a4 b4 c4 0 0 0 0 1 0
 x4   r4  γ4  x4  ρ4 
       
 
0 0 0 a5 b5 c5 x5   r5 
 0 0 0 0 1 γ5 x5  ρ5 

0 0 0 0 a6 b6 x6 r6 0 0 0 0 a6 b6 x6 r6

Row 4. Row 6

a4 x3 + b4 x4 + c4 x5 = r4 a6 x5 + b6 x6 = r6

Use a4 times row 3 of the matrix to eliminate the first term Use a6 times row 5 to eliminate the first term.

a4 (x3 + γ3 x4 = ρ3 ) a6 x5 + a6 γ5 x6 = a6 ρ5

Row 4 a4 x3 + b4 x4 +c4 x5 = r4 Resulting in


a4 × Row 3 a4 x3 + a4 γ3 x 4 = a4 ρ3
(b6 − a6 γ5 ) x6 = r6 − a6 ρ5
New Row 4 (b4 − a4 γ3 ) x4 +c4 x5 = r4 − a4 ρ3
Divide through by b6 − a6 γ5 to get
Divide through by (b4 − a4 γ3 ) to get
r6 − a6 ρ5
c4 r4 − a4 ρ3 x6 = ρ6 , ρ6 =
x3 + x4 = b6 − a 6 γ 5
b4 − a 4 γ 3 b4 − a 4 γ 3
We can rewrite this as     
1 γ1 0 0 0 0 x1 ρ1
c4 r4 − a4 ρ3 0 1 γ2 0 0 0 x  ρ 
x4 + γ4 x5 = ρ4 , γ4 = , ρ4 = .    2  2
b4 − a 4 γ 3 b4 − a 4 γ 3 0

0 1 γ3 0 0  x3  ρ3 
   
   =  
0 0 0 1 γ4 0  x4  ρ4 
   

     0 0 0 0 1 γ5  x5  ρ5 
1 γ1 0 0 0 0 x1 ρ1
0 1 γ2 0 0 0 x  ρ  0 0 0 0 0 1 x6 ρ6
   2  2
0 0 1 γ3 0 0  x3  ρ3 
    
   =   At this point the matrix has been reduced to upper diagonal
0 0 0 1 γ4 0  x4  ρ4  form, so our equations are in the form U x = ρ.
   

0 0 0 a5 b5 c5 x5   r5 

0 0 0 0 a6 b6 x6 r6
II. STAGE 2
Row 5.
The matrix equation is now in a form which is trivial to
a5 x4 + b5 x5 + c5 x6 = r5 solve for x. We start with the last row and work our way up.
The final equation is already solved
Use a5 times row 4 of the matrix to eliminate the first term
x6 = ρ6 .
a5 (x4 + γ4 x5 = ρ4 )

Row 5 a5 x4 + b5 x5 +c5 x6 = r5
    
1 γ1 0 0 0 0 x1 ρ1
a5 × Row 4 a5 x4 + a5 γ4 x 5 = a5 ρ4 0
 1 γ2 0 0 0 x  ρ 
  2  2
New Row 5 (b5 − a5 γ4 ) x5 +c5 x6 = r5 − a5 ρ4 0

0 1 γ3 0 0  x3  ρ3 
   
   =  
0 0 0 1 γ4 0
Divide through by (b5 − a5 γ4 ) to get  x4  ρ4 
   

0 0 0 0 1 γ5  x5  ρ5 
c5 r5 − a5 ρ4 0 0 0 0 0 1 x6 ρ6
x4 + x5 =
b5 − a 5 γ 4 b5 − a 5 γ 4
We can rewrite this as
c5 r5 − a5 ρ4
x5 + γ5 x6 = ρ5 , γ5 = , ρ5 = .
b5 − a 5 γ 4 b5 − a 5 γ 4
3

Row 5: x5 + γ5 x6 = ρ5 . Row 1: x1 + γ1 x2 = ρ1 .
Rearrange to get: x5 = ρ5 − γ5 x6 . Rearrange to get: x1 = ρ1 − γ1 x2 .
         
1 γ1 0 0 0 0 x1 ρ1 1 γ1 0 0 0 0 x1 ρ1
0 1 γ2 0 0 0 x  ρ  0 1 γ2 0 0 0 x  ρ 
     2
2      2
2
0 0 1 γ3 0 0  x3  ρ3  0 0 1 γ3 0 0  x3  ρ3 
         
   =      =  
0 0 0 1 γ4 0 0 0 0 1 0
 x4  ρ4  γ4  x4  ρ4 
       
 
0 0 0 0 1 γ5  x5  ρ5  0 0 0 0 1 γ5  x5  ρ5 
0 0 0 0 0 1 x6 ρ6 0 0 0 0 0 1 x6 ρ6

Row 4: x4 + γ4 x5 = ρ4 . At this point x, the solution to the matrix equation, is fully


Rearrange to get: x4 = ρ4 − γ4 x5 . determined.
     III. IN PRACTICE
1 γ1 0 0 0 0 x1 ρ1
0 1 γ2 0 0 0 x  ρ  The Thomas algorithm is used because it is fast and be-
     2
2
0

0 1 γ3 0 0  x3  ρ3 
    cause tridiagonal matrices often occur in practice. (This ar-
   =   gument is slightly circular because people often manipulate
0 0 0 1 γ4 0  x4  ρ4 
   

0 the problems they are working on to reduce them to solving a
0 0 0 1 γ5  x5  ρ5 
tridiagonal matrix problem.) Although it is rare, the algorithm
0 0 0 0 0 1 x6 ρ6 can be unstable if bi − ai γi−1 is zero or numerically zero for
any i. This will occur if the tridiagonal matrix is singular, but
Row 3: x3 + γ3 x4 = ρ3 .
in rare cases can occur if it is non-singular. The condition for
Rearrange to get: x3 = ρ3 − γ3 x4 .
the algorithm to be stable is
    
1 γ1 0 0 0 0 x1 ρ1 kbi k > kai k + kci k
0 1 γ2 0 0 0 x2  ρ2 
   

0

0 1 γ3 0 0  x3  ρ3 
    for all i. The matrix problems which result from the discreti-
   =   sation of partial differential equations nearly all satisfy this
0 0 0 1 γ4 0 x4  ρ4 
   

0 criterion.
0 0 0 1 γ5 x5  ρ5 

If the algorithm is numerically unstable then you must re-
0 0 0 0 0 1 x6 ρ6 arrange the equations: known as pivoting. Standard LU de-
composition algorithms for full or banded matrices include
Row 2: x2 + γ2 x3 = ρ2 . pivoting. (But first you should check to make sure you have
Rearrange to get: x2 = ρ2 − γ2 x3 . not made a mistake in formulating the problem.)
    
1 γ1 0 0 0 0 x1 ρ1
0 1 γ2 0 0 0  x2  ρ2 
   
 
0 0 1 γ3 0 0  x3  ρ3 
    
   =  
0 0 0 1 γ4 0 x4  ρ4 
   

0 0 0 0 1 γ5 x5  ρ5 

0 0 0 0 0 1 x6 ρ6

You might also like