2.1 Vectors and Linear Equations
The central problem of linear algebra is to solve a system of linear equations which means that the unknown are only multiplied by numbers–we never see x x x times y y y.
- The column picture of A x = b : Ax = b: Ax=b: a combination of n n n columns of A produces the vector b b b.
- The row picture of A x = b : Ax=b: Ax=b: m m m equations from m m m rows give m m m planes meeting at x x x.
[
1
−
2
3
2
]
[
x
y
]
=
[
1
11
]
\left[ \begin{matrix} 1 & -2\\ 3 & 2 \end{matrix} \right]\left[\begin{matrix}x\\y\end{matrix}\right]=\left[\begin{matrix}1\\11\end{matrix}\right]
[13−22][xy]=[111]
The column picture:
x
[
1
3
]
+
y
[
−
2
2
]
=
[
1
11
]
x\left[\begin{matrix}1\\3\end{matrix}\right] + y\left[\begin{matrix}-2\\2\end{matrix}\right]=\left[\begin{matrix}1\\11\end{matrix}\right]
x[13]+y[−22]=[111]
The row picture:
x
−
2
y
=
1
3
x
+
2
y
=
11
x-2y = 1\\ 3x+2y=11
x−2y=13x+2y=11
The column picture is done in numpy
, when you create new matrix.
np.linalg.inv(A)
calculate the inverse of matrix
A
A
A.
>>> import numpy as np
>>> A = np.array([[1,3],[-2,2]])# The column picture
>>> b = np.array([1,11])
>>> b.dot(np.linalg.inv(A))
array([3., 1.])
>>> np.dot(b,np.linalg.inv(A))
array([3., 1.])
Three Equations in Three Unkonwns
A
x
=
b
Ax = b
Ax=b
x
+
2
y
+
3
z
=
6
2
x
+
5
y
+
2
z
=
4
6
x
−
3
y
+
z
=
2
x+2y+3z=6\\ 2x+5y+2z=4\\ 6x-3y+z=2
x+2y+3z=62x+5y+2z=46x−3y+z=2
The Row Picture: Two intersected planes make one line which is intersected with the third plane. This process can do the result.
The Column Picture:
x
[
1
2
6
]
+
y
[
2
5
−
3
]
+
z
[
3
2
1
]
=
[
6
4
2
]
x\left[\begin{matrix} 1\\2\\6 \end{matrix}\right] + y\left[\begin{matrix} 2\\5\\-3\end{matrix}\right] + z\left[\begin{matrix} 3\\2\\1 \end{matrix}\right]=\left[\begin{matrix} 6\\4\\2 \end{matrix}\right]
x⎣⎡126⎦⎤+y⎣⎡25−3⎦⎤+z⎣⎡321⎦⎤=⎣⎡642⎦⎤
The result should be:
[
x
y
z
]
=
[
0
0
2
]
\left[\begin{matrix} x\\y\\z \end{matrix}\right] = \left[\begin{matrix} 0\\0\\2 \end{matrix}\right]
⎣⎡xyz⎦⎤=⎣⎡002⎦⎤
>>> import numpy as np
>>> A = np.array([[1,2,6],[2,5,-3],[3,2,1]])
>>> b = np.array([6,4,2])
>>> b.dot(np.linalg.inv(A))
array([0.00000000e+00, 2.77555756e-17, 2.00000000e+00])
The matrix form merge the row picture and column picture.
[
1
2
3
2
5
2
6
−
3
1
]
[
x
y
z
]
=
[
6
4
2
]
\left[\begin{matrix}1&2&3\\ 2&5&2\\ 6&-3&1 \end{matrix}\right]\left[\begin{matrix} x\\y\\z\end{matrix}\right]=\left[\begin{matrix} 6\\4\\2 \end{matrix}\right]
⎣⎡12625−3321⎦⎤⎣⎡xyz⎦⎤=⎣⎡642⎦⎤
identity matrix
I
\bm{I}
I:
I
x
=
x
\bm{I}\bm{x} = \bm{x}
Ix=x
Whatever vector identity matrix multiplies, that vector is not changed.
I
=
[
1
0
0
0
1
0
0
0
1
]
\bm{I} = \left[\begin{matrix}1&0&0\\0&1&0\\0&0&1\end{matrix}\right]
I=⎣⎡100010001⎦⎤
Except for the Identity Matrix, the following matrix are all interesting.
- Exchange matrix
[ 0 1 1 0 ] [ x y ] = x [ 0 1 ] + y [ 1 0 ] = [ x ∗ 0 + y ∗ 1 x ∗ 1 + y ∗ 0 ] = [ y x ] \left[\begin{matrix} 0&1\\1&0\end{matrix}\right]\left[\begin{matrix} x\\y\end{matrix}\right]=x\left[\begin{matrix} 0\\1\end{matrix}\right] + y\left[\begin{matrix}1\\0 \end{matrix}\right]=\left[\begin{matrix}x*0 + y*1\\x*1+y*0 \end{matrix}\right]=\left[\begin{matrix}y\\x \end{matrix}\right] [0110][xy]=x[01]+y[10]=[x∗0+y∗1x∗1+y∗0]=[yx] - Rotate every vector by 90°
[ 0 1 − 1 0 ] [ x y ] = x [ 0 − 1 ] + y [ 1 0 ] = [ x ∗ 0 + y ∗ 1 x ∗ ( − 1 ) + y ∗ 0 ] = [ y − x ] \left[\begin{matrix} 0&1\\-1&0 \end{matrix}\right]\left[\begin{matrix} x\\y \end{matrix}\right]=x\left[\begin{matrix}0\\-1\end{matrix}\right] + y\left[\begin{matrix} 1\\0 \end{matrix}\right]=\left[\begin{matrix} x*0+y*1\\x*(-1) + y*0 \end{matrix}\right]=\left[\begin{matrix}y\\-x \end{matrix}\right] [0−110][xy]=x[0−1]+y[10]=[x∗0+y∗1x∗(−1)+y∗0]=[y−x] - Rotate every vector by 180°
[ 0 − 1 − 1 0 ] [ x y ] = x [ 0 − 1 ] + y [ − 1 0 ] = [ x ∗ 0 + y ∗ ( − 1 ) x ∗ ( − 1 ) + y ∗ 0 ] = [ − y − x ] \left[\begin{matrix} 0&-1\\-1&0 \end{matrix}\right]\left[\begin{matrix} x\\y\end{matrix}\right]=x\left[\begin{matrix} 0\\-1 \end{matrix}\right] + y\left[\begin{matrix} -1\\0 \end{matrix}\right]=\left[\begin{matrix} x*0 + y * (-1)\\x*(-1) + y*0\end{matrix}\right]=\left[\begin{matrix} -y\\-x \end{matrix}\right] [0−1−10][xy]=x[0−1]+y[−10]=[x∗0+y∗(−1)x∗(−1)+y∗0]=[−y−x] - Rotate every vector through 45°
[ ( 2 ) 2 − ( 2 ) 2 ( 2 ) 2 ( 2 ) 2 ] [ x y ] = x [ ( 2 ) 2 ( 2 ) 2 ] + y [ − ( 2 ) 2 ( 2 ) 2 ] = [ x ( 2 ) 2 − y ( 2 ) 2 x ( 2 ) 2 + y ( 2 ) 2 ] \left[\begin{matrix} \frac{\sqrt(2)}{2} & -\frac{\sqrt(2)}{2} \\\frac{\sqrt(2)}{2}&\frac{\sqrt(2)}{2} \end{matrix}\right]\left[\begin{matrix} x\\y \end{matrix}\right]=x\left[\begin{matrix} \frac{\sqrt(2)}{2}\\\frac{\sqrt(2)}{2} \end{matrix}\right] + y\left[\begin{matrix} -\frac{\sqrt(2)}{2}\\\frac{\sqrt(2)}{2} \end{matrix}\right]=\left[\begin{matrix} x\frac{\sqrt(2)}{2} -y\frac{\sqrt(2)}{2} \\ x\frac{\sqrt(2)}{2}+y\frac{\sqrt(2)}{2} \end{matrix}\right] ⎣⎡2(2)2(2)−2(2)2(2)⎦⎤[xy]=x⎣⎡2(2)2(2)⎦⎤+y⎣⎡−2(2)2(2)⎦⎤=⎣⎡x2(2)−y2(2)x2(2)+y2(2)⎦⎤
All these problems are solved by the column picture.
Row Picture: Each equation in A x = b Ax=b Ax=b gives a line(n = 2) or a plane(n = 3) or a “hyperplane”(n > 3). They intersect at the solution or solutions, if any.
2.2 The Idea of Elimination
The corner entry a 11 a_{11} a11 is the first “pivot” and the ratio a 21 / a 11 a_{21}/a_{11} a21/a11 is the first “multiplier.”
Elimination breaks down if zero appears in the pivot. Exchanging two equations may save it.
2.4 Rules for Matrix Operations
A B = [ a b c d ] [ E F G H ] = [ a c ] [ E F ] + [ b d ] [ G H ] = [ a E a F c E c F ] + [ b G b H d G d H ] = [ a E + b G a F + b H c E + d G c F + d H ] AB = \left[\begin{matrix} a&b\\c&d \end{matrix}\right]\left[\begin{matrix} E&F\\G&H \end{matrix}\right]=\left[\begin{matrix} a\\c \end{matrix}\right]\left[\begin{matrix} E&F \end{matrix}\right]+\left[\begin{matrix} b\\d \end{matrix}\right]\left[\begin{matrix} G&H \end{matrix}\right]=\left[\begin{matrix} aE&aF\\cE&cF \end{matrix}\right] + \left[\begin{matrix} bG&bH\\dG&dH \end{matrix}\right] = \left[\begin{matrix} aE+bG&aF+bH\\cE+dG&cF+dH \end{matrix}\right] AB=[acbd][EGFH]=[ac][EF]+[bd][GH]=[aEcEaFcF]+[bGdGbHdH]=[aE+bGcE+dGaF+bHcF+dH]
2.5 Inverse Matrices
Calculating A − 1 A^{-1} A−1 by Gauss-Jordan Eliminations
A = [ 2 − 1 0 − 1 2 − 1 0 − 1 2 ] A = \left[\begin{matrix} 2&-1&0\\-1&2&-1\\0&-1&2 \end{matrix}\right] A=⎣⎡2−10−12−10−12⎦⎤
A
I
=
[
2
−
1
0
1
0
0
−
1
2
−
1
0
1
0
0
−
1
2
0
0
1
]
→
e
l
i
m
i
n
a
t
i
o
n
AI = \left[\begin{matrix} 2&-1&0&1&0&0\\-1&2&-1&0&1&0\\0&-1&2&0&0&1 \end{matrix}\right]\rightarrow elimination
AI=⎣⎡2−10−12−10−12100010001⎦⎤→elimination
→
[
1
0
0
3
4
1
2
1
4
0
1
1
1
2
1
1
1
0
0
1
1
4
1
2
3
4
]
\rightarrow \left[\begin{matrix} 1&0&0& \frac{3}{4}&\frac{1}{2}&\frac{1}{4} \\0&1&1& \frac{1}{2}&1&\frac{1}{1} \\0&0&1& \frac{1}{4}&\frac{1}{2}&\frac{3}{4} \end{matrix}\right]
→⎣⎡10001001143214121121411143⎦⎤
>>> import numpy as np
>>> import sympy as sm
>>> I = np.identity(3, dtype = int)
>>> A = [[2,-1,0], [-1,2,-1],[0,-1,2]]
>>> aug_matrix = sm.Matrix(np.concatenate((A, I), axis=1))
>>> aug_matrix
Matrix([
[ 2, -1, 0, 1, 0, 0],
[-1, 2, -1, 0, 1, 0],
[ 0, -1, 2, 0, 0, 1]])
>>> # Elimination on the augmented matrix [A, I] to the
>>> # reduced echelon form阶梯形矩阵
>>> R, rref_pivots = aug_matrix.rref()
>>> # Pick X = inverse(A) fromthe last n columns of R
>>> R[:,3:]
Matrix([
[3/4, 1/2, 1/4],
[1/2, 1, 1/2],
[1/4, 1/2, 3/4]])
Diagonally dominant matrices are invertible. The diagonally dominant matrices are needed to be following the formula.
∣
a
i
j
∣
>
∑
j
≠
i
∣
a
i
j
∣
|a_{ij}| > \sum_{j\neq i}|a_{ij}|
∣aij∣>j=i∑∣aij∣
like the following matrix:
[
3
1
1
1
3
1
1
1
3
]
\left[\begin{matrix} 3&1&1\\1&3&1\\1&1&3 \end{matrix}\right]
⎣⎡311131113⎦⎤
Some matrices which are not diagonally dominant matrices are also invertable . like the following matrix:
[
2
1
1
1
2
1
1
1
3
]
\left[\begin{matrix} 2&1&1\\1&2&1\\1&1&3 \end{matrix}\right]
⎣⎡211121113⎦⎤
The inverse of a triangular difference matrix
A
A
A is a triangular sum matrix
S
S
S.
A
I
=
[
1
0
0
1
0
0
−
1
1
0
0
1
0
0
−
1
1
0
0
1
]
→
[
1
0
0
1
0
0
0
1
0
1
1
0
0
0
1
1
1
1
]
=
[
I
A
−
1
]
AI=\left[\begin{matrix} 1&0&0&1&0&0\\-1&1&0&0&1&0\\0&-1&1&0&0&1 \end{matrix}\right] \rightarrow\left[\begin{matrix} 1&0&0&1&0&0\\0&1&0&1&1&0\\0&0&1&1&1&1 \end{matrix}\right]=[I A^{-1}]
AI=⎣⎡1−1001−1001100010001⎦⎤→⎣⎡100010001111011001⎦⎤=[IA−1]
A
A
A is difference matrix.
A
−
1
A^{-1}
A−1 is sum matrix.
2.6 Elimination = = = Factorization
>>> from scipy.linalg import pascal, cholesky
>>> # create 4*4 pascal matrix
>>> A = pascal(4)
>>> # Cholesky factorization
>>> L = cholesky(A, lower=True)
>>> U = cholesky(A, lower = False)
>>> # check the the factorization of A, A = LU
>>> L.dot(U) == A
array([[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True]])
>>> A
array([[ 1, 1, 1, 1],
[ 1, 2, 3, 4],
[ 1, 3, 6, 10],
[ 1, 4, 10, 20]], dtype=uint64)
>>> L
array([[1., 0., 0., 0.],
[1., 1., 0., 0.],
[1., 2., 1., 0.],
[1., 3., 3., 1.]])
>>> U
array([[1., 1., 1., 1.],
[0., 1., 2., 3.],
[0., 0., 1., 3.],
[0., 0., 0., 1.]])
2.7 Transpose and Permutations
( A B ) T = B T A T (AB)^T = B^TA^T (AB)T=BTAT
( A − 1 ) T = ( A T ) − 1 (A^{-1})^T = (A^T)^{-1} (A−1)T=(AT)−1
A symmetric matrix has S T = S S^T = S ST=S. This means that S j i = S i j S_{ji} =S_{ij} Sji=Sij
For permutation matrix P P P, P − 1 = P T P^{-1} = P^T P−1=PT
How to find the permutation matrix
P
P
P in the formula
P
A
=
L
U
PA= LU
PA=LU?
Look down the column for the largest pivot.
>>> from scipy.linalg import lu
>>> import numpy as np
>>> A = np.array([[0,1,1], [1,2,1], [2,7,9]])
>>> P, L, U = lu(A)
>>> A
array([[0, 1, 1],
[1, 2, 1],
[2, 7, 9]])
>>> P.dot(L.dot(U))
array([[0., 1., 1.],
[1., 2., 1.],
[2., 7., 9.]])
至此本书内容最多的一章啃完了!!!