# Tridiagonal matrix algorithm

(Redirected from Thomas algorithm)

In numerical linear algebra, the tridiagonal matrix algorithm, also known as the Thomas algorithm (named after Llewellyn Thomas), is a simplified form of Gaussian elimination that can be used to solve tridiagonal systems of equations. A tridiagonal system for n unknowns may be written as

$a_i x_{i - 1} + b_i x_i + c_i x_{i + 1} = d_i , \,\!$

where $a_1 = 0\,$ and $c_n = 0\,$.

$\begin{bmatrix} {b_1} & {c_1} & { } & { } & { 0 } \\ {a_2} & {b_2} & {c_2} & { } & { } \\ { } & {a_3} & {b_3} & \ddots & { } \\ { } & { } & \ddots & \ddots & {c_{n-1}}\\ { 0 } & { } & { } & {a_n} & {b_n}\\ \end{bmatrix} \begin{bmatrix} {x_1 } \\ {x_2 } \\ {x_3 } \\ \vdots \\ {x_n } \\ \end{bmatrix} = \begin{bmatrix} {d_1 } \\ {d_2 } \\ {d_3 } \\ \vdots \\ {d_n } \\ \end{bmatrix} .$

For such systems, the solution can be obtained in $O(n)$ operations instead of $O(n^3)$ required by Gaussian elimination. A first sweep eliminates the $a_i$'s, and then an (abbreviated) backward substitution produces the solution. Examples of such matrices commonly arise from the discretization of 1D Poisson equation (e.g., the 1D diffusion problem) and natural cubic spline interpolation; similar systems of matrices arise in tight binding physics or nearest neighbor effects models.

## Method

The forward sweep consists of modifying the coefficients as follows, denoting the new modified coefficients with primes:

$c'_i = \begin{cases} \begin{array}{lcl} \cfrac{c_i}{b_i} & ; & i = 1 \\ \cfrac{c_i}{b_i - a_i c'_{i - 1}} & ; & i = 2, 3, \dots, n-1 \\ \end{array} \end{cases} \,$

and

$d'_i = \begin{cases} \begin{array}{lcl} \cfrac{d_i}{b_i} & ; & i = 1 \\ \cfrac{d_i - a_i d'_{i - 1}}{b_i - a_i c'_{i - 1}} & ; & i = 2, 3, \dots, n. \\ \end{array} \end{cases} \,$

The solution is then obtained by back substitution:

$x_n = d'_n\,$
$x_i = d'_i - c'_i x_{i + 1} \qquad ; \ i = n - 1, n - 2, \ldots, 1.$

## Implementations

All the provided implementations assume that the three diagonals, a (below), b (main), and c (above), are passed as arguments.

### C++

The following C++ function will solve a general tridiagonal system (though it will destroy the input vector c and d in the process). Note that the index $i$ here is zero based, in other words $i = 0, 1, \dots, N - 1$ where $N$ is the number of unknowns.

#include <iostream>

using namespace std;

void solve(double* a, double* b, double* c, double* d, int n) {
/*
// n is the number of unknowns

|b0 c0 0 ||x0| |d0|
|a1 b1 c1||x1|=|d1|
|0  a2 b2||x2| |d2|

1st iteration: b0x0 + c0x1 = d0 -> x0 + (c0/b0)x1 = d0/b0 ->

x0 + g0x1 = r0               where g0 = c0/b0        , r0 = d0/b0

2nd iteration:     | a1x0 + b1x1   + c1x2 = d1
from 1st it.: -| a1x0 + a1g0x1        = a1r0
-----------------------------
(b1 - a1g0)x1 + c1x2 = d1 - a1r0

x1 + g1x2 = r1               where g1=c1/(b1 - a1g0) , r1 = (d1 - a1r0)/(d1 - a1r0)

3rd iteration:      | a2x1 + b2x2   = d2
from 2st it. : -| a2x1 + a2g1x2 = a2r2
-----------------------
(b2 - a2g1)x2 = d2 - a2r2
x2 = r2                      where                     r2 = (d2 - a2r2)/(b2 - a2g1)
Finally we have a triangular matrix:
|1  g0 0 ||x0| |r0|
|0  1  g1||x1|=|r1|
|0  0  1 ||x2| |r2|

Condition: ||bi|| > ||ai|| + ||ci||

in this version the c matrix reused instead of g
and             the d matrix reused instead of r and x matrices to report results
*/

n--; // since we start from x0 (not x1)
c[0] /= b[0];
d[0] /= b[0];

for (int i = 1; i < n; i++) {
c[i] /= b[i] - a[i]*c[i-1];
d[i] = (d[i] - a[i]*d[i-1]) / (b[i] - a[i]*c[i-1]);
}

d[n] = (d[n] - a[n]*d[n-1]) / (b[n] - a[n]*c[n-1]);

for (int i = n; i-- > 0;) {
d[i] -= c[i]*d[i+1];
}
}

int main() {
int  n = 4;
double a[4] = { 0, -1, -1, -1 };
double b[4] = { 4,  4,  4,  4 };
double c[4] = {-1, -1, -1,  0 };
double d[4] = { 5,  5, 10, 23 };
// results    { 2,  3,  5, 7  }
solve(a,b,c,d,n);
for (int i = 0; i < n; i++) {
cout << d[i] << endl;
}
cout << endl << "n= " << n << endl << "n is not changed hooray !!";
return 0;
}

The following variant preserves the system of equations for reuse on other inputs. Note the necessity of library calls to allocate and free scratch space - a more efficient implementation for solving the same tridiagonal system on many inputs would rely on the calling function to provide a pointer to the scratch space.

void solve_tridiagonal_in_place_reusable(double x[], const size_t N, const double a[], const double b[], const double c[]) {
size_t in;

/* Allocate scratch space. */
double* cprime = (double*)malloc(sizeof(double) * N);

if (!cprime) {
/* do something to handle error */
}

cprime[0] = c[0] / b[0];
x[0] = x[0] / b[0];

/* loop from 1 to N - 1 inclusive */
for (in = 1; in < N; in++) {
double m = 1.0 / (b[in] - a[in] * cprime[in - 1]);
cprime[in] = c[in] * m;
x[in] = (x[in] - a[in] * x[in - 1]) * m;
}

/* loop from N - 2 to 0 inclusive, safely testing loop end condition */
for (in = N - 1; in-- > 0; )
x[in] = x[in] - cprime[in] * x[in + 1];

/* free scratch space */
free(cprime);
}


### Python

Note that the index $i$ here is zero-based, in other words $i = 0, 1, \dots, N-1$ where $N$ is the number of unknowns.

# note: function also modifies b[] and d[] params while solving
def TDMASolve(a, b, c, d):
n = len(d) # n is the numbers of rows, a and c has length n-1
for i in xrange(n-1):
d[i+1] -= d[i] * a[i] / b[i]
b[i+1] -= c[i] * a[i] / b[i]
for i in reversed(xrange(n-1)):
d[i] -= d[i+1] * c[i] / b[i+1]
return [d[i] / b[i] for i in xrange(n)] # return the solution


### MATLAB

function x = TDMAsolver(a,b,c,d)
%a, b, c are the column vectors for the compressed tridiagonal matrix, d is the right vector
n = length(d); % n is the number of rows

% Modify the first-row coefficients
c(1) = c(1) / b(1);    % Division by zero risk.
d(1) = d(1) / b(1);

for i = 2:n-1
temp = b(i) - a(i) * c(i-1);
c(i) = c(i) / temp;
d(i) = (d(i) - a(i) * d(i-1))/temp;
end

d(n) = (d(n) - a(n) * d(n-1))/( b(n) - a(n) * c(n-1));

% Now back substitute.
x(n) = d(n);
for i = n-1:-1:1
x(i) = d(i) - c(i) * x(i + 1);
end


### Fortran 90

Note that the index $i$ here is one based, in other words $i = 1, 2, \dots, n$ where $n$ is the number of unknowns.

Sometimes it is undesirable to have the solver routine overwrite the tridiagonal coefficients (e.g. for solving multiple systems of equations where only the right side of the system changes), so this implementation gives an example of a relatively inexpensive method of preserving the coefficients.

      subroutine solve_tridiag(a,b,c,d,x,n)
implicit none
!	 a - sub-diagonal (means it is the diagonal below the main diagonal)
!	 b - the main diagonal
!	 c - sup-diagonal (means it is the diagonal above the main diagonal)
!	 d - right part
!	 n - number of equations

integer,intent(in) :: n
real(8),dimension(n),intent(in) :: a,b,c,d
real(8),dimension(n),intent(out) :: x
real(8),dimension(n) :: cp,dp
real(8) :: m
integer i

! initialize c-prime and d-prime
cp(1) = c(1)/b(1)
dp(1) = d(1)/b(1)
! solve for vectors c-prime and d-prime
do i = 2,n
m = b(i)-cp(i-1)*a(i)
cp(i) = c(i)/m
dp(i) = (d(i)-dp(i-1)*a(i))/m
enddo
! initialize x
x(n) = dp(n)
! solve for x from the vectors c-prime and d-prime
do i = n-1, 1, -1
x(i) = dp(i)-cp(i)*x(i+1)
end do

end subroutine solve_tridiag


This subroutine offers an option of overwriting d or not.[1]

      subroutine tdma(n,a,b,c,d,x)
implicit none
integer, intent(in) :: n
real, intent(in) :: a(n), c(n)
real, intent(inout), dimension(n) :: b, d
real, intent(out) :: x(n)
!  --- Local variables ---
integer :: i
real :: q
!  --- Elimination ---
do i = 2,n
q = a(i)/b(i - 1)
b(i) = b(i) - c(i - 1)*q
d(i) = d(i) - d(i - 1)*q
end do
! --- Backsubstitution ---
q = d(n)/b(n)
x(n) = q
do i = n - 1,1,-1
q = (d(i) - c(i)*q)/b(i)
x(i) = q
end do
return
end


## Derivation

The derivation of the tridiagonal matrix algorithm involves manually performing some specialized Gaussian elimination in a generic manner.

Suppose that the unknowns are $x_1,\ldots, x_n$, and that the equations to be solved are:

\begin{align} b_1 x_1 + c_1 x_2 & = d_1;& i & = 1 \\ a_i x_{i - 1} + b_i x_i + c_i x_{i + 1} & = d_i;& i & = 2, \ldots, n - 1 \\ a_n x_{n - 1} + b_n x_n & = d_n;& i & = n. \end{align}

Consider modifying the second ($i = 2$) equation with the first equation as follows:

$(\mbox{equation 2}) \cdot b_1 - (\mbox{equation 1}) \cdot a_2$

which would give:

$(a_2 x_1 + b_2 x_2 + c_2 x_3) b_1 - (b_1 x_1 + c_1 x_2) a_2 = d_2 b_1 - d_1 a_2 \,$
$(b_2 b_1 - c_1 a_2) x_2 + c_2 b_1 x_3 = d_2 b_1 - d_1 a_2 \,$

and the effect is that $x_1$ has been eliminated from the second equation. Using a similar tactic with the modified second equation on the third equation yields:

$(a_3 x_2 + b_3 x_3 + c_3 x_4) (b_2 b_1 - c_1 a_2) - ((b_2 b_1 - c_1 a_2) x_2 + c_2 b_1 x_3) a_3 = d_3 (b_2 b_1 - c_1 a_2) - (d_2 b_1 - d_1 a_2) a_3 \,$
$(b_3 (b_2 b_1 - c_1 a_2) - c_2 b_1 a_3 )x_3 + c_3 (b_2 b_1 - c_1 a_2) x_4 = d_3 (b_2 b_1 - c_1 a_2) - (d_2 b_1 - d_1 a_2) a_3. \,$

This time $x_2$ was eliminated. If this procedure is repeated until the $n^{th}$ row; the (modified) $n^{th}$ equation will involve only one unknown, $x_n$. This may be solved for and then used to solve the $(n - 1)^{th}$ equation, and so on until all of the unknowns are solved for.

Clearly, the coefficients on the modified equations get more and more complicated if stated explicitly. By examining the procedure, the modified coefficients (notated with tildes) may instead be defined recursively:

$\tilde a_i = 0\,$
$\tilde b_1 = b_1\,$
$\tilde b_i = b_i \tilde b_{i - 1} - \tilde c_{i - 1} a_i\,$
$\tilde c_1 = c_1\,$
$\tilde c_i = c_i \tilde b_{i - 1}\,$
$\tilde d_1 = d_1\,$
$\tilde d_i = d_i \tilde b_{i - 1} - \tilde d_{i - 1} a_i.\,$

To further hasten the solution process, $\tilde b_i$ may be divided out (if there's no division by zero risk), the newer modified coefficients notated with a prime will be:

$a'_i = 0\,$
$b'_i = 1\,$
$c'_1 = \frac{c_1}{b_1}\,$
$c'_i = \frac{c_i}{b_i - c'_{i - 1} a_i}\,$
$d'_1 = \frac{d_1}{b_1}\,$
$d'_i = \frac{d_i - d'_{i - 1} a_i}{b_i - c'_{i - 1} a_i}.\,$

This gives the following system with the same unknowns and coefficients defined in terms of the original ones above:

$\begin{array}{lcl} b'_i x_i + c'_i x_{i + 1} = d'_i \qquad &;& \ i = 1, \ldots, n - 1 \\ b'_n x_n = d'_n \qquad &;& \ i = n. \\ \end{array} \,$

The last equation involves only one unknown. Solving it in turn reduces the next last equation to one unknown, so that this backward substitution can be used to find all of the unknowns:

$x_n = d'_n/b'_n\,$
$x_i = (d'_i - c'_i x_{i + 1})/b'_i \qquad ; \ i = n - 1, n - 2, \ldots, 1.$

## Variants

In some situations, particularly those involving periodic boundary conditions, a slightly perturbed form of the tridiagonal system may need to be solved:

\begin{align} a_1 x_{n} + b_1 x_1 + c_1 x_2 & = d_1, \\ a_i x_{i - 1} + b_i x_i + c_i x_{i + 1} & = d_i,\quad\quad i = 2,\ldots,n-1 \\ a_n x_{n-1} + b_n x_n + c_n x_1 & = d_n. \end{align}

In this case, we can make use of the Sherman-Morrison formula to avoid the additional operations of Gaussian elimination and still use the Thomas algorithm. The method requires solving a modified non-cyclic version of the system for both the input and a sparse corrective vector, and then combining the solutions. This can be done efficiently if both solutions are computed at once, as the forward portion of the pure tridiagonal matrix algorithm can be shared.

In other situations, the system of equations may be block tridiagonal (see block matrix), with smaller submatrices arranged as the individual elements in the above matrix system(e.g., the 2D Poisson problem). Simplified forms of Gaussian elimination have been developed for these situations[citation needed].

The textbook Numerical Mathematics by Quarteroni, Sacco and Saleri, lists a modified version of the algorithm which avoids some of the divisions (using instead multiplications), which is beneficial on some computer architectures.

## References

• Press, WH; Teukolsky, SA; Vetterling, WT; Flannery, BP (2007). "Section 2.4". Numerical Recipes: The Art of Scientific Computing (3rd ed.). New York: Cambridge University Press. ISBN 978-0-521-88068-8