Scientific Computing, TU Berlin, WS 2019/2020, Lecture 05
Jürgen Fuhrmann, WIAS Berlin
Let $f,g: \mathbb V \to \mathbb R^+$ be some functions, where $\mathbb V=\mathbb N$ or $\mathbb V=\mathbb R$.
Write $$f(x)=O(g(x)) \quad (x\to\infty)$$ if there exist a constant $C>0$ and $x_0\in \mathbb V$ such that $$ \forall x>x_0, \quad |f(x)|\leq C|g(x)|$$
Often, one skips the part "$(x \to \infty)$"
Solve $Ax=b$ by Cramer's rule
\begin{align*} x_i=\left| \begin{matrix} a_{11}&a_{12}&\ldots&a_{1i-1}&b_1&a_{1i+1}&\ldots&a_{1N}\\ a_{21}& &\ldots& &b_2& &\ldots&a_{2N}\\ \vdots& & & &\vdots& & &\vdots\\ a_{N1}& &\ldots& &b_N& &\ldots&a_{NN} \end{matrix}\right| / |A| \quad (i=1\dots N) \end{align*}
This takes $O(N!)$ operations...
Step 1: $\mathrm{eqn}_2 \leftarrow \mathrm{eqn}_2-2\,\mathrm{eqn}_1$, $\mathrm{eqn}_3 \leftarrow \mathrm{eqn}_3-\frac12\,\mathrm{eqn}_1$
\begin{align*} \left(\begin{matrix} 6&-2 &2\\ 0 &-4 &2\\ 0 &-12&2 \end{matrix} \right) x= \left(\begin{matrix} 16\\ -6\\ -27 \end{matrix}\right) \end{align*}Step 2: $\mathrm{eqn}_3 \leftarrow \mathrm{eqn}_3-3\,\mathrm{eqn}_2$ \begin{align*} \left(\begin{matrix} 6&-2 &2\\ 0 &-4 &2\\ 0 & 0&-4 \end{matrix}\right) x= \left(\begin{matrix} 16\\ -6\\ -9 \end{matrix}\right) \end{align*}
Solve upper triangular system \begin{align*} \left(\begin{matrix} 6&-2 &2\\ 0 &-4 &2\\ 0 &0&-4 \end{matrix}\right) x= \left(\begin{matrix} 16\\ -6\\ -9 \end{matrix}\right) \end{align*} \begin{align*} -4 x_3&= -9 &&&\Rightarrow x_3&= \frac94\\ -4 x_2 + 2 x_3 &= -6 &\Rightarrow -4 x_2 &= -\frac{21}{2} & \Rightarrow x_2&=\frac{21}{8}\\ 6x_1 -2 x_2 + 2 x_3 &= 2 &\Rightarrow 6x_1 &= 2+ \frac{21}{4}-\frac{18}{4}=\frac{11}{4} & \Rightarrow x_1&=\frac{11}{4}\\ \end{align*}
Pass 1 expressed in matrix operation \begin{align*} L_1Ax= \left(\begin{matrix} 6&-2 &2\\ 0 &-4 &2\\ 0 &-12&2 \end{matrix}\right) x&= \left(\begin{matrix} 16\\ -6\\ -27 \end{matrix}\right) =L_1 b, & L_1&= \left(\begin{matrix} 1&0&0\\ -2&1&0\\ -\frac12&0&1 \end{matrix}\right) \end{align*}\begin{align*} L_2L_1Ax= \left(\begin{matrix} 6&-2 &2\\ 0 &-4 &2\\ 0 & 0&-4 \end{matrix}\right) x&= \left(\begin{matrix} 16\\ -6\\ -9 \end{matrix}\right) =L_2L_1 b, & L_2&= \left(\begin{matrix} 1&0&0\\ 0&1&0\\ 0&-3&1 \end{matrix}\right) \end{align*}
Solve $Ax=b$:
using LinearAlgebra
ϵ=1.0e-5
A=[ϵ 1 ; 1 1]
x=[1,1]
b=[1,2]
@show A*x-b
$\Rightarrow$ $x_2=1$ $\Rightarrow$ $\epsilon x_1 +1 =1$ $\Rightarrow$ $x_1=0$
ϵ=1.0e-5
A=[ϵ 1 ; 0 1-1/ϵ]
x=[1,1]
b=[1+ϵ,2-(1+ϵ)/ϵ]
@show A*x-b;
Partial Pivoting
on top'' as the
pivot'' ϵ=1.0e-5
A=[1 1 ; 0 1-ϵ]
x=[1,1]
b=[2,1-ϵ]
@show A*x-b;
How does Julia handle the LU factorization ?
ϵ=1.0e-5
A=[ϵ 1 ; 1 1]
b=[1,2]
x=A\b
@show x;
@show A*x-b;
It seems it does something right ...
Alu=lu(A)
LxU=Alu.L*Alu.U
@show LxU
@show LxU[Alu.p,:]
@show Alu.p
BLAS, LAPACK for Float32, Float64
BLAS: Basic Linear Algebra Subprograms (http://www.netlib.org/blas/)
LAPACK: Linear Algebra PACKage (http://www.netlib.org/lapack/)
dgetrf
: LU factorization dgetrs
: LU solve Used in overwhelming number of codes (e.g. matlab, scipy etc.). Also, C++ matrix libraries use these routines. Unless there is special need, they should be used.
Gaussian elimination using arrays $a,b,c$ as matrix storage ?
Initialization: $\begin{cases} \alpha_{2}&= -\frac{c_1}{b_1}\\ \beta_{2} &= \frac{f_i}{b_1} \end{cases}$
Forward sweep: for $i=2\dots N-1$: $\begin{cases} \alpha_{i+1}&= -\frac{c_i}{a_i\alpha_i+b_i}\\ \beta_{i+1} &= \frac{f_i - a_i\beta_i}{a_i\alpha_i+b_i} \end{cases}$
$u_N=\frac{f_N - a_N\beta_N}{a_N\alpha_N+b_N}$
Diagonally dominant matrix
N=5
a=[0.1 for i=1:N-1]
b=[0.5 for i=1:N]
c=[0.1 for i=1:N-1]
A=Tridiagonal(a,b,c)
Alu=lu(A)
@show Alu.p
@show Alu.L*Alu.U
rhs=[1.0 for i=1:N]
x=A\rhs
@show A*x-rhs
Random matrix $\Rightarrow$ pivoting
a=randn(N-1)
b=randn(N)
c=randn(N-1)
A=Tridiagonal(a,b,c)
Alu=lu(A)
@show Alu.p
@show Alu.L*Alu.U
rhs=[1.0 for i=1:N]
x=A\rhs
@show A*x-rhs
used dgtrsv
from LAPACK
(aka Compressed Sparse Row (CSR) or IA-JA etc.)
AA
, length nnz, containing all nonzero elements row by row JA
, length nnz, containing the column indices of the elements of AA
IA
, length N+1, containing the start indizes of each row in the arrays IA
and JA
and IA[N+1]=nnz+1
\begin{align*} A&= \left(\begin{matrix} 1.& 0. & 0.& 2.& 0.\\ 3.& 4. & 0.& 5.& 0.\\ 6.& 0. & 7.& 8.& 9.\\ 0.& 0. & 10.& 11. & 0.\\ 0.& 0. & 0.& 0.& 12. \end{matrix}\right)\\ \end{align*} (Source: J. Poulson, PhD thesis, http://hdl.handle.net/2152/ETD-UT-2012-12-6622)
Julia code
using SparseArrays
N=100
A=sprand(N,N,0.5) # random matrix with probaility 0.5 that $A_{ij}$ is nonzero
Alu=lu(A)
@show Alu.p
b=ones(N)
x=A\b
@show norm(A*x-b)
using Plots
@show nnz(A)
spy(A)
LxU=Alu.L*Alu.U
@show nnz(LxU)
spy(LxU)
This notebook was generated using Literate.jl.