Tridiagonal matrix algorithm
In numerical linear algebra, the tridiagonal matrix algorithm (TDMA), 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
where and . In matrix form, this system is written as
For such systems, the solution can be obtained in operations instead of required by Gaussian elimination. A first sweep eliminates the '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.
Method
The first step consists of modifying the coefficients as follows, denoting the new modified coefficients with primes:
and:
This is the forward sweep. The solution is then obtained by back substitution:
Implementation in C
The following C99 function will solve a general tridiagonal system (though it will destroy the input vectors b and v in the process). Note that the index here is zero based, in other words where is the number of unknowns.
void solveMatrix (int n, double *a, double *b, double *c, double *v, double *x)
{
/**
* n - number of equations
* a - sub-diagonal (means it is the diagonal below the main diagonal) -- indexed from 1..n-1
* b - the main diagonal
* c - sup-diagonal (means it is the diagonal above the main diagonal) -- indexed from 0..n-2
* v - right part
* x - the answer
*/
for (int i = 1; i < n; i++)
{
double m = b[i] - c[i-1] * a[i];
c[i] = c[i] - m*c[i-1];
v[i] = v[i] - m*v[i-1];
}
x[n-1] = v[n-1]/b[n-1];
for (int i = n - 2; i >= 0; --i)
x[i] = (v[i] - c[i] * x[i+1]) / b[i];
}
Implementation in Matlab
Note that the index here is one based, in other words where is the number of unknowns.
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(b); % 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); % Division by zero would imply a singular matrix.
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
end
Implementation in Fortran 90
Note that the index here is one based, in other words where 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,v,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)
! v - right part
! x - the answer
! n - number of equations
integer,intent(in) :: n
real(8),dimension(n),intent(in) :: a,b,c,v
real(8),dimension(n),intent(out) :: x
real(8),dimension(n) :: bp,vp
real(8) :: m
integer i
! Make copies of the b and v variables so that they are unaltered by this sub
bp(1) = b(1)
vp(1) = v(1)
!The first pass (setting coefficients):
firstpass: do i = 2,n
m = a(i)/bp(i-1)
bp(i) = b(i) - m*c(i-1)
vp(i) = v(i) - m*vp(i-1)
end do firstpass
x(n) = vp(n)/bp(n)
!The second pass (back-substition)
backsub:do i = n-1, 1, -1
x(i) = (vp(i) - c(i)*x(i+1))/bp(i)
end do backsub
end subroutine solve_tridiag
Derivation
The derivation of the tridiagonal matrix algorithm involves manually performing some specialized Gaussian elimination in a generic manner.
Suppose that the unknowns are , and that the equations to be solved are:
Consider modifying the second () equation with the first equation as follows:
which would give:
and the effect is that has been eliminated from the second equation. Using a similar tactic with the modified second equation on the third equation yields:
This time was eliminated. If this procedure is repeated until the row; the (modified) equation will involve only one unknown, . This may be solved for and then used to solve the 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:
To further hasten the solution process, may be divided out (if there's no division by zero risk), the newer modified coefficients notated with an asterisk will be:
This gives the following system with the same unknowns and coefficients defined in terms of the original ones above:
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:
Variants
In some situations, particularly those involving periodic boundary conditions, a slightly perturbed form of the tridiagonal system may need to be solved:
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.
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
- Conte, S.D., and deBoor, C. (1972). Elementary Numerical Analysis. McGraw-Hill, New York.
{{cite book}}
: CS1 maint: multiple names: authors list (link) - This article incorporates text from the article Tridiagonal_matrix_algorithm_-_TDMA_(Thomas_algorithm) on CFD-Wiki that is under the GFDL license.
- 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