61A Lecture 6 Friday, September 7 Lambda Expressions 2 Lambda - - PowerPoint PPT Presentation

61a lecture 6
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 6 Friday, September 7 Lambda Expressions 2 Lambda - - PowerPoint PPT Presentation

61A Lecture 6 Friday, September 7 Lambda Expressions 2 Lambda Expressions >>> ten = 10 2 Lambda Expressions >>> ten = 10 >>> square = x * x 2 Lambda Expressions An expression: this one >>> ten = 10


slide-1
SLIDE 1

61A Lecture 6

Friday, September 7

slide-2
SLIDE 2

Lambda Expressions

2

slide-3
SLIDE 3

Lambda Expressions

2

>>> ten = 10

slide-4
SLIDE 4

Lambda Expressions

2

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

slide-5
SLIDE 5

Lambda Expressions

2

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

slide-6
SLIDE 6

Lambda Expressions

2

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

slide-7
SLIDE 7

Lambda Expressions

2

>>> 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

slide-8
SLIDE 8

Lambda Expressions

2

>>> 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

slide-9
SLIDE 9

Lambda Expressions

2

>>> 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

slide-10
SLIDE 10

Lambda Expressions

2

>>> 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 and body "return x * x" with formal parameter x A function

slide-11
SLIDE 11

Lambda Expressions

2

>>> 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 and body "return x * x" with formal parameter x A function Notice: no "return"

slide-12
SLIDE 12

Lambda Expressions

2

>>> 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 and body "return x * x" with formal parameter x A function Notice: no "return" Must be a single expression

slide-13
SLIDE 13

Lambda Expressions

2

>>> 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 and body "return x * x" with formal parameter x A function Notice: no "return" Must be a single expression

slide-14
SLIDE 14

Lambda Expressions

2

>>> 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 and body "return x * x" with formal parameter x A function Lambda expressions are rare in Python, but important in general Notice: no "return" Must be a single expression

slide-15
SLIDE 15

Lambda Expressions Versus Def Statements

3

slide-16
SLIDE 16

Lambda Expressions Versus Def Statements

3

VS

slide-17
SLIDE 17

Lambda Expressions Versus Def Statements

3

square = lambda x: x * x

VS

slide-18
SLIDE 18

Lambda Expressions Versus Def Statements

3

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

VS

slide-19
SLIDE 19

Lambda Expressions Versus Def Statements

3

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

VS

  • Both create a function with the same arguments & behavior
slide-20
SLIDE 20

Lambda Expressions Versus Def Statements

3

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

VS

  • Both create a function with the same arguments & behavior
  • Both of those functions are associated with the environment

in which they are defined

slide-21
SLIDE 21

Lambda Expressions Versus Def Statements

3

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

VS

  • Both create a function with the same arguments & behavior
  • Both of those functions are associated with the environment

in which they are defined

  • Both bind that function to the name "square"
slide-22
SLIDE 22

Lambda Expressions Versus Def Statements

3

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

VS

  • Both create a function with the same arguments & behavior
  • Both of those functions are associated with the environment

in which they are defined

  • Both bind that function to the name "square"
  • Only the def statement gives the function an intrinsic name
slide-23
SLIDE 23

Lambda Expressions Versus Def Statements

3

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

VS

  • Both create a function with the same arguments & behavior
  • Both of those functions are associated with the environment

in which they are defined

  • Both bind that function to the name "square"
  • Only the def statement gives the function an intrinsic name
slide-24
SLIDE 24

Lambda Expressions Versus Def Statements

3

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

VS

  • Both create a function with the same arguments & behavior
  • Both of those functions are associated with the environment

in which they are defined

  • Both bind that function to the name "square"
  • Only the def statement gives the function an intrinsic name
slide-25
SLIDE 25

Lambda Expressions Versus Def Statements

3

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

VS

  • Both create a function with the same arguments & behavior
  • Both of those functions are associated with the environment

in which they are defined

  • Both bind that function to the name "square"
  • Only the def statement gives the function an intrinsic name
slide-26
SLIDE 26

Lambda Expressions Versus Def Statements

3

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

VS

  • Both create a function with the same arguments & behavior
  • Both of those functions are associated with the environment

in which they are defined

  • Both bind that function to the name "square"
  • Only the def statement gives the function an intrinsic name

The Greek letter lambda

slide-27
SLIDE 27

Function Currying

4

slide-28
SLIDE 28

Function Currying

4

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

slide-29
SLIDE 29

Function Currying

4

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

slide-30
SLIDE 30

Function Currying

4

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

slide-31
SLIDE 31

Function Currying

4

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.

slide-32
SLIDE 32

Function Currying

4

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. Fun Fact: Currying was discovered by Moses Schönfinkel and later re-discovered by Haskell Curry.

slide-33
SLIDE 33

Function Currying

4

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. Fun Fact: Currying was discovered by Moses Schönfinkel and later re-discovered by Haskell Curry. Schönfinkeling?

slide-34
SLIDE 34

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

slide-35
SLIDE 35

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

f(x) = x2 - 2

slide-36
SLIDE 36

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2

slide-37
SLIDE 37

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2 A "zero"

slide-38
SLIDE 38

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2 A "zero" x=1.414213562373095

slide-39
SLIDE 39

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2 A "zero" Application: a method for (approximately) computing square roots, using only basic arithmetic. x=1.414213562373095

slide-40
SLIDE 40

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2 A "zero" Application: a method for (approximately) computing square roots, using only basic arithmetic. The positive zero of f(x) = x2 - a is x=1.414213562373095

slide-41
SLIDE 41

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

  • 5
  • 2.5

2.5 5

  • 2.5

2.5

f(x) = x2 - 2 A "zero" Application: a method for (approximately) computing square roots, using only basic arithmetic. The positive zero of f(x) = x2 - a is x=1.414213562373095 √

slide-42
SLIDE 42

Newton's Method

6

Begin with a function f and an initial guess x − )

slide-43
SLIDE 43

Newton's Method

6

Begin with a function f and an initial guess x − )

slide-44
SLIDE 44

Newton's Method

1. Compute the value of f at the guess: f(x)

6

Begin with a function f and an initial guess x − )

slide-45
SLIDE 45

Newton's Method

1. Compute the value of f at the guess: f(x)

6

Begin with a function f and an initial guess x (x, f(x)) − )

slide-46
SLIDE 46

Newton's Method

1. Compute the value of f at the guess: f(x) 2. Compute the derivative of f at the guess: f'(x)

6

Begin with a function f and an initial guess x (x, f(x)) − )

slide-47
SLIDE 47

Newton's Method

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

6

Begin with a function f and an initial guess x (x, f(x)) − )

slide-48
SLIDE 48

Newton's Method

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

6

Begin with a function f and an initial guess x (x, f(x))

  • f(x)

− )

slide-49
SLIDE 49

Newton's Method

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

6

Begin with a function f and an initial guess x (x, f(x))

  • f(x)/f'(x)
  • f(x)

− )

slide-50
SLIDE 50

Newton's Method

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

6

Begin with a function f and an initial guess x (x, f(x))

  • f(x)/f'(x)
  • f(x)

− )

slide-51
SLIDE 51

Visualization of Newton's Method

7

(Demo)

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

slide-52
SLIDE 52

Using Newton's Method

8

slide-53
SLIDE 53

Using Newton's Method

8

How to find the square root of 2?

slide-54
SLIDE 54

Using Newton's Method

8

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

slide-55
SLIDE 55

Using Newton's Method

8

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

slide-56
SLIDE 56

Using Newton's Method

8

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

slide-57
SLIDE 57

Using Newton's Method

8

>>> f = lambda x: x*x - 2 >>> find_zero(f) 1.4142135623730951 How to find the square root of 2? How to find the log base 2 of 1024? f(x) = x2 - 2

slide-58
SLIDE 58

Using Newton's Method

8

>>> f = lambda x: x*x - 2 >>> find_zero(f) 1.4142135623730951 How to find the square root of 2? How to find the log base 2 of 1024? >>> g = lambda x: pow(2, x) - 1024 >>> find_zero(g) 10.0 f(x) = x2 - 2

slide-59
SLIDE 59

Using Newton's Method

8

>>> f = lambda x: x*x - 2 >>> find_zero(f) 1.4142135623730951 How to find the square root of 2? How to find the log base 2 of 1024? >>> g = lambda x: pow(2, x) - 1024 >>> find_zero(g) 10.0 f(x) = x2 - 2 g(x) = 2x - 1024

slide-60
SLIDE 60

Using Newton's Method

8

>>> f = lambda x: x*x - 2 >>> find_zero(f) 1.4142135623730951 How to find the square root of 2? How to find the log base 2 of 1024? >>> g = lambda x: pow(2, x) - 1024 >>> find_zero(g) 10.0 f(x) = x2 - 2 g(x) = 2x - 1024

slide-61
SLIDE 61

Using Newton's Method

8

>>> f = lambda x: x*x - 2 >>> find_zero(f) 1.4142135623730951 How to find the square root of 2? How to find the log base 2 of 1024? >>> g = lambda x: pow(2, x) - 1024 >>> find_zero(g) 10.0 What number is one less than its square? f(x) = x2 - 2 g(x) = 2x - 1024

slide-62
SLIDE 62

Using Newton's Method

8

>>> f = lambda x: x*x - 2 >>> find_zero(f) 1.4142135623730951 How to find the square root of 2? How to find the log base 2 of 1024? >>> g = lambda x: pow(2, x) - 1024 >>> find_zero(g) 10.0 What number is one less than its square? >>> h = lambda x: x*x - (x+1) >>> find_zero(h) 1.618033988749895 f(x) = x2 - 2 g(x) = 2x - 1024

slide-63
SLIDE 63

Using Newton's Method

8

>>> f = lambda x: x*x - 2 >>> find_zero(f) 1.4142135623730951 How to find the square root of 2? How to find the log base 2 of 1024? >>> g = lambda x: pow(2, x) - 1024 >>> find_zero(g) 10.0 What number is one less than its square? >>> h = lambda x: x*x - (x+1) >>> find_zero(h) 1.618033988749895 f(x) = x2 - 2 g(x) = 2x - 1024 h(x) = x2 - (x+1)

slide-64
SLIDE 64

Using Newton's Method

8

>>> f = lambda x: x*x - 2 >>> find_zero(f) 1.4142135623730951 How to find the square root of 2? How to find the log base 2 of 1024? >>> g = lambda x: pow(2, x) - 1024 >>> find_zero(g) 10.0 What number is one less than its square? >>> h = lambda x: x*x - (x+1) >>> find_zero(h) 1.618033988749895 f(x) = x2 - 2 g(x) = 2x - 1024 h(x) = x2 - (x+1)

slide-65
SLIDE 65

Special Case: Square Roots

9

slide-66
SLIDE 66

Special Case: Square Roots

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

9

slide-67
SLIDE 67

Special Case: Square Roots

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

9

Update:

slide-68
SLIDE 68

= +

  • Special Case: Square Roots

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

9

Update:

slide-69
SLIDE 69

= +

  • Special Case: Square Roots

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

9

Update: Babylonian Method

slide-70
SLIDE 70

= +

  • Special Case: Square Roots

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

9

Implementation questions: Update: Babylonian Method

slide-71
SLIDE 71

= +

  • Special Case: Square Roots

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

9

What guess should start the computation? Implementation questions: Update: Babylonian Method

slide-72
SLIDE 72

= +

  • Special Case: Square Roots

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

9

What guess should start the computation? How do we know when we are finished? Implementation questions: Update: Babylonian Method

slide-73
SLIDE 73

Special Case: Cube Roots

10

slide-74
SLIDE 74

Special Case: Cube Roots

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

10

slide-75
SLIDE 75

Special Case: Cube Roots

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

10

Update:

slide-76
SLIDE 76

= · +

  • Special Case: Cube Roots

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

10

Update:

slide-77
SLIDE 77

= · +

  • Special Case: Cube Roots

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

10

Implementation questions: Update:

slide-78
SLIDE 78

= · +

  • Special Case: Cube Roots

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

10

What guess should start the computation? Implementation questions: Update:

slide-79
SLIDE 79

= · +

  • Special Case: Cube Roots

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

10

What guess should start the computation? How do we know when we are finished? Implementation questions: Update:

slide-80
SLIDE 80

Iterative Improvement

(Demo)

11

slide-81
SLIDE 81

Iterative Improvement

(Demo)

11

def iter_improve(update, done, guess=1, max_updates=1000): """Iteratively improve guess with update until done returns a true value. guess −− An initial guess update −− A function from guesses to guesses; updates the guess done −− A function from guesses to boolean values; tests if guess is good >>> iter_improve(golden_update, golden_test) 1.618033988749895 """ k = 0 while not done(guess) and k < max_updates: guess = update(guess) k = k + 1 return guess

slide-82
SLIDE 82

Iterative Improvement

(Demo)

11

def golden_update(guess): return 1/guess + 1 −− −− −− def iter_improve(update, done, guess=1, max_updates=1000): """Iteratively improve guess with update until done returns a true value. guess −− An initial guess update −− A function from guesses to guesses; updates the guess done −− A function from guesses to boolean values; tests if guess is good >>> iter_improve(golden_update, golden_test) 1.618033988749895 """ k = 0 while not done(guess) and k < max_updates: guess = update(guess) k = k + 1 return guess

slide-83
SLIDE 83

Iterative Improvement

(Demo)

11

def golden_update(guess): return 1/guess + 1 −− −− −− def golden_test(guess): return guess * guess == guess + 1 −− −− −− def iter_improve(update, done, guess=1, max_updates=1000): """Iteratively improve guess with update until done returns a true value. guess −− An initial guess update −− A function from guesses to guesses; updates the guess done −− A function from guesses to boolean values; tests if guess is good >>> iter_improve(golden_update, golden_test) 1.618033988749895 """ k = 0 while not done(guess) and k < max_updates: guess = update(guess) k = k + 1 return guess

slide-84
SLIDE 84

= lim

  • Derivatives of Single-Argument Functions

12

slide-85
SLIDE 85
  • 2
  • 1

1 2

  • 2
  • 1

1

= lim

  • Derivatives of Single-Argument Functions

12

slide-86
SLIDE 86
  • 2
  • 1

1 2

  • 2
  • 1

1

= lim

  • Derivatives of Single-Argument Functions

12

x = 1

slide-87
SLIDE 87
  • 2
  • 1

1 2

  • 2
  • 1

1

= lim

  • Derivatives of Single-Argument Functions

12

x = 1 x + h = 1.1

slide-88
SLIDE 88
  • 2
  • 1

1 2

  • 2
  • 1

1

= lim

  • Derivatives of Single-Argument Functions

12

x = 1 x + h = 1.1

slide-89
SLIDE 89
  • 2
  • 1

1 2

  • 2
  • 1

1

= lim

  • Derivatives of Single-Argument Functions

12

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

(Demo) x = 1 x + h = 1.1

slide-90
SLIDE 90

Approximating Derivatives

(Demo)

13

slide-91
SLIDE 91

Implementing Newton's Method

14

slide-92
SLIDE 92

Implementing Newton's Method

14

− − − − def newton_update(f): """Return an update function for f using Newton's method.""" def update(x): return x − f(x) / approx_derivative(f, x) return update

slide-93
SLIDE 93

Implementing Newton's Method

14

− − − − def newton_update(f): """Return an update function for f using Newton's method.""" def update(x): return x − f(x) / approx_derivative(f, x) return update

Could be replaced with the exact derivative

slide-94
SLIDE 94

Implementing Newton's Method

14

− − def approx_derivative(f, x, delta=1e−5): """Return an approximation to the derivative of f at x.""" df = f(x + delta) − f(x) return df/delta − − − − − def newton_update(f): """Return an update function for f using Newton's method.""" def update(x): return x − f(x) / approx_derivative(f, x) return update

Could be replaced with the exact derivative

slide-95
SLIDE 95

Implementing Newton's Method

14

− − def approx_derivative(f, x, delta=1e−5): """Return an approximation to the derivative of f at x.""" df = f(x + delta) − f(x) return df/delta − − − − − def newton_update(f): """Return an update function for f using Newton's method.""" def update(x): return x − f(x) / approx_derivative(f, x) return update

Could be replaced with the exact derivative Limit approximated by a small value

slide-96
SLIDE 96

Implementing Newton's Method

14

− − def approx_derivative(f, x, delta=1e−5): """Return an approximation to the derivative of f at x.""" df = f(x + delta) − f(x) return df/delta − − − − − − def find_root(f, guess=1): """Return a guess of a zero of the function f, near guess. >>> from math import sin >>> find_root(lambda y: sin(y), 3) 3.141592653589793 """ return iter_improve(newton_update(f), lambda x: f(x) == 0, guess) − − − − def newton_update(f): """Return an update function for f using Newton's method.""" def update(x): return x − f(x) / approx_derivative(f, x) return update

Could be replaced with the exact derivative Limit approximated by a small value

slide-97
SLIDE 97

Implementing Newton's Method

14

− − def approx_derivative(f, x, delta=1e−5): """Return an approximation to the derivative of f at x.""" df = f(x + delta) − f(x) return df/delta − − − − − − def find_root(f, guess=1): """Return a guess of a zero of the function f, near guess. >>> from math import sin >>> find_root(lambda y: sin(y), 3) 3.141592653589793 """ return iter_improve(newton_update(f), lambda x: f(x) == 0, guess) − − − − def newton_update(f): """Return an update function for f using Newton's method.""" def update(x): return x − f(x) / approx_derivative(f, x) return update

Could be replaced with the exact derivative Limit approximated by a small value Definition of a function zero