CS115 Linear Algebra Review
CS115 Linear Algebra Review
Review
1
https://xkcd.com/1838/
University of Information Technology (UIT) Maths for Computer Science CS115 2 / 78
References
The contents of this document are taken mainly from the following
sources:
I Gilbert Strang. Linear Algebra and Learning from Data.
https://math.mit.edu/~gs/learningfromdata/
I Gilbert Strang. Introduction to Linear Algebra.
http://math.mit.edu/~gs/linearalgebra/
I Gilbert Strang. Linear Algebra for Everyone.
http://math.mit.edu/~gs/everyone/
1 Matrix-Vector Multiplication Ax
2 Matrix-Matrix Multiplication AB
4 Elimination and A = LU
1 Matrix-Vector Multiplication Ax
2 Matrix-Matrix Multiplication AB
4 Elimination and A = LU
4 4
3 3
2 2
1 1
-4 -3 -2 -1 0 1 2 3 4 -4 -3 -2 -1 0 1 2 3 4
-1
-2
-3
-4
d
X d
X
x·y = x i yi = yi xi = y · x
i=1 i=1
x · (y + z) = x · y + x · z
I The dot product of a vector with itself produces the squared Euclidean
norm. The norm defines the vector length and is denoted by k · k:
d
X
2
kxk = x · x = x2i
i=1
I Consider the triangle created by the origin, x, and y. Find the angle
θ between x and y.
I The side lengths of this triangle are: a = kxk, b = kyk, and
c = kx − yk. Using the cosine law, we have:
Definition
With m, n ∈ N, a real-valued (m, n) matrix A is an m · n-tuple of
elements aij , i = 1, . . . , m, j = 1, . . . , n, which is ordered according to a
rectangular scheme consisting of m rows and n columns:
a11 a12 . . . a1n
a21 a22 . . . a2n
A= . .. , aij ∈ R
. .. . .
. . . .
am1 am2 . . . amn
2
https://xkcd.com/184/
University of Information Technology (UIT) Maths for Computer Science CS115 20 / 78
Matrix-Vector Multiplication Ax
Ax
Ax is a linear combination of the columns of A.
a11 a12 ... a1n x1 a11 a12 a1n
a21 a22 ... a2n
x2
a21 a22 a2n
.. .. = x1 .. + x2 .. + · · · + xn ..
.. .. ..
. . . . . . . .
am1 am2 . . . amn xn am1 am2 amn
Ax = x1 a1 + x2 a2 + · · · + xn an
2 3 2 3
x
Ax = 2 4 1 = x1 2 + x2 4
x2
3 7 3 7
Definition
The combinations of the columns fill out the column space of A.
2 3 2 3
x
Ax = 2 4 1 = x1 2 + x2 4
x2
3 7 3 7
I C(A) is plane.
I The plane includes (0, 0, 0), produced when x1 = x2 = 0.
I The plane includes (5, 6, 10) = a1 + a2 and (−1, −2, −4) = a1 − a2 .
Every combination x1 a1 + x2 a2 is in C(A).
I The probability the plane does not include a random point rand(3,1)?
Which points are in the plane?
Ax = b
b is in C(A) exactly when Ax = b has a solution x.
x shows how to express b as a combination of the columns of A.
I Subspaces of R3 :
The zero vector (0, 0, 0).
A line of all vectors x1 a1 .
A plane of all vectors x1 a1 + x2 a2 .
The whole R3 with all vectors x1 a1 + x2 a2 + x3 a3 .
I Vectors a1 , a2 , a3 need to be independent. The only combination
that gives the zero vector is 0a1 + 0a2 + 0a3 .
I The zero vector is in every subspace.
3
https://mathsci2.appstate.edu/ sjg/class/2240/hf14.html
University of Information Technology (UIT) Maths for Computer Science CS115 27 / 78
Independent Columns, Basis, and Ranks of A
Definition
A basis for a subspace is a full set of independent vectors: All vectors in
the space are combinations of the basis vector.
Definition
The rank of a matrix is the dimension of its column space.
University of Information Technology (UIT) Maths for Computer Science CS115 29 / 78
Rank Factorization A = CR
I The matrix C connects to A by a third matrix R: A = CR.
I A ∈ Rm×n , C ∈ Rm×r , R ∈ Rr×n
1 3 8 1 3
1 0 2
A= 1 2 6 = 1 2
= CR
0 1 2
0 1 2 0 1
Definition
R =rref(A) = row-reduced echelon form of A.
University of Information Technology (UIT) Maths for Computer Science CS115 30 / 78
Rank Factorization A = CR
1 3 8 1 3
1 0 2
A= 1 2 6 = 1 2
= CR
0 1 2
0 1 2 0 1
Notice
The number of independent columns = The number of independent rows.
The column space and row space of A both have dimension r.
The column rank of A = The row rank of A.
1 Matrix-Vector Multiplication Ax
2 Matrix-Matrix Multiplication AB
4 Elimination and A = LU
4
https://mathsci2.appstate.edu/ sjg/class/2240/hf14.html
University of Information Technology (UIT) Maths for Computer Science CS115 35 / 78
Compute AB by Inner Products
1 3 4 6
— b∗1 —
| |
.. ∗ ∗ ∗
AB = a1 . . . an = a1 b1 + a2 b2 + · · · + an bn
.
| | — b∗n —
I Example:
1 0 2 4 1 0 2 4 0 0 2 4
= 2 4 + 0 5 = + =
3 1 0 5 3 1 6 12 0 5 6 17
Important Factorizations
1 A = LU : elimination
2 A = QR: orthogonalization
3 S = QΛQ> : eigenvalues and orthonormal eigenvectors
4 A = XΛX −1 : diagonalization
5 A = U ΣV > : Singular Value Decomposition (SVD)
(AB)−1 = B −1 A−1
1 Matrix-Vector Multiplication Ax
2 Matrix-Matrix Multiplication AB
4 Elimination and A = LU
1 2
A= = uv >
3 6
1
I Column space C(A) is the line through u = .
3
> 1
I Row space C(A ) is the line through v = .
2
2
I Nullspace N(A) is the line through x = . Ax = 0.
−1
> 3
I Left nullspace N(A ) is the line through y = . A> y = 0.
−1
4 4
3 Left nullspace 3
Nullspace
2 2
Row space Column space
1 1
1 2
-4 -3 -2 -1 0 1 2 3 4
A= = uv > -4 -3 -2 -1 0 1 2 3 4
-1
3 6 -1
-2 -2
-3 -3
-4 -4
Definition
The column space C(A) contains all combinations of the columns of A.
The row space C(A> ) contains all combinations of the columns of A> .
The nullspace N(A) contains all solutions x to Ax = 0.
The left nullspace N(A> ) contains all solutions y to A> y = 0.
1 −2 −2
B=
3 −6 −6
row 1 0
.. ..
If Ax = 0 then . x = .
row m 0
I x is orthogonal to every row of A.
I Every x in the nullspace of A is orthogonal to the row space of A.
I Every y in the nullspace of A> is orthogonal to the column space of
A.
N(A) ⊥ C(A> ) N(A> )⊥ C(A)
Dimensions n−r r m−r r
1 Matrix-Vector Multiplication Ax
2 Matrix-Matrix Multiplication AB
4 Elimination and A = LU
2x1 + x2 − x3 + 2x4 = 5
3x2 − x3 + 2x4 = −1
− x3 + 4x4 = 11
2x4 = 6
By back substitution, we get
x4 = 3, x3 = 1, x2 = −2, x1 = 1
1 0 0 0
pivot row 1 2 1 −1 2
l21 1 0 0
pivot row 2 = 4
5 −3 6
A=
l31 l32 0 pivot row 3 −2 5 −2 6
1
l41 l42 l43 1 pivot row 4 4 11 −4 8
1 0 0 0 0
l21 0 x x x aij
l31 pivot row 1 + 0 x x x
= lij =
ajj
l41 0 x x x
1 0 0 0 0
2 0 x x x
−1 2 1 −1 2 + 0 x x x
=
2 0 x x x
2 1 −1 2 0 0 0 0
4 2 −2 4 + 0 3 −1 2
=
−2 −1 1 −2 0 6 −3 8
4 2 −2 4 0 9 −2 4
1 0 0 0 pivot row
1 0 0 0 0
l21 1 0 0 pivot row
2 ∗
0 3 −1 2
A= = l1 u1 +
l31 l32 1 0 pivot row
3 0 6 −3 8
l41 l42 l43 1 pivot row
4 0 9 −2 4
0 0 0 0 0
1 0 0 0 0 aij
= l1 u∗1 +
l32 pivot
row 2 + 0 0 x x
lij =
ajj
l42 0 0 x x
0 0 0 0 0
∗
1 0 0 0 0
= l1 u1 + 2 0 3
−1 2 + 0 0 x x
3 0 0 x x
0 0 0 0 0 0 0 0
0 3 −1 2
∗ + 0 0 0 0
= l1 u1 + 0 6 −2
4 0 0 −1 4
0 9 −3 6 0 0 1 −2
−1 0 0 0 x
0 0 0 0 0 0 0 0
0 0 0 0
= l1 u∗1 + l2 u∗2 + + 0 0 0 0
0 0 −1 4 0 0 0 0
0 0 1 −4 0 0 0 2
= l1 u∗1 + l2 u∗2 + l3 u∗3 + l4 u∗4
The third step reduces the 2 × 2 problem to a single number by removing l3 u∗3 .
University of Information Technology (UIT) Maths for Computer Science CS115 54 / 78
Elimination and A = LU
I Start from A b = LU b .
I Elimination produces U L−1 b = U c .
1 Matrix-Vector Multiplication Ax
2 Matrix-Matrix Multiplication AB
4 Elimination and A = LU
(column 1)>
0
> .
.. .
A y= y = ..
(column m)> 0
I The row space C(A> ) is the plane of all vectors β1 a∗1 + β2 a∗2 .
I The nullspace N(A) is the line through u = (0, 0, 1): all vectors β3 u
v1 v1 1 0 0
v = v2 ∈ R3 v = v2 = β1 0 + β2 1 + β3 0
v3 v3 0 0 1
| {z } | {z }
v row v null
dimension
C(A)
dimension C(AT) =r
=r row column
space space
of A row space to column space of A
Axrow = b
Rn 0 0 Rm
= 0
Ax null
nullspace to 0 nullspace
pace
of A nulls of AT
dimension N(A) dimension
=n-r N(AT) =m-r
a> e = 0
a> (b − p) = 0
a> (b − xa) = 0
xa> a = a> b
a> b
x=
a> a
>
I Therefore, p = ax = a a> b
a a
I There is a projection matrix P that p = P b.
aa>
P =
a> a
University of Information Technology (UIT) Maths for Computer Science CS115 63 / 78
Projection onto a Line
aa>
P =
a> a
p = x̂1 a1 + x̂2 a2
= Ax̂
I Find x̂.
I Is P symmetric?
P > = (A(A> A)−1 A> )> = A((A> A)−1 )> A>
= A((A> A)> )−1 A>
= A(A> A)−1 A> = P
Yes.
I Is P 2 = P ?
P 2 = A(A> A)−1 A> A(A> A)−1 A>
= A(A> A)−1 (A> A)(A> A)−1 A>
= A(A> A)−1 A> = P
Yes.
University of Information Technology (UIT) Maths for Computer Science CS115 68 / 78
Q with Orthonormal Columns
2
1
Q>
Q1 = 2 1 Q1 = 1
3
−1
2 2
1 1 0
Q2 = 2 −1 Q>
2 Q2 =
3 0 1
−1 2
2 2 −1 1 0 0
1
Q3 = 2 −1 2 Q>
3 Q3 = 0 1 0
3
−1 2 2 0 0 1
I P is a projection matrix.
Orthogonal Projection
If P 2 = P = P > then P b is the orthogonal projection of b onto the
column space of P .
2 2 −1
1
Q3 = 2 −1 2
3
−1 2 2
I What is P3 b = Q3 Q> 3b ?
I Project b onto the whole space R3 .
I P3 = Q3 Q> 3
3 = I. Thus, P3 b = b. Vector b is in R already.
I The error e is zero!!!
Gram-Smidth algorithm
given vectors a1 , a2 , . . . , ak
for i = 1, . . . , k
1 Orthogonalization. q̃ i = ai − (q T T
1 ai )q 1 − . . . − (q i−1 ai )q i−1
2 Test for linear dependence. If q̃ i = 0, quit.
3 Normalization. q i = q̃ i /kq̃ i k
I i = 2:
q̃ 2 = a2 − (q T
1 a2 )q 1
= (−1, 3, −1, 3) − 4(−1/2, 1/2, −1/2, 1/2) = (1, 1, 1, 1)
1
q2 = q̃ = (1/2, 1/2, 1/2, 1/2)
kq̃ 2 k 2
I i = 3:
q̃ 3 = a3 − (q T T
1 a3 )q 1 − (q 2 a3 )q 2
1 −1/2 1/2 −2
3 1/2 1/2 −2
=5 − 2 −1/2 − 8 1/2 = 2
7 1/2 1/2 2
1
q3 = q̃ = (−1/2, −1/2, 1/2, 1/2)
kq̃ 3 k 3
A = QR
r11 r12 . . . r1n
| | | | | | 0 r
22 . . . r2n
a1 a2 . . . an = q 1 q 2 . . . qn .
.. . . ..
.. . . .
| | | | | |
0 0 . . . rnn
rkk = kq̃ k k
rk−1,k = q T
k−1 ak
x̂ = (AT A)−1 AT b
= ((QR)T (QR))−1 (QR)T b
= (RT QT QR)−1 RT QT b
= (RT R)−1 RT QT b (because QT Q = I)
= R−1 R−T RT QT b
= R−1 QT b