Lecture 5 Math Review I Justin Johnson EECS

  • Slides: 43
Download presentation
Lecture 5: Math Review I Justin Johnson EECS 442 WI 2020: Lecture 5 -

Lecture 5: Math Review I Justin Johnson EECS 442 WI 2020: Lecture 5 - 1 January 23, 2020

Administrative HW 0 due Wednesday 1/29 (1 week from yesterday) HW 1 out yesterday,

Administrative HW 0 due Wednesday 1/29 (1 week from yesterday) HW 1 out yesterday, due Wednesday 2/5 (3 weeks from yesterday) Justin Johnson EECS 442 WI 2020: Lecture 5 - 2 January 23, 2020

Floating Point Arithmetic Justin Johnson EECS 442 WI 2020: Lecture 5 - 3 January

Floating Point Arithmetic Justin Johnson EECS 442 WI 2020: Lecture 5 - 3 January 23, 2020

This Lecture and Next: Math Two goals for the next two classes: • Math

This Lecture and Next: Math Two goals for the next two classes: • Math with computers ≠ Math • Practical math you need to know but may not have been taught Justin Johnson EECS 442 WI 2020: Lecture 5 - 4 January 23, 2020

This Lecture and Next: Goal • Not a “Linear algebra in two lectures” –

This Lecture and Next: Goal • Not a “Linear algebra in two lectures” – that’s impossible. • Some of this you should know! • Aimed at reviving your knowledge and plugging any gaps • Aimed at giving you intuitions Justin Johnson EECS 442 WI 2020: Lecture 5 - 5 January 23, 2020

Adding Numbers Justin Johnson EECS 442 WI 2020: Lecture 5 - 6 January 23,

Adding Numbers Justin Johnson EECS 442 WI 2020: Lecture 5 - 6 January 23, 2020

Free Drinks in Vegas Each game/variable has mean $0. 10, std $2 100 games

Free Drinks in Vegas Each game/variable has mean $0. 10, std $2 100 games is uncertain and fun! Justin Johnson 100 K games is guaranteed profit: 99. 999999% lowest value is $0. 064. $0. 01 for drinks $0. 054 for profits EECS 442 WI 2020: Lecture 5 - 7 January 23, 2020

Let’s make it big Justin Johnson EECS 442 WI 2020: Lecture 5 - 8

Let’s make it big Justin Johnson EECS 442 WI 2020: Lecture 5 - 8 January 23, 2020

Trying it out Hmm. Justin Johnson EECS 442 WI 2020: Lecture 5 - 9

Trying it out Hmm. Justin Johnson EECS 442 WI 2020: Lecture 5 - 9 January 23, 2020

What is a number? 27 26 25 24 23 22 21 20 1 1

What is a number? 27 26 25 24 23 22 21 20 1 1 1 0 0 1 185 128 + 32 + 16 + 8 + 1 = 185 Justin Johnson EECS 442 WI 2020: Lecture 5 - 10 January 23, 2020

Adding two numbers 28 1 27 1 0 0 26 0 1 0 25

Adding two numbers 28 1 27 1 0 0 26 0 1 0 25 1 1 1 24 1 0 0 23 1 1 0 22 0 0 0 21 0 0 1 20 1 185 1 105 0 34 Carry Result Flag “Integers” on a computer are integers modulo 2 k Justin Johnson EECS 442 WI 2020: Lecture 5 - 11 January 23, 2020

Some Gotchas 32 + (3 / 4) x 40 = 32 + (3 x

Some Gotchas 32 + (3 / 4) x 40 = 32 + (3 x 40) / 4 = Underflow 32 + (3 / 4) x 40 = 32 + 0 = 32 32 62 Why? No Underflow 32 + (3 x 40) / 4 = 32 + 120 / 4 = 32 + 30 = 62 Ok – you have to multiply before dividing Justin Johnson EECS 442 WI 2020: Lecture 5 - 12 January 23, 2020

Some Gotchas math 32 + (9 x 40) / 10 = 68 uint 8

Some Gotchas math 32 + (9 x 40) / 10 = 68 uint 8 32 + (9 x 40) / 10 = 42 Should be: 9 x 4=36 Overflow 32 + 9 x 40 / 10 = 32 + 104 / 10 = 32 + 10 = 42 Justin Johnson Why 104? 9 x 40 = 360 % 256 = 104 EECS 442 WI 2020: Lecture 5 - 13 January 23, 2020

What is a number? 27 26 25 24 23 22 21 20 1 1

What is a number? 27 26 25 24 23 22 21 20 1 1 1 0 0 1 185 How can we do fractions? 25 24 23 22 21 20 2 -1 2 -2 1 0 1 1 1 0 0 1 45. 25 45 0. 25 Justin Johnson EECS 442 WI 2020: Lecture 5 - 14 January 23, 2020

Fixed-Point Arithmetic 25 24 23 22 21 20 2 -1 2 -2 1 0

Fixed-Point Arithmetic 25 24 23 22 21 20 2 -1 2 -2 1 0 1 1 1 0 0 1 45. 25 What’s the largest number we can represent? 63. 75 – Why? How precisely can we measure at 63? 0. 25 How precisely can we measure at 0? 0. 25 Fine for many purposes but for science, seems silly Justin Johnson EECS 442 WI 2020: Lecture 5 - 15 January 23, 2020

Floating Point Numbers Sign (S) 1 Exponent (E) 0 1 1 1 0 Fraction

Floating Point Numbers Sign (S) 1 Exponent (E) 0 1 1 1 0 Fraction (F) 0 1 1 7 1 -1 27 -7 = 20 =1 1+1/8 = 1. 125 Bias: allows exponent to be negative (bias = -127 for float 32) Note: fraction = significant = mantissa; exponents of all ones or all zeros are special numbers Justin Johnson EECS 442 WI 2020: Lecture 5 - 16 January 23, 2020

Floating Point Numbers Fraction 0/8 Sign Exponent 1 0111 -1 7 -7=0 *(-bias)* 1/8

Floating Point Numbers Fraction 0/8 Sign Exponent 1 0111 -1 7 -7=0 *(-bias)* 1/8 2/8 6/8 7/8 Justin Johnson 000 001 010 … -20 x 1. 00 = -1 110 111 -20 x 1. 75 = -1. 75 -20 x 1. 125 = -1. 125 -20 x 1. 25 = -1. 25 -20 x 1. 875 = -1. 875 EECS 442 WI 2020: Lecture 5 - 17 January 23, 2020

Floating Point Numbers Fraction 0/8 Sign Exponent 1 1001 -1 9 -7=2 *(-bias)* 1/8

Floating Point Numbers Fraction 0/8 Sign Exponent 1 1001 -1 9 -7=2 *(-bias)* 1/8 2/8 6/8 7/8 Justin Johnson 000 001 010 … -22 x 1. 00 = -4 110 111 -22 x 1. 75 = -7 -22 x 1. 125 = -4. 5 -22 x 1. 25 = -5 -22 x 1. 875 = -7. 5 EECS 442 WI 2020: Lecture 5 - 18 January 23, 2020

Floating Point Numbers Sign Exponent 1 1 Fraction 0111 000 001 -20 x 1.

Floating Point Numbers Sign Exponent 1 1 Fraction 0111 000 001 -20 x 1. 00 = -1 1001 000 001 -22 x 1. 00 = -4 -20 x 1. 125 = -1. 125 -22 x 1. 125 = -4. 5 Gap between numbers is relative, not absolute Justin Johnson EECS 442 WI 2020: Lecture 5 - 19 January 23, 2020

Adding Floating Point Numbers Sign Exponent Fraction 1 1 0110 1001 000 -2 -1

Adding Floating Point Numbers Sign Exponent Fraction 1 1 0110 1001 000 -2 -1 x 1. 00 = -0. 5 1 1001 -22 x 1. 125 = -4. 5 -22 x 1. 00 = -4 Actual implementation is complex Justin Johnson EECS 442 WI 2020: Lecture 5 - 20 January 23, 2020

Adding Floating Point Numbers Sign Exponent Fraction 1 1 0100 1001 000 -2 -3

Adding Floating Point Numbers Sign Exponent Fraction 1 1 0100 1001 000 -2 -3 x 1. 00 = -0. 125 -22 x 1. 00 = -4 -22 x 1. 03125 = -4. 125 1 1001 000 -22 x 1. 00 = -4 1 1001 -22 x 1. 125 = -4. 5 Justin Johnson EECS 442 WI 2020: Lecture 5 - 21 ? January 23, 2020

Adding Floating Point Numbers Sign Exponent Fraction 1 1 0100 1001 000 -2 -3

Adding Floating Point Numbers Sign Exponent Fraction 1 1 0100 1001 000 -2 -3 x 1. 00 = -0. 125 -22 x 1. 00 = -4 -22 x 1. 03125 = -4. 125 1 1001 000 -22 x 1. 00 = -4 For a and b, these can happen a + b = a a+b-a ≠ b Justin Johnson EECS 442 WI 2020: Lecture 5 - 22 January 23, 2020

Real Floating Point Numbers IEEE 754 Single Precision / Single / float 32 8

Real Floating Point Numbers IEEE 754 Single Precision / Single / float 32 8 bits 23 bits 2127 ≈ 1038 ≈ 7 decimal digits S Exponent Fraction IEEE 754 Double Precision / Double / float 64 11 bits 21023 ≈ 10308 52 bits ≈ 15 decimal digits Exponent Fraction S Justin Johnson EECS 442 WI 2020: Lecture 5 - 23 January 23, 2020

Real Floating Point Numbers IEEE 754 Half Precision / Half / float 16 5

Real Floating Point Numbers IEEE 754 Half Precision / Half / float 16 5 bits 10 bits 232 ≈ 109 ≈ 3 decimal digits S Exponent Fraction Brain Floating Point / bfloat 16 8 bits 7 bits 2127 ≈ 1038 ≈ 2 decimal digits S Exponent Fraction Same range as FP 32, but reduced precision Justin Johnson EECS 442 WI 2020: Lecture 5 - 24 January 23, 2020

Trying it out Roundoff error occurs a+b=a -> numerator is stuck, denominator isn’t Justin

Trying it out Roundoff error occurs a+b=a -> numerator is stuck, denominator isn’t Justin Johnson EECS 442 WI 2020: Lecture 5 - 25 January 23, 2020

Things to Remember • Computer numbers aren’t math numbers • Overflow, accidental zeros, roundoff

Things to Remember • Computer numbers aren’t math numbers • Overflow, accidental zeros, roundoff error, and basic equalities are almost certainly incorrect for some values • Floating point defaults and numpy try to protect you. • Generally safe to use a double and use built-infunctions in numpy (not necessarily others!) • Spooky behavior = look for numerical issues Justin Johnson EECS 442 WI 2020: Lecture 5 - 26 January 23, 2020

Vectors Justin Johnson EECS 442 WI 2020: Lecture 5 - 27 January 23, 2020

Vectors Justin Johnson EECS 442 WI 2020: Lecture 5 - 27 January 23, 2020

Vectors [2, 3] = 2 x [1, 0] + 3 x [0, 1] 2

Vectors [2, 3] = 2 x [1, 0] + 3 x [0, 1] 2 x + 3 x 2 x e 1 + 3 x e 2 x = [2, 3] Justin Johnson Can be arbitrary # of dimensions (typically denoted Rn) EECS 442 WI 2020: Lecture 5 - 28 January 23, 2020

Scaling Vectors 2 x = [4, 6] x = [2, 3] Justin Johnson •

Scaling Vectors 2 x = [4, 6] x = [2, 3] Justin Johnson • Can scale vector by a scalar • Scalar = single number • Dimensions changed independently • Changes magnitude / length, does not change direction. EECS 442 WI 2020: Lecture 5 - 29 January 23, 2020

Adding Vectors • • x = [2, 3] Can add vectors Dimensions changed independently

Adding Vectors • • x = [2, 3] Can add vectors Dimensions changed independently Order irrelevant Can change direction and magnitude x+y = [5, 4] y = [3, 1] Justin Johnson EECS 442 WI 2020: Lecture 5 - 30 January 23, 2020

Scaling and Adding 2 x+y = [7, 7] Can do both at the same

Scaling and Adding 2 x+y = [7, 7] Can do both at the same time x = [2, 3] y = [3, 1] Justin Johnson EECS 442 WI 2020: Lecture 5 - 31 January 23, 2020

Measuring Length Magnitude / length / (L 2) norm of vector x = [2,

Measuring Length Magnitude / length / (L 2) norm of vector x = [2, 3] There are other norms; assume L 2 unless told otherwise y = [3, 1] Why? Justin Johnson EECS 442 WI 2020: Lecture 5 - 32 January 23, 2020

Normalizing a Vector x = [2, 3] Diving by norm gives something on the

Normalizing a Vector x = [2, 3] Diving by norm gives something on the unit sphere (all vectors with length 1) y = [3, 1] Justin Johnson EECS 442 WI 2020: Lecture 5 - 33 January 23, 2020

Dot Products Justin Johnson What happens with normalized / unit vectors? EECS 442 WI

Dot Products Justin Johnson What happens with normalized / unit vectors? EECS 442 WI 2020: Lecture 5 - 34 January 23, 2020

Dot Products Justin Johnson EECS 442 WI 2020: Lecture 5 - 35 January 23,

Dot Products Justin Johnson EECS 442 WI 2020: Lecture 5 - 35 January 23, 2020

Dot Products Justin Johnson EECS 442 WI 2020: Lecture 5 - 36 January 23,

Dot Products Justin Johnson EECS 442 WI 2020: Lecture 5 - 36 January 23, 2020

Special Angles Perpendicular / orthogonal vectors have dot product 0 irrespective of their magnitude

Special Angles Perpendicular / orthogonal vectors have dot product 0 irrespective of their magnitude Justin Johnson EECS 442 WI 2020: Lecture 5 - 37 January 23, 2020

Special Angles Perpendicular / orthogonal vectors have dot product 0 irrespective of their magnitude

Special Angles Perpendicular / orthogonal vectors have dot product 0 irrespective of their magnitude Justin Johnson EECS 442 WI 2020: Lecture 5 - 38 January 23, 2020

Orthogonal Vectors • Geometrically, what’s the set of vectors that are orthogonal to x?

Orthogonal Vectors • Geometrically, what’s the set of vectors that are orthogonal to x? • A line [3, -2] Justin Johnson EECS 442 WI 2020: Lecture 5 - 39 January 23, 2020

Orthogonal Vectors Justin Johnson EECS 442 WI 2020: Lecture 5 - 40 January 23,

Orthogonal Vectors Justin Johnson EECS 442 WI 2020: Lecture 5 - 40 January 23, 2020

Cross Product Image credit: Wikipedia. org Justin Johnson EECS 442 WI 2020: Lecture 5

Cross Product Image credit: Wikipedia. org Justin Johnson EECS 442 WI 2020: Lecture 5 - 41 January 23, 2020

Operations You Should Know • Scale (vector, scalar → vector) • Add (vector, vector

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 Justin Johnson EECS 442 WI 2020: Lecture 5 - 42 January 23, 2020

Next Time: Matrices, Vectorization, Linear Algebra Justin Johnson EECS 442 WI 2020: Lecture 5

Next Time: Matrices, Vectorization, Linear Algebra Justin Johnson EECS 442 WI 2020: Lecture 5 - 43 January 23, 2020