61A Lecture 6 Friday, September 13 Announcements 2 Announcements - - PowerPoint PPT Presentation

61a lecture 6
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 6 Friday, September 13 Announcements 2 Announcements - - PowerPoint PPT Presentation

61A Lecture 6 Friday, September 13 Announcements 2 Announcements Homework 2 due Tuesday 9/17 @ 11:59pm 2 Announcements Homework 2 due Tuesday 9/17 @ 11:59pm Project 2 due Thursday 9/19 @ 11:59pm 2 Announcements Homework 2 due


slide-1
SLIDE 1

61A Lecture 6

Friday, September 13

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Homework 2 due Tuesday 9/17 @ 11:59pm

2

slide-4
SLIDE 4

Announcements

  • Homework 2 due Tuesday 9/17 @ 11:59pm
  • Project 2 due Thursday 9/19 @ 11:59pm

2

slide-5
SLIDE 5

Announcements

  • Homework 2 due Tuesday 9/17 @ 11:59pm
  • Project 2 due Thursday 9/19 @ 11:59pm
  • Optional Guerrilla section next Monday for students to master higher-order functions

2

slide-6
SLIDE 6

Announcements

  • Homework 2 due Tuesday 9/17 @ 11:59pm
  • Project 2 due Thursday 9/19 @ 11:59pm
  • Optional Guerrilla section next Monday for students to master higher-order functions
  • Organized by Andrew Huang and the readers

2

slide-7
SLIDE 7

Announcements

  • Homework 2 due Tuesday 9/17 @ 11:59pm
  • Project 2 due Thursday 9/19 @ 11:59pm
  • Optional Guerrilla section next Monday for students to master higher-order functions
  • Organized by Andrew Huang and the readers
  • Work in a group on a problem until everyone in the group understands the solution

2

slide-8
SLIDE 8

Announcements

  • Homework 2 due Tuesday 9/17 @ 11:59pm
  • Project 2 due Thursday 9/19 @ 11:59pm
  • Optional Guerrilla section next Monday for students to master higher-order functions
  • Organized by Andrew Huang and the readers
  • Work in a group on a problem until everyone in the group understands the solution
  • Midterm 1 on Monday 9/23 from 7pm to 9pm

2

slide-9
SLIDE 9

Announcements

  • Homework 2 due Tuesday 9/17 @ 11:59pm
  • Project 2 due Thursday 9/19 @ 11:59pm
  • Optional Guerrilla section next Monday for students to master higher-order functions
  • Organized by Andrew Huang and the readers
  • Work in a group on a problem until everyone in the group understands the solution
  • Midterm 1 on Monday 9/23 from 7pm to 9pm
  • Details and review materials will be posted early next week

2

slide-10
SLIDE 10

Announcements

  • Homework 2 due Tuesday 9/17 @ 11:59pm
  • Project 2 due Thursday 9/19 @ 11:59pm
  • Optional Guerrilla section next Monday for students to master higher-order functions
  • Organized by Andrew Huang and the readers
  • Work in a group on a problem until everyone in the group understands the solution
  • Midterm 1 on Monday 9/23 from 7pm to 9pm
  • Details and review materials will be posted early next week
  • There will be a web form for students who cannot attend due to a conflict

2

slide-11
SLIDE 11

Lambda Expressions

(Demo)

slide-12
SLIDE 12

Lambda Expressions

4

slide-13
SLIDE 13

Lambda Expressions

>>> ten = 10

4

slide-14
SLIDE 14

Lambda Expressions

>>> ten = 10 >>> square = x * x

4

slide-15
SLIDE 15

Lambda Expressions

>>> ten = 10 >>> square = x * x An expression: this one evaluates to a number

4

slide-16
SLIDE 16

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number

4

slide-17
SLIDE 17

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function

4

slide-18
SLIDE 18

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function A function

4

slide-19
SLIDE 19

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function with formal parameter x A function

4

slide-20
SLIDE 20

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function

4

slide-21
SLIDE 21

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Important: No "return" keyword!

4

slide-22
SLIDE 22

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Important: No "return" keyword! Must be a single expression

4

slide-23
SLIDE 23

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x >>> square(4) 16 An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Important: No "return" keyword! Must be a single expression

4

slide-24
SLIDE 24

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x >>> square(4) 16 An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Lambda expressions are not common in Python, but important in general Important: No "return" keyword! Must be a single expression

4

slide-25
SLIDE 25

Lambda Expressions

>>> ten = 10 >>> square = x * x >>> square = lambda x: x * x >>> square(4) 16 An expression: this one evaluates to a number Also an expression: evaluates to a function that returns the value of "x * x" with formal parameter x A function Lambda expressions are not common in Python, but important in general Important: No "return" keyword! Must be a single expression

4

Lambda expressions in Python cannot contain statements at all!

slide-26
SLIDE 26

Lambda Expressions Versus Def Statements

5

Example: http://goo.gl/XH54uE

slide-27
SLIDE 27

Lambda Expressions Versus Def Statements

VS

5

Example: http://goo.gl/XH54uE

slide-28
SLIDE 28

Lambda Expressions Versus Def Statements

square = lambda x: x * x

VS

5

Example: http://goo.gl/XH54uE

slide-29
SLIDE 29

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

5

Example: http://goo.gl/XH54uE

slide-30
SLIDE 30

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.

5

Example: http://goo.gl/XH54uE

slide-31
SLIDE 31

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both functions have as their parent the environment in which they were defined.

5

Example: http://goo.gl/XH54uE

slide-32
SLIDE 32

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both functions have as their parent the environment in which they were defined.
  • Both bind that function to the name square.

5

Example: http://goo.gl/XH54uE

slide-33
SLIDE 33

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both functions have as their parent the environment in which they were defined.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name.

5

Example: http://goo.gl/XH54uE

slide-34
SLIDE 34

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both functions have as their parent the environment in which they were defined.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name.

5

Example: http://goo.gl/XH54uE

slide-35
SLIDE 35

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both functions have as their parent the environment in which they were defined.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name.

5

Example: http://goo.gl/XH54uE

slide-36
SLIDE 36

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both functions have as their parent the environment in which they were defined.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name.

The Greek letter lambda

5

Example: http://goo.gl/XH54uE

slide-37
SLIDE 37

Lambda Expressions Versus Def Statements

square = lambda x: x * x def square(x): return x * x

VS

  • Both create a function with the same domain, range, and behavior.
  • Both functions have as their parent the environment in which they were defined.
  • Both bind that function to the name square.
  • Only the def statement gives the function an intrinsic name.

The Greek letter lambda

5

Example: http://goo.gl/XH54uE

slide-38
SLIDE 38

Currying

slide-39
SLIDE 39

Function Currying

7

slide-40
SLIDE 40

Function Currying

def make_adder(n): return lambda k: n + k

7

slide-41
SLIDE 41

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5

7

slide-42
SLIDE 42

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 There's a general relationship between these functions

7

slide-43
SLIDE 43

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 There's a general relationship between these functions

7

(Demo)

slide-44
SLIDE 44

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 There's a general relationship between these functions Currying: Transforming a multi-argument function into a single-argument, higher-order function.

7

(Demo)

slide-45
SLIDE 45

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 There's a general relationship between these functions Currying: Transforming a multi-argument function into a single-argument, higher-order function. Currying was discovered by Moses Schönfinkel and re-discovered by Haskell Curry.

7

(Demo)

slide-46
SLIDE 46

Function Currying

def make_adder(n): return lambda k: n + k >>> make_adder(2)(3) 5 >>> add(2, 3) 5 There's a general relationship between these functions Currying: Transforming a multi-argument function into a single-argument, higher-order function. Currying was discovered by Moses Schönfinkel and re-discovered by Haskell Curry. Schönfinkeling?

7

(Demo)

slide-47
SLIDE 47

Newton's Method

slide-48
SLIDE 48

Newton's Method Background

Quickly finds accurate approximations to zeroes of differentiable functions!

9

slide-49
SLIDE 49

Newton's Method Background

Quickly finds accurate approximations to zeroes of differentiable functions! f(x) = x2 - 2

9

slide-50
SLIDE 50

Newton's Method Background

Quickly finds accurate approximations to zeroes of differentiable functions!

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2

9

slide-51
SLIDE 51

Newton's Method Background

Quickly finds accurate approximations to zeroes of differentiable functions!

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2 A "zero" of a function f is an input x such that f(x)=0

9

slide-52
SLIDE 52

Newton's Method Background

Quickly finds accurate approximations to zeroes of differentiable functions!

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2 A "zero" of a function f is an input x such that f(x)=0 x=1.414213562373095

9

slide-53
SLIDE 53

Newton's Method Background

Quickly finds accurate approximations to zeroes of differentiable functions!

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2 A "zero" of a function f is an input x such that f(x)=0 Application: a method for computing square roots, cube roots, etc. x=1.414213562373095

9

slide-54
SLIDE 54

Newton's Method Background

Quickly finds accurate approximations to zeroes of differentiable functions!

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2 A "zero" of a function f is an input x such that f(x)=0 Application: a method for computing square roots, cube roots, etc. The positive zero of f(x) = x2 - a is . (We're solving the equation x2 = a.) x=1.414213562373095 √

  • 9
slide-55
SLIDE 55

Newton's Method

Given a function f and initial guess x,

10

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

slide-56
SLIDE 56

Newton's Method

Given a function f and initial guess x,

10

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-57
SLIDE 57

Newton's Method

Given a function f and initial guess x,

10

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-58
SLIDE 58
  • 1. Compute the value of f

at the guess: f(x)

Newton's Method

Given a function f and initial guess x,

10

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-59
SLIDE 59
  • 1. Compute the value of f

at the guess: f(x)

  • 2. Compute the derivative
  • f f at the guess: f'(x)

Newton's Method

Given a function f and initial guess x,

10

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-60
SLIDE 60
  • 1. Compute the value of f

at the guess: f(x)

  • 2. Compute the derivative
  • f f at the guess: f'(x)
  • 3. Update guess x to be:

Newton's Method

Given a function f and initial guess x,

− )

  • 10

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-61
SLIDE 61
  • 1. Compute the value of f

at the guess: f(x)

  • 2. Compute the derivative
  • f f at the guess: f'(x)
  • 3. Update guess x to be:

Newton's Method

Given a function f and initial guess x, Current point: (x, f(x))

− )

  • 10

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-62
SLIDE 62
  • 1. Compute the value of f

at the guess: f(x)

  • 2. Compute the derivative
  • f f at the guess: f'(x)
  • 3. Update guess x to be:

Newton's Method

Given a function f and initial guess x, Current point: (x, f(x)) Length from 0:

  • f(x)

− )

  • 10

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-63
SLIDE 63
  • 1. Compute the value of f

at the guess: f(x)

  • 2. Compute the derivative
  • f f at the guess: f'(x)
  • 3. Update guess x to be:

Newton's Method

Given a function f and initial guess x, Current point: (x, f(x)) Length from 0:

  • f(x)

− )

  • 10

Slope of this tangent line is f'(x)

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-64
SLIDE 64
  • 1. Compute the value of f

at the guess: f(x)

  • 2. Compute the derivative
  • f f at the guess: f'(x)
  • 3. Update guess x to be:

Newton's Method

Given a function f and initial guess x, Current point: (x, f(x)) Change to x:

  • f(x)/f'(x)

Length from 0:

  • f(x)

− )

  • 10

Slope of this tangent line is f'(x)

http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-65
SLIDE 65
  • 1. Compute the value of f

at the guess: f(x)

  • 2. Compute the derivative
  • f f at the guess: f'(x)
  • 3. Update guess x to be:

Newton's Method

Given a function f and initial guess x, Current point: (x, f(x)) Change to x:

  • f(x)/f'(x)

Length from 0:

  • f(x)

− )

  • 10

Slope of this tangent line is f'(x) Zero of tangent line:

− )

  • http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-66
SLIDE 66
  • 1. Compute the value of f

at the guess: f(x)

  • 2. Compute the derivative
  • f f at the guess: f'(x)
  • 3. Update guess x to be:

Newton's Method

Given a function f and initial guess x, Current point: (x, f(x)) Change to x:

  • f(x)/f'(x)

Length from 0:

  • f(x)

− )

  • 10

Slope of this tangent line is f'(x) Zero of tangent line:

− )

  • http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x:

slide-67
SLIDE 67
  • 1. Compute the value of f

at the guess: f(x)

  • 2. Compute the derivative
  • f f at the guess: f'(x)
  • 3. Update guess x to be:

Newton's Method

Given a function f and initial guess x, Current point: (x, f(x)) Change to x:

  • f(x)/f'(x)

Length from 0:

  • f(x)

− )

  • 10

Slope of this tangent line is f'(x) Zero of tangent line:

− )

  • http://en.wikipedia.org/wiki/File:NewtonIteration_Ani.gif

Repeatedly improve x: Finish when f(x) = 0 (or close enough)

slide-68
SLIDE 68

Using Newton's Method

11

slide-69
SLIDE 69

Using Newton's Method

How to find the square root of 2?

11

slide-70
SLIDE 70

Using Newton's Method

>>> f = lambda x: x*x - 2 >>> df = lambda x: 2*x >>> find_zero(f, df) 1.4142135623730951 How to find the square root of 2?

11

slide-71
SLIDE 71

Using Newton's Method

>>> f = lambda x: x*x - 2 >>> df = lambda x: 2*x >>> find_zero(f, df) 1.4142135623730951 How to find the square root of 2?

11

slide-72
SLIDE 72

Using Newton's Method

>>> f = lambda x: x*x - 2 >>> df = lambda x: 2*x >>> find_zero(f, df) 1.4142135623730951 How to find the square root of 2? f(x) = x2 - 2 f'(x) = 2x

11

slide-73
SLIDE 73

Using Newton's Method

>>> f = lambda x: x*x - 2 >>> df = lambda x: 2*x >>> find_zero(f, df) 1.4142135623730951 How to find the square root of 2? f(x) = x2 - 2 f'(x) = 2x

11

Applies Newton's method until |f(x)| < 10-15, starting at 1

slide-74
SLIDE 74

Using Newton's Method

>>> f = lambda x: x*x - 2 >>> df = lambda x: 2*x >>> find_zero(f, df) 1.4142135623730951 How to find the square root of 2? How to find the cube root of 729? f(x) = x2 - 2 f'(x) = 2x

11

Applies Newton's method until |f(x)| < 10-15, starting at 1

slide-75
SLIDE 75

Using Newton's Method

>>> f = lambda x: x*x - 2 >>> df = lambda x: 2*x >>> find_zero(f, df) 1.4142135623730951 How to find the square root of 2? How to find the cube root of 729? f(x) = x2 - 2 f'(x) = 2x

11

3

√ V V

Applies Newton's method until |f(x)| < 10-15, starting at 1

slide-76
SLIDE 76

Using Newton's Method

>>> f = lambda x: x*x - 2 >>> df = lambda x: 2*x >>> find_zero(f, df) 1.4142135623730951 How to find the square root of 2? How to find the cube root of 729? >>> g = lambda x: x*x*x - 729 >>> dg = lambda x: 3*x*x >>> find_zero(g, dg) 9.0 f(x) = x2 - 2 f'(x) = 2x

11

3

√ V V

Applies Newton's method until |f(x)| < 10-15, starting at 1

slide-77
SLIDE 77

Using Newton's Method

>>> f = lambda x: x*x - 2 >>> df = lambda x: 2*x >>> find_zero(f, df) 1.4142135623730951 How to find the square root of 2? How to find the cube root of 729? >>> g = lambda x: x*x*x - 729 >>> dg = lambda x: 3*x*x >>> find_zero(g, dg) 9.0 f(x) = x2 - 2 f'(x) = 2x g(x) = x3 - 729 g'(x) = 3x2

11

3

√ V V

Applies Newton's method until |f(x)| < 10-15, starting at 1

slide-78
SLIDE 78

Iterative Improvement

slide-79
SLIDE 79

Special Case: Square Roots

13

slide-80
SLIDE 80

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a

13

slide-81
SLIDE 81

Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a Update:

13

slide-82
SLIDE 82

= +

  • Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a Update:

13

slide-83
SLIDE 83

= +

  • Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a Update: Babylonian Method

13

slide-84
SLIDE 84

= +

  • Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a Implementation questions: Update: Babylonian Method

13

slide-85
SLIDE 85

= +

  • Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a What guess should start the computation? Implementation questions: Update: Babylonian Method

13

slide-86
SLIDE 86

= +

  • Special Case: Square Roots

How to compute square_root(a) Idea: Iteratively refine a guess x about the square root of a What guess should start the computation? How do we know when we are finished? Implementation questions: Update: Babylonian Method

13

slide-87
SLIDE 87

Special Case: Cube Roots

14

slide-88
SLIDE 88

Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a

14

slide-89
SLIDE 89

Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a Update:

14

slide-90
SLIDE 90

= · +

  • Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a Update:

14

slide-91
SLIDE 91

= · +

  • Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a Implementation questions: Update:

14

slide-92
SLIDE 92

= · +

  • Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a What guess should start the computation? Implementation questions: Update:

14

slide-93
SLIDE 93

= · +

  • Special Case: Cube Roots

How to compute cube_root(a) Idea: Iteratively refine a guess x about the cube root of a What guess should start the computation? How do we know when we are finished? Implementation questions: Update:

14

slide-94
SLIDE 94

Implementing Newton's Method

(Demo)