Tridiagonal matrix algorithm explained

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

aixi-1+bixi+cixi+1=di,

where

a1=0

and

cn=0

.

\begin{bmatrix} b1&c1&&&0\\ a2&b2&c2&&\\ &a3&b3&\ddots&\\ &&\ddots&\ddots&cn-1\\ 0&&&an&bn \end{bmatrix} \begin{bmatrix} x1\\ x2\\ x3\\ \vdots\\ xn \end{bmatrix} = \begin{bmatrix} d1\\ d2\\ d3\\ \vdots\\ dn \end{bmatrix} .

For such systems, the solution can be obtained in

O(n)

operations instead of

O(n3)

required by Gaussian elimination. A first sweep eliminates the

ai

's, and then an (abbreviated) backward substitution produces the solution. Examples of such matrices commonly arise from the discretization of 1D Poisson equation and natural cubic spline interpolation.

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

Method

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

c'i= \begin{cases} \cfrac{ci}{bi},&i=1,\\ \cfrac{ci}{bi-aic'i

}, & i = 2, 3, \dots, n - 1 \end

and

d'i= \begin{cases} \cfrac{di}{bi},&i=1,\\ \cfrac{di-aid'i

}, & i = 2, 3, \dots, n. \end

The solution is then obtained by back substitution:

xn=d'n,

xi=d'i-c'ixi,i=n-1,n-2,\ldots,1.

The method above does not modify the original coefficient vectors, but must also keep track of the new coefficients. If the coefficient vectors may be modified, then an algorithm with less bookkeeping is:

For

i=2,3,...,n,

do

w=\cfrac{ai}{bi-1

},

bi:=bi-wci-1,

di:=di-wdi-1,

followed by the back substitution

xn=\cfrac{dn}{bn},

xi=\cfrac{di-cixi+1

} \quad \text i = n - 1, n - 2, \dots, 1.

The implementation as a C function, which uses scratch space to avoid modifying its inputs for a-c, allowing them to be reused:void thomas(const int X, double x[restrict X], const double a[restrict X], const double b[restrict X], const double c[restrict X], double scratch[restrict X])

Derivation

The derivation of the tridiagonal matrix algorithm is a special case of Gaussian elimination.

Suppose that the unknowns are

x1,\ldots,xn

, and that the equations to be solved are:

\begin{alignat}{4} &&&b1x1&&+c1x2&&=d1\\ &aixi&&+bixi&&+cixi&&=di,i=2,\ldots,n-1\\ &anxn&&+bnxn&&&&=dn. \end{alignat}

Consider modifying the second (

i=2

) equation with the first equation as follows:

(equation2)b1-(equation1)a2

which would give:

(b2b1-c1a2)x2+c2b1x3=d2b1-d1a2.

Note that

x1

has been eliminated from the second equation. Using a similar tactic with the modified second equation on the third equation yields:

(b3(b2b1-c1a2)-c2b1a3)x3+c3(b2b1-c1a2)x4 =d3(b2b1-c1a2)-(d2b1-d1a2)a3.

This time

x2

was eliminated. If this procedure is repeated until the

nth

row; the (modified)

nth

equation will involve only one unknown,

xn

. 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:

\tildeai=0

\tildeb1=b1

\tildebi=bi\tildebi-\tildeciai

\tildec1=c1

\tildeci=ci\tildebi

\tilded1=d1

\tildedi=di\tildebi-\tildediai.

To further hasten the solution process,

\tildebi

may be divided out (if there's no division by zero risk), the newer modified coefficients, each notated with a prime, will be:

a'i=0

b'i=1

c'1=

c1
b1

c'i=

ci
bi-c'iai

d'1=

d1
b1

d'i=

di-d'iai
bi-c'iai

.

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

\begin{array}{lcl} xi+c'ixi=d'i    &;&i=1,\ldots,n-1\\ xn=d'n    &;&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:

xn=d'n

xi=d'i-c'ixi    ;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{alignat}{4} &a1xn&&+b1x1&&+c1x2&&=d1\\ &aixi&&+bixi&&+cixi&&=di,i=2,\ldots,n-1\\ &anxn&&+bnxn&&+cnx1&&=dn. \end{alignat}

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.

If we indicate by:A=\begin b_1 & c_1 & & & a_1 \\ a_2 & b_2 & c_2 & & \\ & a_3 & b_3 & \ddots & \\ & & \ddots & \ddots & c_ \\ c_n & & & a_n & b_n\end,x=\begin x_1 \\ x_2 \\ x_3 \\ \vdots \\ x_n\end,d=\begin d_1 \\ d_2 \\ d_3 \\ \vdots \\ d_n\end

Then the system to be solved is: Ax = d

In this case the coefficients

a1

and

cn

are, generally speaking, non-zero, so their presence does not allow to apply the Thomas algorithm directly. We can therefore consider

B\inRn x

and

u,v\inRn

as following:B=\begin b_1-\gamma & c_1 & & & 0 \\ a_2 & b_2 & c_2 & & \\ & a_3 & b_3 & \ddots & \\ & & \ddots & \ddots & c_ \\ 0 & & & a_n & b_n-\frac\end,u=\begin \gamma \\ 0 \\ 0 \\ \vdots \\ c_n\end,v=\begin 1 \\ 0 \\ 0 \\ \vdots \\ a_1/\gamma\end.Where

\gamma\inR

is a parameter to be chosen. The matrix

A

can be reconstructed as

A=B+uvT

. The solution is then obtained in the following way:[4] first we solve two tridiagonal systems of equations applying the Thomas algorithm:By=d \qquad \qquad Bq=u

Then we reconstruct the solution

x

using the Shermann-Morrison formula:\beginx &=A^d=(B+uv^T)^d=B^d-\fracd=y-\frac\end

The implementation as a C function, which uses scratch space to avoid modifying its inputs for a-c, allowing them to be reused:void cyclic_thomas(const int X, double x[restrict X], const double a[restrict X], const double b[restrict X], const double c[restrict X], double cmod[restrict X], double u[restrict X])

There is also another way to solve the slightly perturbed form of the tridiagonal system considered above. Let us consider two auxiliary linear systems of dimension

(n-1) x (n-1)

:\begin \qquad \ \ \ \ \ b_2 u_ + c_2 u_3 &= d_2 \\ a_3 u_2 + b_3 u_3 + c_3 u_4 &= d_3 \\ a_i u_ + b_i u_i + c_i u_ &= d_i\\ \dots \\ a_n u_+ b_n u_n\qquad &= d_n \,.\end\quad i = 4, \ldots, n - 1 \qquad \qquad

\begin \qquad \ \ \ \ \ b_2 v_ + c_2 v_3 &= -a_2 \\ a_3 v_2 + b_3 v_3 + c_3 v_4 &= 0 \\ a_i u_ + b_i u_i + c_i u_ &= 0\\ \dots \\ a_n v_+ b_n v_n\qquad &= -c_n \,.\end\quad i = 4, \ldots, n - 1

For convenience, we additionally define

u1=0

and

v1=1

. We can now find the solutions

\{u2,u3...,un\}

and

\{v2,v3...,vn\}

applying Thomas algorithm to the two auxiliary tridiagonal system.

The solution

\{x1,x2...,xn\}

can be then represented in the form:x_i = u_i + x_1 v_i \qquad i=1,2,\dots, n

Indeed, multiplying each equation of the second auxiliary system by

x1

, adding with the corresponding equation of the first auxiliary system and using the representation

xi=ui+x1vi

, we immediately see that equations number

2

through

n

of the original system are satisfied; it only remains to satisfy equation number

1

. To do so, consider formula for

i=2

and

i=n

and substitute

x2=u2+x1v2

and

xn=un+x1vn

into the first equation of the original system. This yields one scalar equation for

x1

: b_1x_1+c_1(u_2+x_1v_2)+a_1(u_n+x_1v_n) = d_1

As such, we find: x_1 = \frac

The implementation as a C function, which uses scratch space to avoid modifying its inputs for a-c, allowing them to be reused:void cyclic_thomas(const int X, double x[restrict X], const double a[restrict X], const double b[restrict X], const double c[restrict X], double cmod[restrict X], double v[restrict X])

In both cases the auxiliary systems to be solved are genuinely tri-diagonal, so the overall computational complexity of solving system

Ax=d

remains linear with the respect to the dimension of the system

n

, that is

O(n)

arithmetic operations.

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.[5]

The textbook Numerical Mathematics by Alfio 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.

Parallel tridiagonal solvers have been published for many vector and parallel architectures, including GPUs[6] [7]

For an extensive treatment of parallel tridiagonal and block tridiagonal solvers see [8]

References

  1. Book: Pradip Niyogi . Introduction to Computational Fluid Dynamics . 2006 . Pearson Education India . 978-81-7758-764-7 . 76.
  2. Book: Nicholas J. Higham . Accuracy and Stability of Numerical Algorithms: Second Edition . 2002 . SIAM . 978-0-89871-802-7 . 175.
  3. Book: Biswa Nath Datta . Numerical Linear Algebra and Applications, Second Edition . 2010 . SIAM . 978-0-89871-765-5 . 162.
  4. Batista . Milan . Ibrahim Karawia . Abdel Rahman A. . 2009 . The use of the Sherman–Morrison–Woodbury formula to solve cyclic block tri-diagonal and cyclic block penta-diagonal linear systems of equations . Applied Mathematics and Computation . 210 . 2 . 558–563 . 10.1016/j.amc.2009.01.003 . 0096-3003.
  5. Book: Alfio Quarteroni. Quarteroni. Alfio . Sacco. Riccardo . Saleri. Fausto . 2007. Numerical Mathematics. Springer, New York. 978-3-540-34658-6 . Section 3.8.
  6. Chang. L.-W.. Hwu. W,-M.. A guide for implementing tridiagonal solvers on GPUs. Numerical Computations with GPUs . Springer. 978-3-319-06548-9. V. Kidratenko. 2014.
  7. Venetis. I.E. . Kouris. A.. Sobczyk. A.. Gallopoulos. E.. Sameh. A.. A direct tridiagonal solver based on Givens rotations for GPU architectures. Parallel Computing. 49. 101–116. 2015. 10.1016/j.parco.2015.03.008.
  8. Book: Gallopoulos. E. . Philippe. B.. Sameh. A.H. . 2016. Parallelism in Matrix Computations. Springer. 978-94-017-7188-7. Chapter 5.