24.2 s

Tridiagonal systems

In the previous lecture (nb08) we introudced the discretization matrix for the 1D heat conduction problem. In general form it can be written as a tridiagonal matrix A=(b1c1a2b2c2a3b3cn1aNbN)

and stored in three arrays a,b,c.

6.6 μs

Gaussian elimination using arrays a,b,c as matrix storage ?

  • From what we have seen, this question arises in a quite natural way, and historically, the answer has been given several times and named differently

  • TDMA (tridiagonal matrix algorithm)

  • "Thomas algorithm" (Llewellyn H. Thomas, 1949 (?))

  • "Progonka method" (from Russian "прогонка": "run through"; Gelfand, Lokutsievski, 1952, published 1960)

6.9 μs

Прогонка: derivation

Write solution of Au=f as

aiui1+biui+ciui+1=fi(i=1N)

where we define a1=0, cN=0.

  • For i=1N1, assume there are coefficients αi,βi such that

ui=αi+1ui+1+βi+1

.

  • Re-arranging, we can express ui1 and ui via ui+1:

(aiαiαi+1+biαi+1+ci)ui+1+(aiαiβi+1+aiβi+biβi+1fi)=0

  • This is true for arbitrary u if {aiαiαi+1+biαi+1+ci=0aiαiβi+1+aiβi+biβi+1fi=0

  • Re-arranging gives for i=1N1:

{αi+1=ciaiαi+biβi+1=fiaiβiaiαi+bi

15.7 μs

Прогонка: realization

  • Initialization of forward sweep:

{α2=c1b1β2=fib1

  • Forward sweep: for i=2N1:

{αi+1=ciaiαi+biβi+1=fiaiβiaiαi+bi

  • Initialization of backward sweep: uN=fNaNβNaNαN+bN

  • Backward sweep: for i=N11:

ui=αi+1ui+1+βi+1

12.6 μs

Прогонка: properties

  • N unknowns, one forward sweep, one backward sweep O(N) operations vs. O(N2.75) for algorithm using full matrix

  • No pivoting stability issues

  • Stability for diagonally dominant matrices where |bi|>|ai|+|ci|

  • Stability for symmetric positive definite matrices

  • In fact, this is a realization of Gaussian elimination on a particular data structure.

10.3 μs

Tridiagonal matrices in Julia

In Julia, solution of a tridiagonal system is based on the LU factorization in the LAPACK routine dgtsv which also does pivoting.

4.0 μs
N
5
994 ns
  • LU Factorization in the case of a tridiagonal matrix with random diagonal entries

3.9 μs
A
5×5 Tridiagonal{Float64,Array{Float64,1}}:
 0.186793  0.985412   ⋅         ⋅         ⋅ 
 0.201945  0.336733  0.671338   ⋅         ⋅ 
  ⋅        0.518801  0.191529  0.416203   ⋅ 
  ⋅         ⋅        0.889957  0.147218  0.769763
  ⋅         ⋅         ⋅        0.726546  0.470719
138 ms
LU{Float64,Tridiagonal{Float64,Array{Float64,1}}}
L factor:
5×5 Array{Float64,2}:
 1.0       0.0       0.0       0.0       0.0
 0.924968  1.0       0.0       0.0       0.0
 0.0       0.0       1.0       0.0       0.0
 0.0       0.0       0.0       1.0       0.0
 0.0       0.769797  0.752337  0.420408  1.0
U factor:
5×5 Array{Float64,2}:
 0.201945  0.336733   0.671338  0.0        0.0
 0.0       0.673945  -0.620967  0.0        0.0
 0.0       0.0        0.889957  0.147218   0.769763
 0.0       0.0        0.0       0.726546   0.470719
 0.0       0.0        0.0       0.0       -0.777014
50.1 ms
46.2 μs
329 ms

Solving this system with a positive right hand side can yield negative solution components.

2.6 μs

We see that the in order to maintain stability, pivoting is performed: the LU factorization is performed as PA=LU where P is a permutation matrix. The underlying permutation can be obtained as lu(A).p)

2.6 μs
  • Define a diagonally dominant matrix with random entries with positive main diagonal and nonpositive off-diagonal elements:

3.4 μs
A1
5×5 Tridiagonal{Float64,Array{Float64,1}}:
  2.04343   -0.907038    ⋅          ⋅          ⋅ 
 -0.265936   2.25515   -0.597263    ⋅          ⋅ 
   ⋅        -0.739934   2.24558   -0.790491    ⋅ 
   ⋅          ⋅        -0.701657   2.47384   -0.233202
   ⋅          ⋅          ⋅        -0.662966   2.22106
69.5 ms
LU{Float64,Tridiagonal{Float64,Array{Float64,1}}}
L factor:
5×5 Array{Float64,2}:
  1.0        0.0        0.0        0.0       0.0
 -0.130142   1.0        0.0        0.0       0.0
  0.0       -0.346232   1.0        0.0       0.0
  0.0        0.0       -0.344154   1.0       0.0
  0.0        0.0        0.0       -0.301103  1.0
U factor:
5×5 Array{Float64,2}:
 2.04343  -0.907038   0.0        0.0        0.0
 0.0       2.1371    -0.597263   0.0        0.0
 0.0       0.0        2.03879   -0.790491   0.0
 0.0       0.0        0.0        2.20179   -0.233202
 0.0       0.0        0.0        0.0        2.15084
3.6 μs
4.7 μs

Here we see, that no permutation is needed to maintain stability, confirming the statement made. In this case, the underlying algorithm is equivalent to Progonka, and the resulting LU factorization can be stored in three diagonals.

2.4 μs
5.6 μs

Here we get only nonnegative solution values, though the matrix off-diagonal elements are nonpositive. Later we will see that this is a theorem for this type of matrices.

2.4 μs
5×5 Array{Float64,2}:
 0.519495    0.231457   0.0686099  0.0225583  0.00236853
 0.0678612   0.52144    0.154568   0.0508208  0.00533598
 0.024921    0.191491   0.55307    0.181845   0.019093
 0.00727301  0.0558852  0.16141    0.469004   0.0492435
 0.00217093  0.0166812  0.0481793  0.139993   0.464934
41.9 ms

The inverse is a nonnegative full matrix! This is a theorem as well.

2.4 μs