Activating project at `~/Documents/github.com/ucla-biostat-257/2023spring/slides/17-iterative`
Status `~/Documents/github.com/ucla-biostat-257/2023spring/slides/17-iterative/Project.toml`
[6e4b80f9] BenchmarkTools v1.3.2
[42fd0dbc] IterativeSolvers v0.9.2
[b51810bb] MatrixDepot v1.0.10
[b8865327] UnicodePlots v3.5.2
[37e2e46d] LinearAlgebra
[9a3f8284] Random
0.1 Introduction
So far we have considered direct methods for solving linear equations.
Direct methods (flops fixed a priori) vs iterative methods:
Direct method (GE/LU, Cholesky, QR, SVD): good for dense, small to moderate sized \(\mathbf{A}\)
Iterative methods (Jacobi, Gauss-Seidal, SOR, conjugate-gradient, GMRES): good for large, sparse, structured linear system, parallel computing, warm start
0.2 PageRank problem
\(\mathbf{A} \in \{0,1\}^{n \times n}\) the connectivity matrix of webpages with entries \[
a_{ij} = \begin{cases}
1 & \text{if page $i$ links to page $j$} \\
0 & \text{otherwise}
\end{cases}.
\]\(n \approx 10^9\) in May 2017.
\(r_i = \sum_j a_{ij}\) is the out-degree of page \(i\).
Larry Page imagines a random surfer wandering on internet according to following rules:
From a page \(i\) with \(r_i>0\)
with probability \(p\), (s)he randomly chooses a link on page \(i\) (uniformly) and follows that link to the next page
with probability \(1-p\), (s)he randomly chooses one page from the set of all \(n\) pages (uniformly) and proceeds to that page
From a page \(i\) with \(r_i=0\) (a dangling page), (s)he randomly chooses one page from the set of all \(n\) pages (uniformly) and proceeds to that page
The process defines a Markov chain on the space of \(n\) pages. Stationary distribution of this Markov chain gives the ranks (probabilities) of each page.
Stationary distribution is the top left eigenvector of the transition matrix \(\mathbf{P}\) corresponding to eigenvalue 1. Equivalently it can be cast as a linear equation \[
(\mathbf{I} - \mathbf{P}^T) \mathbf{x} = \mathbf{0}.
\]
Gene Golub: Largest matrix computation in world.
GE/LU will take \(2 \times (10^9)^3/3/10^{12} \approx 6.66 \times 10^{14}\) seconds \(\approx 20\) million years on a tera-flop supercomputer!
One round costs \(2n^2\) flops with an unstructured\(\mathbf{A}\). Gain over GE/LU if converges in \(o(n)\) iterations. Saving is huge for sparse or structured\(\mathbf{A}\). By structured, we mean the matrix-vector multiplication \(\mathbf{A} \mathbf{v}\) is fast (\(O(n)\) or \(O(n \log n)\)).
Need to pick \(\omega \in [0,1]\) beforehand, with the goal of improving convergence rate.
0.6 Conjugate gradient method
Conjugate gradient and its variants are the top-notch iterative methods for solving huge, structured linear systems.
A UCLA invention! Hestenes and Stiefel in 60s.
Solving \(\mathbf{A} \mathbf{x} = \mathbf{b}\) is equivalent to minimizing the quadratic function \(\frac{1}{2} \mathbf{x}^T \mathbf{A} \mathbf{x} - \mathbf{b}^T \mathbf{x}\).
MatrixDepot.jl is an extensive collection of test matrices in Julia. After installation, we can check available test matrices by
usingMatrixDepotmdinfo()
[ Info: verify download of index files...
[ Info: reading database
[ Info: adding metadata...
[ Info: adding svd data...
[ Info: writing database
[ Info: used remote sites are sparse.tamu.edu with MAT index and math.nist.gov with HTML index
0.7.1 Currently loaded Matrices
builtin(#)
1 baart
13 fiedler
25 invhilb
37 parter
49 shaw
2 binomial
14 forsythe
26 invol
38 pascal
50 smallworld
3 blur
15 foxgood
27 kahan
39 pei
51 spikes
4 cauchy
16 frank
28 kms
40 phillips
52 toeplitz
5 chebspec
17 gilbert
29 lehmer
41 poisson
53 tridiag
6 chow
18 golub
30 lotkin
42 prolate
54 triw
7 circul
19 gravity
31 magic
43 randcorr
55 ursell
8 clement
20 grcar
32 minij
44 rando
56 vand
9 companion
21 hadamard
33 moler
45 randsvd
57 wathen
10 deriv2
22 hankel
34 neumann
46 rohess
58 wilkinson
11 dingdong
23 heat
35 oscillate
47 rosser
59 wing
12 erdrey
24 hilb
36 parallax
48 sampling
user(#)
Groups
all
local
eigen
illcond
posdef
regprob
symmetric
builtin
user
graph
inverse
random
sparse
Suite Sparse
of
2
2893
MatrixMarket
of
0
498
# List matrices that are positive definite and sparse:mdlist(:symmetric &:posdef &:sparse)
2-element Vector{String}:
"poisson"
"wathen"
# Get help on Poisson matrixmdinfo("poisson")
1 Poisson Matrix (poisson)
A block tridiagonal matrix from Poisson’s equation. This matrix is sparse, symmetric positive definite and has known eigenvalues. The result is of type SparseMatrixCSC.
Input options:
[type,] dim: the dimension of the matrix is dim^2.
Wathen Matrix is a sparse, symmetric positive, random matrix arose from the finite element method. The generated matrix is the consistent mass matrix for a regular nx-by-ny grid of 8-nodes.
Input options:
[type,] nx, ny: the dimension of the matrix is equal to 3 * nx * ny + 2 * nx * ny + 1.
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min … max): 35.250 μs … 11.939 ms┊ GC (min … max): 0.00% … 99.46%
Time (median): 40.250 μs ┊ GC (median): 0.00%
Time (mean ± σ): 42.514 μs ± 119.086 μs┊ GC (mean ± σ): 2.79% ± 0.99%
▇█▄▃▃▁
▁▁▁▁▁▁▆██████▇▆▅▄▃▃▃▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
35.2 μs Histogram: frequency by time 62.9 μs <
Memory estimate: 78.17 KiB, allocs estimate: 2.
2.1.3 Dense solve via Cholesky
# record the Cholesky solutionxchol =cholesky(Afull) \ b;
# dense solve via Cholesky@benchmarkcholesky($Afull) \$b
BenchmarkTools.Trial: 5 samples with 1 evaluation.
Range (min … max): 1.139 s … 1.150 s┊ GC (min … max): 0.00% … 0.02%
Time (median): 1.144 s ┊ GC (median): 0.02%
Time (mean ± σ): 1.145 s ± 4.085 ms┊ GC (mean ± σ): 0.23% ± 0.34%
█ █ █ █ █
█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁█ ▁
1.14 s Histogram: frequency by time 1.15 s <
Memory estimate: 763.02 MiB, allocs estimate: 4.
2.1.4 Jacobi solver
It seems that Jacobi solver doesn’t give the correct answer.
xjacobi =jacobi(A, b)@shownorm(xjacobi - xchol)
norm(xjacobi - xchol) = 498.7144076230415
498.7144076230415
Reading documentation we found that the default value of maxiter is 10. A couple trial runs shows that 30000 Jacobi iterations are enough to get an accurate solution.