Professional Documents
Culture Documents
线性代数
线性代数
27-Sep-2018
Another, very in-depth linear algebra review from CS229 is available here:
http://cs229.stanford.edu/section/cs229-linalg.pdf
And a video discussion of linear algebra from EE263 is here (lectures 3 and 4):
https://see.stanford.edu/Course/EE263
1
Stanford University
Outline
• Vectors and matrices
27-Sep-2018
• Matrix rank
• Eigenvalues and Eigenvectors
• Matrix Calculus
2
Stanford University
Outline
Vectors and matrices are just
• Vectors and matrices
27-Sep-2018
• Matrix rank
• Eigenvalues and Eigenvectors
• Matrix Calculus
3
Stanford University
Vector
27-Sep-2018
denotes the transpose operation
4
Stanford University
Vector
• We’ll default to column vectors in this class
27-Sep-2018
– Points are just vectors image keypoint, etc)
from the origin. – Such vectors don’t have a
– We can make geometric interpretation
calculations like
“distance” between 2 – We can still make calculations like
vectors “distance” between 2 vectors
6
Stanford University
Matrix
27-Sep-2018
• If , we say that is square.
7
Stanford University 7
Images
27-Sep-2018
Python indices
row column start at 0
8
Stanford University 8
Images can be represented as a matrix of pixels.
Images can also be represented as a vector of pixels
• Color images have 3 numbers per pixel – red, green, and blue
brightnesses (RGB) - and are stored as an m×n×3 matrix
27-Sep-2018
= 10
Stanford University
Basic Matrix Operations
• We will discuss:
27-Sep-2018
– Inverse / pseudoinverse
– Determinant / trace
11
Stanford University
Matrix Operations
• Addition
27-Sep-2018
• Scaling
12
Stanford University
Vector Norms
27-Sep-2018
• Homogeneity: For all
• Triangle inequality: For all
14
Stanford University
Vector Operation: inner product
• Inner product (dot product) of two vectors
27-Sep-2018
15
Stanford University
Vector Operation: inner product
27-Sep-2018
16
Stanford University
Matrix Operations
• The product of two matrices
27-Sep-2018
• Each entry in the result is:
(that row of A) dot product with (that column of B)
• Many uses, which will be covered later
18
Stanford University
Matrix Operations
• Multiplication example:
27-Sep-2018
19
Stanford University
Matrix Operations
• The product of two matrices
27-Sep-2018
21
Stanford University
Matrix Operations
• Transpose a matrix: flip the matrix, so row 1
27-Sep-2018
• A useful identity:
22
Stanford University
Matrix Operations
• Determinant
27-Sep-2018
23
Stanford University
Matrix Operations
• Trace
27-Sep-2018
24
Stanford University
Matrix Operations
• Vector Norms (we’ve talked about them earlier)
25
Stanford University
Special Matrices
• Identity matrix I
• Diagonal matrix
Square matrix with numbers along diagonal, 0’s
27-Sep-2018
elsewhere
[diagonal matrix A] · [another matrix B] scales the
rows of matrix B
26
Stanford University
Special Matrices
• Skew-symmetric matrix 2 3
0 2 5
42 0 75
5 7 0
27-Sep-2018
27
Stanford University
Outline
• Vectors and matrices
27-Sep-2018
• Matrix rank
• Eigenvalues and Eigenvectors
• Matrix Calculus
28
Stanford University
Transformation: scaling
27-Sep-2018
(Verify to yourself that the matrix multiplication works out this way)
27-Sep-2018
Multiply by the
scaling matrix:
30
Stanford University
Transformation: rotation
Initial
vector: 𝜃 = 30°
27-Sep-2018
31
Stanford University
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new,
y0 y0
y0 y1 x1
27-Sep-2018
x0 x0
32
It is
Stanford University the same data point, but it is represented in a new, rotated frame.
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new,
y0 y0
y0 y1 x1
y
27-Sep-2018
This is y
This is x coordinate in
coordinate
frame “0”
in frame
“0” x x0 x0
33
It is
Stanford University the same data point, but it is represented in a new, rotated frame.
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new,
y0 y0
y0 y1 x1
y
27-Sep-2018
This is x
This is x x' coordinate
This is y coordinate in frame
coordinate in frame “0” This is y “1”
in frame coordinate in y'
“0” x x0 frame “1” x0
34
It is
Stanford University the same data point, but it is represented in a new, rotated frame.
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new,
27-Sep-2018
This is x
x' coordinate
This is y in frame
coordinate “1”
in frame y'
“0” x x0 x0
This is x coordinate in This is y coordinate in 35
Stanford University frame “0” frame “1”
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new,
y0 y0
y0 y1 x1
y
27-Sep-2018
This is x
x' coordinate
This is y in frame
coordina “1”
te in
y'
frame
“0”
x x0 x0
This is x coordinate in This is y coordinate in 36
Stanford University frame “0” frame “1”
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new, rotated
27-Sep-2018
o y’ (the new y coordinate) is the length of the original vector which lies
in the direction of the new y axis
• So:
o x’ is [original vector] dot [the new x axis]
o y’ is [original vector] dot [the new y axis] 37
Stanford University
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new,
27-Sep-2018
• So:
o x’ = [x, y] dot [the new x axis]
38
o y’University
Stanford = [x, y] dot [the new y axis]
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new, rotated
27-Sep-2018
• So:
o
= [
Stanford University
[the new x axis] dot [x, y]
[the new y axis] dot [x, y] ] 39
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new,
o
[ [the new x axis] dot [x, y]
[the new y axis] dot [x, y] ]
• So:
27-Sep-2018
o =[ [the new x axis]
[the new y axis] ]x à it is a matrix-vector
=[ multiplication!!
o
Stanford University
2x2 Rotation Matrix
]x 40
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new,
27-Sep-2018
o =
Rx P' = R P
41
Stanford University
Before learning how to rotate a vector, let’s
understand how to do something slightly different
• How can you convert a vector represented in frame “0” to a new,
y0 y0
y0 y1 x1
y
27-Sep-2018
x'
y'
x x0
Stanford University
x0 =
Rx 42
Now, here’s how we do a rotation!!
• Until now, we’ve seen how to convert a data point represented in frame “0” to a
y0 y0
= Rx
y1 y1
27-Sep-2018
x1 x1
y
x'
x x0 y' x0
43
Stanford University
Now, here’s how we do a rotation!!
• Until now, we’ve seen how to convert a data point represented in frame “0” to a
y0 y0
= Rx y0
= Rx
y1 y1 y1
27-Sep-2018
x1 x1 x1
y
x'
y’
x x0 y' x0
x’ x0
44
Stanford University
Now, here’s how we do a rotation!!
• Until now, we’ve seen how to convert a data point represented in frame “0” to a
27-Sep-2018
y’
x x0
= Rx x’ x0
• Thus, rotation matrices can be used to rotate vectors. We’ll usually think of 45
Stanford
them University
in that sense-- as operators to rotate vectors
2D Rotation Matrix Formula: what is R?
Counter-clockwise rotation by an angle q
27-Sep-2018
x
ê y 'ú = ê sin q cosq úû êë y úû
ë û ë
Stanford University
P' = R P 46
Transformation Matrices
• Multiple transformation matrices can be used to transform a
27-Sep-2018
• The result is exactly the same if we multiply the matrices
first, to form a single transformation matrix:
p’=(R2 R1 S) p
47
Stanford University
Outline
• Vectors and matrices
27-Sep-2018
• Matrix rank
• Eigenvalues and Eigenvectors
• Matrix Calculus
48
Stanford University
Homogeneous system
27-Sep-2018
– This is sufficient for scale, rotate, skew transformations.
– But notice, we can’t add a constant! L
49
Stanford University
Homogeneous system
27-Sep-2018
– Now we can rotate, scale, and skew like before, AND translate
(note how the multiplication works out, above)
– This is called “homogeneous coordinates”
50
Stanford University
Homogeneous system
– In homogeneous coordinates, the multiplication works out
27-Sep-2018
– Generally, a homogeneous transformation matrix will have a
bottom row of [0 0 1], so that the result has a “1” at the
bottom too.
51
Stanford University
Homogeneous system
• One more thing we might want: to divide the result by
27-Sep-2018
52
Stanford University
2D Translation using Homogeneous Coordinates
P’
27-Sep-2018
53
Stanford University
2D Translation using Homogeneous
Coordinates
x tx é x + t x ù é1 0 t x ù é x ù
27-Sep-2018
P' ® êê y + t y úú = êê0 1 t y úú× êê y úú
êë 1 úû êë0 0 1 úû êë 1 úû
54
Stanford University
2D Translation using Homogeneous
Coordinates
é x + t x ù é1 0 t x ù é x ù
27-Sep-2018
x tx
ê ú ê ú ê ú
P' ® ê y + t y ú = ê0 1 t y ú× ê y ú
êë 1 úû êë0 0 1 úû êë 1 úû
55
Stanford University
2D Translation using Homogeneous
Coordinates
27-Sep-2018
x tx é x + t x ù é1 0 t x ù é x ù
P' ® êê y + t y úú = êê0 1 t y úú× êê y úú
êë 1 úû êë0 0 1 úû êë 1 úû
56
Stanford University
2D Translation using Homogeneous
Coordinates
27-Sep-2018
x tx é x + t x ù é1 0 t x ù é x ù
P' ® êê y + t y úú = êê0 1 t y úú× êê y úú
êë 1 úû êë0 0 1 úû êë 1 úû
57
Stanford University
2D Translation using Homogeneous
Coordinates
P = ( x, y ) ® ( x, y,1)
x tx é x + t x ù é1 0 t x ù é x ù
P' ® êê y + t y úú = êê0 1 t y úú× êê y úú
27-Sep-2018
êë 1 úû êë0 0 1 úû êë 1 úû
éI tù
=ê ú ×P = T×P
Stanford University
ë0 1û 58
Linear Algebra Review 27-Sep-2018
59
P’
Stanford University
Scaling
Scaling Equation
27-Sep-2018
60
Stanford University
Scaling Equation
P’
27-Sep-2018
é sx x ù ésx 0 0ù é x ù
P' ® êê s y y úú = êê 0 sy 0úú× êê y úú
êë 1 úû êë 0 0 1úû êë 1 úû
61
Stanford University
Scaling Equation
sy y
P’
P = ( x, y) ® P' = (s x x, s y y)
é sx x ù ésx 0 0ù é x ù
27-Sep-2018
ê ú ê ú ê ú éS' 0ù
P' ® ê s y y ú = ê 0 sy 0ú× ê y ú = ê ú ×P = S×P
ë 0 1û
êë 1 úû êë 0 0 1úû êë 1 úû
Stanford University S 62
Scaling & Translating
P
P’=S∙P
27-Sep-2018
P’’=T∙P’
P’’=T · P’=T ·(S · P)= T · S ·P
63
Stanford University
Scaling & Translating
27-Sep-2018
$ x x
'$ ' $
x x
' " S t %$ '
= $ 0 sy t y '$ y ' = $ sy y + t y '=$ '$ y '
$ '$ $ ' # 0 1 &$
$# 0 0 1 '&# 1 '& $# 1 '& # 1 '
&
A 64
Stanford University
Scaling & Translating
27-Sep-2018
$ x x
'$ ' $
x x
' " S t %$ '
= $ 0 sy t y '$ y ' = $ sy y + t y '=$ '$ y '
$ '$ $ ' # 0 1 &$
$# 0 0 1 '&# 1 '& $# 1 '& # 1 '
&
65
Stanford University
Translating & Scaling != Scaling & Translating
27-Sep-2018
66
Stanford University
Translating & Scaling != Scaling & Translating
és x 0 0ù é1 0 t x ùé x ù
P ' ' ' = S × T × P = êê 0 sy 0úú êê0 1 t y úúêê y úú =
êë 0 0 1úû êë0 0 1 úûêë 1 úû
27-Sep-2018
és x 0 s x t x ù é x ù és x x + s x t x ù
= êê 0 sy s y t y úú êê y úú = êês y y + s y t y úú
êë 0 0 1 úû êë 1 úû êë 1 úû
67
Stanford University
Translating & Scaling != Scaling & Translating
és x 0 0ù é1 0 t x ùé x ù
P ' ' ' = S × T × P = êê 0 sy 0úú êê0 1 t y úúêê y úú =
êë 0 0 1úû êë0 0 1 úûêë 1 úû
27-Sep-2018
és x 0 s x t x ù é x ù és x x + s x t x ù
= êê 0 sy s y t y úú êê y úú = êês y y + s y t y úú
êë 0 0 1 úû êë 1 úû êë 1 úû
68
Stanford University
Rotation
P’
27-Sep-2018
69
Stanford University
Rotation Equations
27-Sep-2018
x’ x é x'ù écosq - sin q ù é x ù
ê y 'ú = ê sin q cosq úû êë y úû
ë û ë
P' = R P 70
Stanford University
Rotation Matrix Properties
27-Sep-2018
and satisfies many interesting properties:
R × RT = RT × R = I
det( R ) = 1 71
Stanford University
Rotation Matrix Properties
• Transpose of a rotation matrix produces a
R × RT = RT × R = I
det( R ) = 1
• The rows of a rotation matrix are always
27-Sep-2018
mutually perpendicular (a.k.a. orthogonal)
unit vectors
– (and so are its columns)
72
Stanford University
Rotation Equation
x, y, 1
27-Sep-2018
cos𝜃. x − sin𝜃. y, cos 𝜃. y + sin 𝜃. x, 1
73
Stanford University
Rotation Equation
𝐏 = x, y → x, y, 1
𝐏 + = cos𝜃. x − sin𝜃. y, cos 𝜃. y + sin 𝜃. x
→ cos𝜃. x − sin𝜃. y, cos 𝜃. y + sin 𝜃. x, 1
27-Sep-2018
cos 𝜃. x − sin 𝜃. y cos 𝜃 −sin θ 0 x
𝐏 + → cos 𝜃. y + sin 𝜃. x = sin θ cos θ 0 . y
1 0 0 1 1
= 𝐑′ 𝟎 ? 𝐏 = 𝐑 ? 𝐏 74
Stanford University 𝟎 𝟏
Scaling + Rotation + Translation
P’= (T R S) P
écos θ - sin θ t x ù és x 0 0ù é x ù
= êê sin θ cos θ t y úú êê 0 sy 0úú êê y úú =
This is the form of the
27-Sep-2018
êë 0 0 1 úû êë 0 0 1úû êë 1 úû general-purpose
transformation matrix
é xù é xù
éR t ù éS 0ù ê ú é R S tùê ú
=ê y = y
ë0 1úû êë 0 1úû ê ú êë 0 1úû ê ú
êë 1 úû êë 1 úû
75
Stanford University
Outline
• Vectors and matrices
27-Sep-2018
• Matrix rank
• Eigenvalues and Eigenvectors
• Matrix Calculate
76
Stanford University
Inverse
• Given a matrix A, its inverse A-1 is a matrix such
• E.g.
27-Sep-2018
• Useful identities, for matrices that are invertible:
77
Stanford University
Matrix Operations
• Pseudoinverse
27-Sep-2018
– Python will return the value of X which solves the equation
• If there is no exact solution, it will return the closest one
• If there are many solutions, it will return the smallest one
78
Stanford University
Matrix Operations
• Python example:
27-Sep-2018
>> x = np.linalg.solve(A,B)
x =
1.0000
-0.5000
79
Stanford University
Outline
• Vectors and matrices
27-Sep-2018
The rank of a transformation matrix
• Matrix rank tells you how many dimensions it
transforms a vector to.
• Eigenvalues and Eigenvectors
• Matrix Calculate
80
Stanford University
Linear independence
• Suppose we have a set of vectors v1, …, vn
27-Sep-2018
81
Stanford University
Linear independence
• Suppose we have a set of vectors v1, …, vn
27-Sep-2018
the set is linearly independent.
– Common case: a set of vectors v1, …, vn is always linearly
independent if each vector is perpendicular to every other
vector (and non-zero)
82
Stanford University
Linear independence
Linearly independent set Not linearly independent
• Matrix rank
27-Sep-2018
84
Stanford University
Matrix rank
• For transformation matrices, the rank tells you the
27-Sep-2018
All points get
mapped to
the line y=2x
85
Stanford University
A matrix has “full rank” <-> it is invertible
• If an m x m matrix is rank m, we say it’s “full rank”
27-Sep-2018
– Inverse does not exist
• Inverse also doesn’t exist for non-square matrices
86
Stanford University
Outline
• Vectors and matrices
27-Sep-2018
• Matrix rank
• Eigenvalues and Eigenvectors
• Matrix Calculus
87
Stanford University
Eigenvector and Eigenvalue
27-Sep-2018
88
Stanford University
Eigenvector and Eigenvalue
27-Sep-2018
89
Stanford University
Eigenvector and Eigenvalue
27-Sep-2018
• Therefore:
90
Stanford University
Eigenvector and Eigenvalue
27-Sep-2018
91
Stanford University
Properties
• The trace of a A is equal to the sum of its eigenvalues:
27-Sep-2018
• The rank of A is equal to the number of non-zero eigenvalues
of A.
• The eigenvalues of a diagonal matrix D = diag(d1, . . . dn) are
just the diagonal entries d1, . . . dn
92
Stanford University
Spectral theory
27-Sep-2018
93
Stanford University
Spectral theory
27-Sep-2018
94
Stanford University
Spectral theory
27-Sep-2018
95
Stanford University
Spectral theory
27-Sep-2018
96
Stanford University
Diagonalization
• An n × n matrix A is diagonalizable if it has n linearly
27-Sep-2018
Lemma: Eigenvectors associated with distinct eigenvalues are
linearly independent.
97
Stanford University
Diagonalization
27-Sep-2018
98
Stanford University
Diagonalization
27-Sep-2018
• Remember that the inverse of an orthogonal matrix is just its
transpose and the eigenvectors are orthogonal
99
Stanford University
Symmetric matrices
27-Sep-2018
100
Stanford University
Symmetric matrices
– where
• So, what can you say about the vector x that satisfies the
following optimization?
27-Sep-2018
101
Stanford University
Symmetric matrices
– where
• So, what can you say about the vector x that satisfies the
following optimization?
27-Sep-2018
– Is the same as finding the eigenvector that corresponds to the
largest eigenvalue of A.
102
Stanford University
Some applications of Eigenvalues
27-Sep-2018
103
Stanford University
Outline
• Vectors and matrices
27-Sep-2018
• Matrix rank
• Eigenvalues and Eigenvectors
• Matrix Calculus
104
Stanford University
Matrix Calculus – The Gradient
27-Sep-2018
105
Stanford University
Matrix Calculus – The Gradient
27-Sep-2018
106
Stanford University
Exercise
27-Sep-2018
• Find:
107
Stanford University
Exercise
27-Sep-2018
• From this we can conclude that:
108
Stanford University
Matrix Calculus – The Gradient
27-Sep-2018
109
Stanford University
Matrix Calculus – The Hessian
27-Sep-2018
• The Hessian of n-dimensional vector is the n × n matrix.
110
Stanford University
Matrix Calculus – The Hessian
27-Sep-2018
111
Stanford University
Matrix Calculus – The Hessian
27-Sep-2018
• This is known as Schwarz's theorem: The order of partial
derivatives don’t matter as long as the second derivative
exists and is continuous.
112
Stanford University
Matrix Calculus – The Hessian
27-Sep-2018
113
Stanford University
Matrix Calculus – The Hessian
27-Sep-2018
114
Stanford University
Exercise
27-Sep-2018
115
Stanford University
116
27-Sep-2018
• j == k
117
Stanford University
118
27-Sep-2018
• Matrix rank
• Eigenvalues and Eigenvectors
• Matrix Calculate
127
Stanford University