# Tridiagonal matrix 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.

Thomas' algorithm is not stable in general, but is so in several special cases, such as when the matrix is diagonally dominant or symmetric positive definite;[1][2] for a more precise characterization of stability of Thomas' algorithm, see Higham Theorem 9.12.[3] If stability is required in the general case, Gaussian elimination with partial pivoting (GEPP) is recommended instead.[2]

## 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.$

## Algorithm (programmatic)

The following is an example of the implementation of this algorithm in the C programming language.

/*
* tridiagonal: Thomas' algorithm implementation
*Inputs: a,b,c are L,D,U vectors respectively
*        d is coefficient vector; is overwritten with solution vector
*        len is length of b et d
*Outputs: Returns 0 (if you want to signal an error at some point, return something else)
*         As above, d is overwritten with answer vector.
*         a,b also modified
*Note: For simplicity's sake b is overwritten with successive denominators (see Method above);
*      it isn't hard to change this if you so wish
*Note 2: We don't check for diagonal dominance, etc.;
*        this is not guaranteed stable
*/
int tridiagonal (double *c, double *b, double *a, double *d, int len)
{
int i;

/*Forward sweep*/
*a /= *b;
for(i=1;i<len-1;i++)
{
b[i] -= (a[i-1]*c[i-1]);
a[i] /=b[i];
}
b[len-1] -= (a[len-2]*c[len-2]);

for(i=1;i<len;i++)
d[i] -= (d[i-1]*a[i-1]);
d[len-1] /= b[len-1];

/*Backward sweep*/
for(i=len-2;i>=0;i--)
{
d[i] -= (d[i+1]*c[i]);
d[i] /= b[i];
}
return 0;

}


## 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

1. ^ Pradip Niyogi (2006). Introduction to Computational Fluid Dynamics. Pearson Education India. p. 76. ISBN 978-81-7758-764-7.
2. ^ a b Biswa Nath Datta (2010). Numerical Linear Algebra and Applications, Second Edition. SIAM. p. 162. ISBN 978-0-89871-765-5.
3. ^ Nicholas J. Higham (2002). Accuracy and Stability of Numerical Algorithms: Second Edition. SIAM. p. 175. ISBN 978-0-89871-802-7.
• 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