61a lecture 6 - university of california, berkeleycs61a/fa11/61a... · friday, september 9, 2011....

Post on 25-Aug-2021

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

61A Lecture 6

Friday, September 9

Friday, September 9, 2011

Lambda Expressions

2

Friday, September 9, 2011

Lambda Expressions

2

>>> ten = 10

Friday, September 9, 2011

Lambda Expressions

2

>>> ten = 10

>>> square = x * x

Friday, September 9, 2011

Lambda Expressions

2

>>> ten = 10

>>> square = x * x

An expression: this one evaluates to a number

Friday, September 9, 2011

Lambda Expressions

2

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

An expression: this one evaluates to a number

Friday, September 9, 2011

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

Friday, September 9, 2011

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

Friday, September 9, 2011

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

Friday, September 9, 2011

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

Friday, September 9, 2011

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"

Friday, September 9, 2011

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

Friday, September 9, 2011

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

Friday, September 9, 2011

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

Friday, September 9, 2011

Lambda Expressions Versus Def Statements

3

Friday, September 9, 2011

Lambda Expressions Versus Def Statements

3

VS

Friday, September 9, 2011

Lambda Expressions Versus Def Statements

3

square = lambda x: x * x VS

Friday, September 9, 2011

Lambda Expressions Versus Def Statements

3

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

Friday, September 9, 2011

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

Friday, September 9, 2011

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

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

Friday, September 9, 2011

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

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

• Both bind that function to the name "square"

Friday, September 9, 2011

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

• 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

Friday, September 9, 2011

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

• 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

<lambda>(x):

return x * x

Friday, September 9, 2011

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

• 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

<lambda>(x):

return x * x

square(x):

return x * x

Friday, September 9, 2011

Function Currying

4

Friday, September 9, 2011

Function Currying

4

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

Friday, September 9, 2011

Function Currying

4

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

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

Friday, September 9, 2011

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

Friday, September 9, 2011

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.

Friday, September 9, 2011

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.

Friday, September 9, 2011

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?

Friday, September 9, 2011

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

Friday, September 9, 2011

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

y = x2 - 2

Friday, September 9, 2011

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 2.5 5

-2.5

2.5

y = x2 - 2

Friday, September 9, 2011

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 2.5 5

-2.5

2.5

y = x2 - 2 A "zero"

Friday, September 9, 2011

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 2.5 5

-2.5

2.5

y = x2 - 2 A "zero"

x=1.414213562373095

Friday, September 9, 2011

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 2.5 5

-2.5

2.5

y = x2 - 2 A "zero"

Application: a method for (approximately) computing square roots, using only basic arithmetic.

x=1.414213562373095

Friday, September 9, 2011

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 2.5 5

-2.5

2.5

y = x2 - 2 A "zero"

Application: a method for (approximately) computing square roots, using only basic arithmetic.

The positive zero of y = x2 - a is

x=1.414213562373095

Friday, September 9, 2011

Newton's Method Background

Finds approximations to zeroes of differentiable functions

5

-5 -2.5 0 2.5 5

-2.5

2.5

y = x2 - 2 A "zero"

Application: a method for (approximately) computing square roots, using only basic arithmetic.

The positive zero of y = x2 - a is

x=1.414213562373095

Friday, September 9, 2011

Newton's Method

6

Begin with a function f and

an initial guess x

− )

Friday, September 9, 2011

Newton's Method

6

Begin with a function f and

an initial guess x

− )

Friday, September 9, 2011

Newton's Method

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

6

Begin with a function f and

an initial guess x

− )

Friday, September 9, 2011

Newton's Method

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

6

Begin with a function f and

an initial guess x

(x, f(x))

− )

Friday, September 9, 2011

Newton's Method

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

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

6

Begin with a function f and

an initial guess x

(x, f(x))

− )

Friday, September 9, 2011

Newton's Method

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

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

3. Update guess x to be:

6

Begin with a function f and

an initial guess x

(x, f(x))

− )

Friday, September 9, 2011

Newton's Method

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

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

3. Update guess x to be:

6

Begin with a function f and

an initial guess x

(x, f(x))

-f(x)

− )

Friday, September 9, 2011

Newton's Method

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

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

3. Update guess x to be:

6

Begin with a function f and

an initial guess x

(x, f(x))

-f(x)/f'(x)

-f(x)

− )

Friday, September 9, 2011

Newton's Method

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

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

3. Update guess x to be:

6

Begin with a function f and

an initial guess x

(x, f(x))

-f(x)/f'(x)

-f(x)

− )

Friday, September 9, 2011

Visualization of Newton's Method

7

(Demo)

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

Friday, September 9, 2011

Using Newton's Method

8

f(x)=x2 - 2

Friday, September 9, 2011

Using Newton's Method

8

How to find the square root of 2?

f(x)=x2 - 2

Friday, September 9, 2011

Using Newton's Method

8

>>> f = lambda x: x*x - 2>>> find_root(f, 1)

How to find the square root of 2?

f(x)=x2 - 2

Friday, September 9, 2011

Using Newton's Method

8

>>> f = lambda x: x*x - 2>>> find_root(f, 1)

How to find the square root of 2?

How to find the log base 2 of 1024?

f(x)=x2 - 2

Friday, September 9, 2011

Using Newton's Method

8

>>> f = lambda x: x*x - 2>>> find_root(f, 1)

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_root(g, 1)

f(x)=x2 - 2

Friday, September 9, 2011

Using Newton's Method

8

>>> f = lambda x: x*x - 2>>> find_root(f, 1)

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_root(g, 1)

What number is one less than its square?

f(x)=x2 - 2

Friday, September 9, 2011

Using Newton's Method

8

>>> f = lambda x: x*x - 2>>> find_root(f, 1)

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_root(g, 1)

What number is one less than its square?

>>> h = lambda x: x*x - (x+1)>>> find_root(h, 1)

f(x)=x2 - 2

Friday, September 9, 2011

=+

Special Case: Square Roots

9

Babylonian Method

Friday, September 9, 2011

=+

Special Case: Square Roots

How to compute square_root(a)

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

9

Babylonian Method

Friday, September 9, 2011

=+

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

Friday, September 9, 2011

=+

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?

Update:

Babylonian Method

Friday, September 9, 2011

=+

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?

Update:

Babylonian Method

Friday, September 9, 2011

=+

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

Friday, September 9, 2011

=· +

Special Case: Square Roots

10

Friday, September 9, 2011

=· +

Special Case: Square Roots

How to compute cube_root(a)

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

10

Friday, September 9, 2011

=· +

Special Case: Square 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?

Friday, September 9, 2011

=· +

Special Case: Square 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?

Friday, September 9, 2011

=· +

Special Case: Square 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:

Friday, September 9, 2011

=· +

Special Case: Square 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:

Friday, September 9, 2011

Iterative Improvement

(Demo)

11

Friday, September 9, 2011

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)

Friday, September 9, 2011

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)

Friday, September 9, 2011

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)

Friday, September 9, 2011

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

Friday, September 9, 2011

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

update:done:guess: 1iter_improve

Friday, September 9, 2011

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

1

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

12.0

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

12.01.5

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

12.01.51.6666666666666665

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

12.01.51.66666666666666651.6

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

12.01.51.66666666666666651.61.625

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

12.01.51.66666666666666651.61.6251.6153846153846154

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

12.01.51.66666666666666651.61.6251.61538461538461541.619047619047619

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

12.01.51.66666666666666651.61.6251.61538461538461541.6190476190476191.6176470588235294

Friday, September 9, 2011

guess:

Iterative Improvement

12

golden_update:golden_test:iter_improve:

golden_update

golden_test

iter_improve

guess: 1golden_test

update:done:guess: 1iter_improve

12.01.51.66666666666666651.61.6251.61538461538461541.6190476190476191.61764705882352941.6181818181818182

Friday, September 9, 2011

Square Roots by Iterative Improvement

13

(Demo)

Friday, September 9, 2011

Square Roots by Iterative Improvement

13

iter_improve:square_root

iter_improve

square_root:

...

(Demo)

Friday, September 9, 2011

Square Roots by Iterative Improvement

13

iter_improve:square_root

iter_improve

square_root:

...

square_root(256)

(Demo)

Friday, September 9, 2011

Square Roots by Iterative Improvement

13

iter_improve:square_root

iter_improve

square_root:

...

square_root(256)

(Demo)

Friday, September 9, 2011

square_root(256)

Square Roots by Iterative Improvement

13

iter_improve:square_root

iter_improve

square_root:

...

square_root(256)

(Demo)

Friday, September 9, 2011

a:

square_root

256

square_root(256)

Square Roots by Iterative Improvement

13

iter_improve:square_root

iter_improve

square_root:

...

square_root(256)

(Demo)

Friday, September 9, 2011

a:

square_root

256

square_root(256)

Square Roots by Iterative Improvement

13

iter_improve:square_root

iter_improve

square_root:

...

square_root(256)

def update(guess): ... ... return iter_improve(...)

(Demo)

Friday, September 9, 2011

a:

square_root

256

square_root(256)

Square Roots by Iterative Improvement

13

iter_improve:square_root

iter_improve

square_root:

...

square_root(256)

def update(guess): ... ... return iter_improve(...)

(Demo)

Friday, September 9, 2011

a:

square_root

256

square_root(256)

Square Roots by Iterative Improvement

13

update(guess):return ...

update:

iter_improve:square_root

iter_improve

square_root:

...

square_root(256)

def update(guess): ... ... return iter_improve(...)

(Demo)

Friday, September 9, 2011

a:

square_root

256

square_root(256)

Square Roots by Iterative Improvement

13

update(guess):return ...

update:

iter_improve:square_root

iter_improve

square_root:

...

square_root(256)

def update(guess): ... ... return iter_improve(...)

(Demo)

done: ...

Friday, September 9, 2011

a:

square_root

256

iter_improve:square_root

iter_improve

square_root:

...

update(guess):return ...

Square Roots by Iterative Improvement

14

square_root

update:done: ...

square_root(256) ... return iter_improve(...) square_root(256)

(Demo)

Friday, September 9, 2011

a:

square_root

256

iter_improve:square_root

iter_improve

square_root:

...

update(guess):return ...

Square Roots by Iterative Improvement

14

square_root

update:done: ...

square_root(256) ... return iter_improve(...) square_root(256)

iter_improve

update:guess: 1

...

(Demo)

Friday, September 9, 2011

a:

square_root

256

iter_improve:square_root

iter_improve

square_root:

...

update(guess):return ...

Square Roots by Iterative Improvement

14

square_root

update:done: ...

square_root(256) ... return iter_improve(...)

update

guess:1

square_root(256)

iter_improve

update:guess: 1

...

(Demo)

Friday, September 9, 2011

= lim→

Derivatives of Single-Argument Functions

15

Friday, September 9, 2011

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

-3

-2

-1

1

2

3

= lim→

Derivatives of Single-Argument Functions

15

Friday, September 9, 2011

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

-3

-2

-1

1

2

3

= lim→

Derivatives of Single-Argument Functions

15

x = 1

Friday, September 9, 2011

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

-3

-2

-1

1

2

3

= lim→

Derivatives of Single-Argument Functions

15

x = 1

x + h = 1.1

Friday, September 9, 2011

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

-3

-2

-1

1

2

3

= lim→

Derivatives of Single-Argument Functions

15

x = 1

x + h = 1.1

Friday, September 9, 2011

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

-3

-2

-1

1

2

3

= lim→

Derivatives of Single-Argument Functions

15

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

(Demo)

x = 1

x + h = 1.1

Friday, September 9, 2011

Approximating Derivatives

(Demo)

16

Friday, September 9, 2011

Implementing Newton's Method

17

Friday, September 9, 2011

Implementing Newton's Method

17

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

Friday, September 9, 2011

Implementing Newton's Method

17

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

Friday, September 9, 2011

Implementing Newton's Method

17

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

Friday, September 9, 2011

Implementing Newton's Method

17

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

Friday, September 9, 2011

Implementing Newton's Method

17

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

Friday, September 9, 2011

Implementing Newton's Method

17

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

Friday, September 9, 2011

top related