61a lecture 6cs61a/fa12/slides/06...lambda expressions versus def statements 3 square = lambda x: x...

97
61A Lecture 6 Friday, September 7

Upload: others

Post on 12-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

61A Lecture 6

Friday, September 7

Page 2: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions

2

Page 3: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions

2

>>> ten = 10

Page 4: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions

2

>>> ten = 10

>>> square = x * x

Page 5: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions

2

>>> ten = 10

>>> square = x * x

An expression: this one evaluates to a number

Page 6: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions

2

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

An expression: this one evaluates to a number

Page 7: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 8: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 9: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 10: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 11: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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 functionNotice: no "return"

Page 12: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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 functionNotice: no "return"

Must be a single expression

Page 13: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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 functionNotice: no "return"

Must be a single expression

Page 14: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 15: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

Page 16: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

VS

Page 17: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

square = lambda x: x * x VS

Page 18: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

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

Page 19: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

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

• Both create a function with the same arguments & behavior

Page 20: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

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

• Both create a function with the same arguments & behavior

• Both of those functions are associated with the environment in which they are defined

Page 21: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

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

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

Page 22: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

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

• 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

Page 23: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

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

• 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

Page 24: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

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

• 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

Page 25: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

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

• 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

Page 26: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Lambda Expressions Versus Def Statements

3

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

• 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

Page 27: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Function Currying

4

Page 28: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Function Currying

4

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

Page 29: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Function Currying

4

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

>>> make_adder(2)(3)5>>> add(2, 3)5

Page 30: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 31: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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.

Page 32: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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.

Page 33: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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?

Page 34: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

Page 35: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

f(x) = x2 - 2

Page 36: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 2.5 5

-2.5

2.5

f(x) = x2 - 2

Page 37: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 2.5 5

-2.5

2.5

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

Page 38: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 2.5 5

-2.5

2.5

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

x=1.414213562373095

Page 39: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 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

Page 40: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 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

Page 41: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 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

��

Page 42: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method

6

Begin with a function f and

an initial guess x

�� ���)�����

Page 43: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Newton's Method

6

Begin with a function f and

an initial guess x

�� ���)�����

Page 44: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

�� ���)�����

Page 45: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

�� ���)�����

Page 46: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

�� ���)�����

Page 47: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

�� ���)�����

Page 48: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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)

�� ���)�����

Page 49: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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)

�� ���)�����

Page 50: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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)

�� ���)�����

Page 51: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Visualization of Newton's Method

7

(Demo)

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

Page 52: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Using Newton's Method

8

Page 53: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Using Newton's Method

8

How to find the square root of 2?

Page 54: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Using Newton's Method

8

>>> f = lambda x: x*x - 2>>> find_zero(f)1.4142135623730951

How to find the square root of 2?

Page 55: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 56: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 57: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 58: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 59: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 60: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 61: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 62: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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

Page 63: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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)

Page 64: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

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)

Page 65: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Special Case: Square Roots

9

Page 66: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Special Case: Square Roots

How to compute square_root(a)

Idea: Iteratively refine a guess x about the square root of a

9

Page 67: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Special Case: Square Roots

How to compute square_root(a)

Idea: Iteratively refine a guess x about the square root of a

9

Update:

Page 68: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

� =�+ �

��

Special Case: Square Roots

How to compute square_root(a)

Idea: Iteratively refine a guess x about the square root of a

9

Update:

Page 69: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

� =�+ �

��

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

Page 70: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

� =�+ �

��

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

Page 71: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

� =�+ �

��

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

Page 72: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

� =�+ �

��

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

Page 73: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Special Case: Cube Roots

10

Page 74: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Special Case: Cube Roots

How to compute cube_root(a)

Idea: Iteratively refine a guess x about the cube root of a

10

Page 75: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Special Case: Cube Roots

How to compute cube_root(a)

Idea: Iteratively refine a guess x about the cube root of a

10

Update:

Page 76: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

� =� · �+ �

��

Special Case: Cube Roots

How to compute cube_root(a)

Idea: Iteratively refine a guess x about the cube root of a

10

Update:

Page 77: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

� =� · �+ �

��

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:

Page 78: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

� =� · �+ �

��

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:

Page 79: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

� =� · �+ �

��

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:

Page 80: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Iterative Improvement

(Demo)

11

Page 81: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Iterative Improvement

(Demo)

11

6_newton.py Page 1

"""Iterative improvement."""

from ucb import main, interact

# Direct implementations

def square_root(a): """Return the square root of a. >>> square_root(9) 3.0 """ x = 1 while x * x != a: x = square_root_update(x, a) return x

def square_root_update(x, a): return average(x, a/x)

def average(x, y): return (x + y)/2

def cube_root(a): """Return the cube root of a. >>> cube_root(27) 3.0 """ x = 1 while pow(x, 3) != a: x = cube_root_update(x, a) return x

def cube_root_update(x, a): return (2*x + a/(x * x))/3

# Iterative improvement

def golden_update(x): return 1/x + 1

def golden_test(x): return x * x == x + 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

def square_root_improve(a): """Return the square root of a. >>> square_root_improve(9) 3.0 """ def update(guess): return square_root_update(guess, a) def done(guess): return guess * guess == a return iter_improve(update, done)

Page 82: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Iterative Improvement

(Demo)

11

newton.py Page 1

"""Iterative improvement."""

from ucb import main, interact

def average(x, y): return (x + y)/2

def square_root(x): """Return the square root of x. >>> square_root(9) 3.0 """ guess = 1 while not guess * guess == x: guess = square_root_update(guess, x) return guess

def cube_root(x): """Return the cube root of x. >>> cube_root(27) 3.0 """ guess = 1 while not guess * guess * guess == x: guess = cube_root_update(guess, x) return guess

def square_root_update(guess, x): return average(guess, x / guess)

def cube_root_update(guess, x): return (x / (guess * guess) + 2 * guess)/3

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 −− A number update −− A function from guesses to guesses done −− A function from guesses to boolean values

>>> iter_improve(golden_update, golden_test) 1.618033988749895 """ n = 0 while not done(guess) and n < max_updates: guess = update(guess) n = n + 1 return guess

def square_root_improve(x): """Return the square root of x. >>> square_root_improve(9) 3.0 """ def update(guess): return square_root_update(guess, x) def done(guess): return guess * guess == x return iter_improve(update, done)

def cube_root_improve(x): """Return the cube root of x. >>> cube_root_improve(27)

6_newton.py Page 1

"""Iterative improvement."""

from ucb import main, interact

# Direct implementations

def square_root(a): """Return the square root of a. >>> square_root(9) 3.0 """ x = 1 while x * x != a: x = square_root_update(x, a) return x

def square_root_update(x, a): return average(x, a/x)

def average(x, y): return (x + y)/2

def cube_root(a): """Return the cube root of a. >>> cube_root(27) 3.0 """ x = 1 while pow(x, 3) != a: x = cube_root_update(x, a) return x

def cube_root_update(x, a): return (2*x + a/(x * x))/3

# Iterative improvement

def golden_update(x): return 1/x + 1

def golden_test(x): return x * x == x + 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

def square_root_improve(a): """Return the square root of a. >>> square_root_improve(9) 3.0 """ def update(guess): return square_root_update(guess, a) def done(guess): return guess * guess == a return iter_improve(update, done)

Page 83: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Iterative Improvement

(Demo)

11

newton.py Page 1

"""Iterative improvement."""

from ucb import main, interact

def average(x, y): return (x + y)/2

def square_root(x): """Return the square root of x. >>> square_root(9) 3.0 """ guess = 1 while not guess * guess == x: guess = square_root_update(guess, x) return guess

def cube_root(x): """Return the cube root of x. >>> cube_root(27) 3.0 """ guess = 1 while not guess * guess * guess == x: guess = cube_root_update(guess, x) return guess

def square_root_update(guess, x): return average(guess, x / guess)

def cube_root_update(guess, x): return (x / (guess * guess) + 2 * guess)/3

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 −− A number update −− A function from guesses to guesses done −− A function from guesses to boolean values

>>> iter_improve(golden_update, golden_test) 1.618033988749895 """ n = 0 while not done(guess) and n < max_updates: guess = update(guess) n = n + 1 return guess

def square_root_improve(x): """Return the square root of x. >>> square_root_improve(9) 3.0 """ def update(guess): return square_root_update(guess, x) def done(guess): return guess * guess == x return iter_improve(update, done)

def cube_root_improve(x): """Return the cube root of x. >>> cube_root_improve(27)

newton.py Page 1

"""Iterative improvement."""

from ucb import main, interact

def average(x, y): return (x + y)/2

def square_root(x): """Return the square root of x. >>> square_root(9) 3.0 """ guess = 1 while not guess * guess == x: guess = square_root_update(guess, x) return guess

def cube_root(x): """Return the cube root of x. >>> cube_root(27) 3.0 """ guess = 1 while not guess * guess * guess == x: guess = cube_root_update(guess, x) return guess

def square_root_update(guess, x): return average(guess, x / guess)

def cube_root_update(guess, x): return (x / (guess * guess) + 2 * guess)/3

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 −− A number update −− A function from guesses to guesses done −− A function from guesses to boolean values

>>> iter_improve(golden_update, golden_test) 1.618033988749895 """ n = 0 while not done(guess) and n < max_updates: guess = update(guess) n = n + 1 return guess

def square_root_improve(x): """Return the square root of x. >>> square_root_improve(9) 3.0 """ def update(guess): return square_root_update(guess, x) def done(guess): return guess * guess == x return iter_improve(update, done)

def cube_root_improve(x): """Return the cube root of x. >>> cube_root_improve(27)

6_newton.py Page 1

"""Iterative improvement."""

from ucb import main, interact

# Direct implementations

def square_root(a): """Return the square root of a. >>> square_root(9) 3.0 """ x = 1 while x * x != a: x = square_root_update(x, a) return x

def square_root_update(x, a): return average(x, a/x)

def average(x, y): return (x + y)/2

def cube_root(a): """Return the cube root of a. >>> cube_root(27) 3.0 """ x = 1 while pow(x, 3) != a: x = cube_root_update(x, a) return x

def cube_root_update(x, a): return (2*x + a/(x * x))/3

# Iterative improvement

def golden_update(x): return 1/x + 1

def golden_test(x): return x * x == x + 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

def square_root_improve(a): """Return the square root of a. >>> square_root_improve(9) 3.0 """ def update(guess): return square_root_update(guess, a) def done(guess): return guess * guess == a return iter_improve(update, done)

Page 84: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

���� = lim��

����������

Derivatives of Single-Argument Functions

12

Page 85: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

-4 -3 -2 -1 0 1 2 3 4 5

-3

-2

-1

1

2

3

���� = lim��

����������

Derivatives of Single-Argument Functions

12

Page 86: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

-4 -3 -2 -1 0 1 2 3 4 5

-3

-2

-1

1

2

3

���� = lim��

����������

Derivatives of Single-Argument Functions

12

x = 1

Page 87: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

-4 -3 -2 -1 0 1 2 3 4 5

-3

-2

-1

1

2

3

���� = lim��

����������

Derivatives of Single-Argument Functions

12

x = 1

x + h = 1.1

Page 88: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

-4 -3 -2 -1 0 1 2 3 4 5

-3

-2

-1

1

2

3

���� = lim��

����������

Derivatives of Single-Argument Functions

12

x = 1

x + h = 1.1

Page 89: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

-4 -3 -2 -1 0 1 2 3 4 5

-3

-2

-1

1

2

3

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

Page 90: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Approximating Derivatives

(Demo)

13

Page 91: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Implementing Newton's Method

14

Page 92: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Implementing Newton's Method

14

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

Page 93: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Implementing Newton's Method

14

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

Could be replaced with the exact derivative

Page 94: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Implementing Newton's Method

14

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

Could be replaced with the exact derivative

Page 95: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Implementing Newton's Method

14

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

Could be replaced with the exact derivative

Limit approximated by a small value

Page 96: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Implementing Newton's Method

14

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

Could be replaced with the exact derivative

Limit approximated by a small value

Page 97: 61A Lecture 6cs61a/fa12/slides/06...Lambda Expressions Versus Def Statements 3 square = lambda x: x * x def square(x): VS return x * x • Both create a function with the same arguments

Implementing Newton's Method

14

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

newton.py Page 2

3.0 """ def update(guess): return cube_root_update(guess, x) def done(guess): return guess * guess * guess == x return iter_improve(update, done)

def square_root_newton(x): """Return the square root of x. >>> square_root_newton(9) 3.0 """ return find_root(lambda y: y * y − x)

def cube_root_newton(x): """Return the cube root of x. >>> cube_root_newton(27) 3.0 """ return find_root(lambda y: y * y * y − x)

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

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)

@maindef run(): interact()

Could be replaced with the exact derivative

Limit approximated by a small value

Definition of a function zero