+ All Categories
Home > Documents > Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of...

Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of...

Date post: 01-Nov-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
30
Linear Algebra Tutorial I EECS 442 Fall 2020, University of Michigan Slide Adapted from David Fouhey 1
Transcript
Page 1: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

Linear Algebra Tutorial I

EECS 442 Fall 2020, University of Michigan

Slide Adapted from David Fouhey 1

Page 2: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 3: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 4: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 5: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 6: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 7: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 8: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

Dot Products

๐’™๐’™โ€ฒ

๐’š๐’šโ€ฒ

๐’™๐’™ โ‹… ๐’š๐’š = ๏ฟฝ๐‘–๐‘–=1

๐‘›๐‘›

๐‘ฅ๐‘ฅ๐‘–๐‘–๐‘ฆ๐‘ฆ๐‘–๐‘– = ๐’™๐’™๐‘ป๐‘ป๐’š๐’š

๐œƒ๐œƒ

๐’™๐’™ โ‹… ๐’š๐’š = cos ๐œƒ๐œƒ ๐’™๐’™ ๐’š๐’š

What happens with normalized / unit vectors?

Slide Adapted from David Fouhey 8

Page 9: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 10: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

Dot Products

Whatโ€™s ๐’™๐’™ โ‹… ๐’™๐’™ ?Ans: ๐’™๐’™ โ‹… ๐’™๐’™ = โˆ‘๐‘ฅ๐‘ฅ๐‘–๐‘–๐‘ฅ๐‘ฅ๐‘–๐‘– = ๐’™๐’™ 2

2

๐’™๐’™ โ‹… ๐’š๐’š = ๏ฟฝ๐‘–๐‘–

๐‘›๐‘›

๐‘ฅ๐‘ฅ๐‘–๐‘–๐‘ฆ๐‘ฆ๐‘–๐‘–๐’™๐’™ = [2,3]

Slide Adapted from David Fouhey 10

Page 11: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 12: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 13: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 14: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 15: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 16: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 17: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 18: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 19: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 20: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 21: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 22: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

Matrix Multiplication

โˆ’ ๐’‚๐’‚๐Ÿ๐Ÿ๐‘ป๐‘ป โˆ’โ‹ฎ

โˆ’ ๐’‚๐’‚๐’Ž๐’Ž๐‘ป๐‘ป โˆ’

| |๐’ƒ๐’ƒ๐Ÿ๐Ÿ โ‹ฏ ๐’ƒ๐’ƒ๐’‘๐’‘| |

๐‘จ๐‘จ๐‘จ๐‘จ =๐’‚๐’‚๐Ÿ๐Ÿ๐‘ป๐‘ป๐’ƒ๐’ƒ๐Ÿ๐Ÿ โ‹ฏ ๐’‚๐’‚๐Ÿ๐Ÿ๐‘ป๐‘ป๐’ƒ๐’ƒ๐’‘๐’‘โ‹ฎ โ‹ฑ โ‹ฎ

๐’‚๐’‚๐’Ž๐’Ž๐‘ป๐‘ป ๐’ƒ๐’ƒ๐Ÿ๐Ÿ โ‹ฏ ๐’‚๐’‚๐’Ž๐’Ž๐‘ป๐‘ป ๐’ƒ๐’ƒ๐’‘๐’‘

๐‘จ๐‘จ๐‘จ๐‘จ๐‘–๐‘–๐‘–๐‘– = ๐’‚๐’‚๐’Š๐’Š๐‘ป๐‘ป๐’ƒ๐’ƒ๐’‹๐’‹

Generally: Amn and Bnp yield product (AB)mp

Slide Adapted from David Fouhey 22

Page 23: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 24: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 25: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 26: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

Operations They Donโ€™t Teach

๐‘Ž๐‘Ž + ๐‘’๐‘’ ๐‘๐‘ + ๐‘’๐‘’๐‘๐‘ + ๐‘’๐‘’ ๐‘‘๐‘‘ + ๐‘’๐‘’

๐‘Ž๐‘Ž ๐‘๐‘๐‘๐‘ ๐‘‘๐‘‘ + ๐‘’๐‘’ ๐‘“๐‘“

๐‘”๐‘” โ„Ž = ๐‘Ž๐‘Ž + ๐‘’๐‘’ ๐‘๐‘ + ๐‘“๐‘“๐‘๐‘ + ๐‘”๐‘” ๐‘‘๐‘‘ + โ„Ž

You Probably Saw Matrix Addition

๐‘Ž๐‘Ž ๐‘๐‘๐‘๐‘ ๐‘‘๐‘‘ + ๐‘’๐‘’ =

What is this? FYI: e is a scalar

Slide Adapted from David Fouhey 26

Page 27: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 28: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 29: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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

Page 30: Linear Algebra Tutorial IMeasuring Length y = [3,1] x = [2,3] Magnitude / length / (L2) norm of vector ๐’™๐’™= ๐’™๐’™ 2 = ๐‘–๐‘– ๐‘›๐‘› ๐‘ฅ๐‘ฅ ๐‘–๐‘– 2 1/2 There are other

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


Recommended