Linear Algebra Tutorial I
EECS 442 Fall 2020, University of Michigan
Slide Adapted from David Fouhey 1
Vectors
x = [2,3]
๐๐ = 23
๐๐1 = 2๐๐2 = 3
Just an array!Get in the habit of thinking of
them as columns.
Slide Adapted from David Fouhey 2
Scaling Vectors
x = [2,3]
2x = [4,6] โข Can scale vector by a scalarโข Scalar = single numberโข Dimensions changed
independentlyโข Changes magnitude / length,
does not change direction.
Slide Adapted from David Fouhey 3
Adding Vectors
y = [3,1]
x+y = [5,4]x = [2,3]
โข Can add vectorsโข Dimensions changed independentlyโข Order irrelevantโข Can change direction and magnitude
Slide Adapted from David Fouhey 4
Scaling and Adding
y = [3,1]
2x+y = [7,7]
Can do both at the same timex = [2,3]
Slide Adapted from David Fouhey 5
Measuring Length
y = [3,1]
x = [2,3]
Magnitude / length / (L2) norm of vector
๐๐ = ๐๐ 2 = ๏ฟฝ๐๐
๐๐
๐ฅ๐ฅ๐๐21/2
There are other norms; assume L2 unless told otherwise
๐๐ 2 = 13๐๐ 2 = 10
Why?Slide Adapted from David Fouhey 6
Normalizing a Vector
x = [2,3]
y = [3,1]๐๐โฒ = ๐๐/ ๐๐ ๐๐
๐๐โฒ = ๐๐/ ๐๐ ๐๐
Diving by norm gives something on the unit sphere (all vectors with length 1)
Slide Adapted from David Fouhey 7
Dot Products
๐๐โฒ
๐๐โฒ
๐๐ โ ๐๐ = ๏ฟฝ๐๐=1
๐๐
๐ฅ๐ฅ๐๐๐ฆ๐ฆ๐๐ = ๐๐๐ป๐ป๐๐
๐๐
๐๐ โ ๐๐ = cos ๐๐ ๐๐ ๐๐
What happens with normalized / unit vectors?
Slide Adapted from David Fouhey 8
Dot Products
๐๐๐๐
๐๐๐๐
๐๐ โ ๐๐ = ๏ฟฝ๐๐
๐๐
๐ฅ๐ฅ๐๐๐ฆ๐ฆ๐๐๐๐ = [2,3]
Whatโs ๐๐ โ ๐๐๐๐, ๐๐ โ ๐๐๐๐?Ans: ๐๐ โ ๐๐๐๐ = 2 ; ๐๐ โ ๐๐๐๐ = 3โข Dot product is projectionโข Amount of x thatโs also
pointing in direction of y
Slide Adapted from David Fouhey 9
Dot Products
Whatโs ๐๐ โ ๐๐ ?Ans: ๐๐ โ ๐๐ = โ๐ฅ๐ฅ๐๐๐ฅ๐ฅ๐๐ = ๐๐ 2
2
๐๐ โ ๐๐ = ๏ฟฝ๐๐
๐๐
๐ฅ๐ฅ๐๐๐ฆ๐ฆ๐๐๐๐ = [2,3]
Slide Adapted from David Fouhey 10
Special Angles
๐๐โฒ
๐๐โฒ
๐๐
10 โ 0
1 = 0 โ 1 + 1 โ 0 = 0
Perpendicular / orthogonal vectors have dot product 0 irrespective of their magnitude
๐๐
๐๐Slide Adapted from David Fouhey 11
Special Angles๐ฅ๐ฅ1๐ฅ๐ฅ2 โ
๐ฆ๐ฆ1๐ฆ๐ฆ2 = ๐ฅ๐ฅ1๐ฆ๐ฆ1 + ๐ฅ๐ฅ2๐ฆ๐ฆ2 = 0
Perpendicular / orthogonal vectors have dot product 0 irrespective of their magnitude
๐๐โฒ
๐๐โฒ
๐๐
๐๐
๐๐Slide Adapted from David Fouhey 12
Orthogonal Vectors
๐๐ = [2,3]โข Geometrically,
whatโs the set of vectors that are orthogonal to x?
โข A line [3,-2]
Slide Adapted from David Fouhey 13
Orthogonal Vectorsโข Whatโs the set of vectors that are
orthogonal to x = [5,0,0]?โข A plane/2D space of vectors/any
vector [0,๐๐, ๐๐]
โข Whatโs the set of vectors that are orthogonal to x and y = [0,5,0]?
โข A line/1D space of vectors/any vector [0,0, ๐๐]
โข Ambiguity in sign and magnitude
๐๐
๐๐
๐๐Slide Adapted from David Fouhey 14
Cross Productโข Cross product ๐๐ ร ๐๐ is: (1)
orthogonal to x, y (2) has sign given by right hand rule and (3) has magnitude given by area of parallelogram of x and y
โข Important: if x and y are the same direction or either is 0, then ๐๐ ร๐๐ = ๐๐ .
โข Only in 3D!
๐๐๐๐
๐๐ ร ๐๐
Image credit: Wikipedia.org Slide Adapted from David Fouhey 15
Operations You Should Know
โข Scale (vector, scalar โ vector)โข Add (vector, vector โ vector)โข Magnitude (vector โ scalar)โข Dot product (vector, vector โ scalar)
โข Dot products are projection / angles โข Cross product (vector, vector โ vector)
โข Vectors facing same direction have cross product 0โข You can never mix vectors of different sizes
Slide Adapted from David Fouhey 16
MatricesHorizontally concatenate n, m-dim column vectors
and you get a mxn matrix A (here 2x3)
๐จ๐จ = ๐๐1,โฏ ,๐๐๐๐ =๐ฃ๐ฃ11 ๐ฃ๐ฃ21 ๐ฃ๐ฃ31๐ฃ๐ฃ12 ๐ฃ๐ฃ22 ๐ฃ๐ฃ32
Slide Adapted from David Fouhey 17
Matrices
Vertically concatenate m, n-dim row vectors and you get a mxn matrix A (here 2x3)
๐ด๐ด =๐๐1๐๐โฎ๐๐๐๐๐๐
=๐ข๐ข11 ๐ข๐ข12 ๐ข๐ข13๐ข๐ข21 ๐ข๐ข22 ๐ข๐ข23
Transpose: flip rows / columns
๐๐๐๐๐๐
๐๐
= ๐๐ ๐๐ ๐๐ (3x1)T = 1x3
Slide Adapted from David Fouhey 18
Matrix-Vector Product๐๐2๐ฅ๐ฅ1 = ๐จ๐จ2๐ฅ๐ฅ3๐๐3๐ฅ๐ฅ1
๐๐ = ๐ฅ๐ฅ1๐๐๐๐ + ๐ฅ๐ฅ2๐๐๐๐ + ๐ฅ๐ฅ3๐๐๐๐Linear combination of columns of A
๐ฆ๐ฆ1๐ฆ๐ฆ2 = ๐๐๐๐ ๐๐๐๐ ๐๐๐๐
๐ฅ๐ฅ1๐ฅ๐ฅ2๐ฅ๐ฅ3
Slide Adapted from David Fouhey 19
Matrix-Vector Product๐๐2๐ฅ๐ฅ1 = ๐จ๐จ2๐ฅ๐ฅ3๐๐3๐ฅ๐ฅ1
๐ฆ๐ฆ1 = ๐๐๐๐๐ป๐ป๐๐
Dot product between rows of A and x
๐ฆ๐ฆ2 = ๐๐๐๐๐ป๐ป๐๐
๐๐๐๐๐ป๐ป
๐๐๐๐๐ป๐ป๐ฆ๐ฆ1๐ฆ๐ฆ2 = ๐๐
3
3
Slide Adapted from David Fouhey 20
Matrix Multiplication
โ ๐๐๐๐๐ป๐ป โโฎ
โ ๐๐๐๐๐ป๐ป โ
| |๐๐๐๐ โฏ ๐๐๐๐| |
๐จ๐จ๐จ๐จ =
Generally: Amn and Bnp yield product (AB)mp
Yes โ in A, Iโm referring to the rows, and in B, Iโm referring to the columns
Slide Adapted from David Fouhey 21
Matrix Multiplication
โ ๐๐๐๐๐ป๐ป โโฎ
โ ๐๐๐๐๐ป๐ป โ
| |๐๐๐๐ โฏ ๐๐๐๐| |
๐จ๐จ๐จ๐จ =๐๐๐๐๐ป๐ป๐๐๐๐ โฏ ๐๐๐๐๐ป๐ป๐๐๐๐โฎ โฑ โฎ
๐๐๐๐๐ป๐ป ๐๐๐๐ โฏ ๐๐๐๐๐ป๐ป ๐๐๐๐
๐จ๐จ๐จ๐จ๐๐๐๐ = ๐๐๐๐๐ป๐ป๐๐๐๐
Generally: Amn and Bnp yield product (AB)mp
Slide Adapted from David Fouhey 22
Matrix Multiplication
โข Dimensions must matchโข Dimensions must matchโข Dimensions must matchโข (Yes, itโs associative): ABx = (A)(Bx) = (AB)xโข (No itโs not commutative): ABx โ (BA)x โ (BxA)
Slide Adapted from David Fouhey 23
Cross-correlationConsider 1D case for simplicityโข Correlation c ๐๐ = โ โ g = โ๐๐ โ ๐๐ + ๐๐ ๐๐[๐๐]โข Convolution ๐๐ ๐๐ = โ โ g = โ๐๐ โ ๐๐ โ ๐๐ ๐๐[๐๐]Let โ= [3,1,2,5,4],๐๐ = [1,2,3], then c = [11, 20, 24]:
c 0 = โ๐๐ โ 0 + ๐๐ ๐๐[๐๐] =โ 0 ๐๐ 0 + โ 1 ๐๐ 1 + โ 2 ๐๐ 2 = 312๏ฟฝ
123
Slide Adapted from David Fouhey 24
3 1 2 5 4
1 2 3
1 2 3
1 2 3
Each output element is from a dot product!
c 0 = 312๏ฟฝ
123
= 3 + 2 + 6 = 11
c 1 = 125๏ฟฝ
123
=1 + 4 + 15 = 20
c 2 = 254๏ฟฝ
123
=2 + 10 + 12 = 24
ConvolutionConsider 1D case for simplicityโข Correlation c ๐๐ = โ โ g = โ๐๐ โ ๐๐ + ๐๐ ๐๐[๐๐]โข Convolution ๐๐ ๐๐ = โ โ g = โ๐๐ โ ๐๐ โ ๐๐ ๐๐[๐๐]Let โ= [3,1,2,5,4],๐๐ = [1,2,3], then f = [3, 7, 13, 12, 20, 23, 12]:
f 0 = โ๐๐ โ 0 โ ๐๐ ๐๐[๐๐] =โ 0 ๐๐ 0 + โ โ1 ๐๐ 1 + โ โ2 ๐๐ 2 = 003๏ฟฝ
321
Slide Adapted from David Fouhey 25
0 0 3 1 2 5 4 0 0
3 2 1
3 2 1
3 2 1
Each output element is from a dot product!
37
13
12..
3 2 1
Operations They Donโt Teach
๐๐ + ๐๐ ๐๐ + ๐๐๐๐ + ๐๐ ๐๐ + ๐๐
๐๐ ๐๐๐๐ ๐๐ + ๐๐ ๐๐
๐๐ โ = ๐๐ + ๐๐ ๐๐ + ๐๐๐๐ + ๐๐ ๐๐ + โ
You Probably Saw Matrix Addition
๐๐ ๐๐๐๐ ๐๐ + ๐๐ =
What is this? FYI: e is a scalar
Slide Adapted from David Fouhey 26
Broadcasting
๐๐ ๐๐๐๐ ๐๐ + ๐๐
= ๐๐ ๐๐๐๐ ๐๐ + ๐๐ ๐๐
๐๐ ๐๐
= ๐๐ ๐๐๐๐ ๐๐ + ๐๐2๐ฅ๐ฅ2๐๐
If you want to be pedantic and proper, you expand e by multiplying a matrix of 1s (denoted 1)
Many smart matrix libraries do this automatically. This is the source of many bugs.
Slide Adapted from David Fouhey 27
Broadcasting Example
๐ท๐ท =๐ฅ๐ฅ1 ๐ฆ๐ฆ1โฎ โฎ๐ฅ๐ฅ๐๐ ๐ฆ๐ฆ๐๐
๐๐ = ๐๐๐๐
Given: a nx2 matrix P and a 2D column vector v, Want: nx2 difference matrix D
๐ซ๐ซ =๐ฅ๐ฅ1 โ ๐๐ ๐ฆ๐ฆ1 โ ๐๐โฎ โฎ
๐ฅ๐ฅ๐๐ โ ๐๐ ๐ฆ๐ฆ๐๐ โ ๐๐
๐ท๐ท โ ๐๐๐๐ =๐ฅ๐ฅ1 ๐ฆ๐ฆ1โฎ โฎ๐ฅ๐ฅ๐๐ ๐ฆ๐ฆ๐๐
โ๐๐ ๐๐
๐๐ ๐๐โฎ
Blue stuff is assumed / broadcast
Slide Adapted from David Fouhey 28
Broadcasting RuleWhen operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions and works its way forward. Two dimensions are compatible when
1. they are equal, or2. one of them is 1
Slide Adapted from David Fouhey 29
Two Uses for Matrices
1. Storing things in a rectangular array (images, maps)โข Typical operations: element-wise operations,
convolution (which weโll cover next)โข Atypical operations: almost anything you learned in
a math linear algebra class2. A linear operator that maps vectors to
another space (Ax)โข Typical/Atypical: reverse of above
Slide Adapted from David Fouhey 30