CS61A Lecture 7 - University of California,...

112
CS61A Lecture 7 Amir Kamil UC Berkeley February 6, 2013

Transcript of CS61A Lecture 7 - University of California,...

Page 1: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

CS61A Lecture 7

Amir KamilUC Berkeley

February 6, 2013

Page 2: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

HW3 out, due Tuesday at 7pmMidterm next Wednesday at 7pm

Keep an eye out for your assigned location Old exams posted soon Review sessions

Saturday 2‐4pm in TBA Extend office hours Sunday 11‐3pm in TBA HKN review session Sunday 3‐6pm in 145 Dwinelle

Environment diagram handout on website Code review system online

See Piazza post for details

Announcements

Page 3: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

 

 

 

 

 

Page 4: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

When defining a function:

 

 

 

 

Page 5: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

When defining a function:

Create a function value with signature <name>(<formal parameters>)

 

 

 

Page 6: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

When defining a function:

Create a function value with signature <name>(<formal parameters>)

For nested definitions, label the parent as the first frame of the current environment

 

 

Page 7: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

When defining a function:

Create a function value with signature <name>(<formal parameters>)

For nested definitions, label the parent as the first frame of the current environment

Bind <name> to the function value in the first frame of the current environment

 

Page 8: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

When defining a function:

Create a function value with signature <name>(<formal parameters>)

For nested definitions, label the parent as the first frame of the current environment

Bind <name> to the function value in the first frame of the current environment

When calling a function:

Page 9: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

When defining a function:

Create a function value with signature <name>(<formal parameters>)

For nested definitions, label the parent as the first frame of the current environment

Bind <name> to the function value in the first frame of the current environment

When calling a function:

1. Add a local frame labeled with the <name> of the function

Page 10: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

When defining a function:

Create a function value with signature <name>(<formal parameters>)

For nested definitions, label the parent as the first frame of the current environment

Bind <name> to the function value in the first frame of the current environment

When calling a function:

1. Add a local frame labeled with the <name> of the function

2. If the function has a parent label, copy it to this frame

Page 11: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

When defining a function:

Create a function value with signature <name>(<formal parameters>)

For nested definitions, label the parent as the first frame of the current environment

Bind <name> to the function value in the first frame of the current environment

When calling a function:

1. Add a local frame labeled with the <name> of the function

2. If the function has a parent label, copy it to this frame

3. Bind the <formal parameters> to the arguments in this frame

Page 12: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

How to Draw an Environment Diagram

When defining a function:

Create a function value with signature <name>(<formal parameters>)

For nested definitions, label the parent as the first frame of the current environment

Bind <name> to the function value in the first frame of the current environment

When calling a function:

1. Add a local frame labeled with the <name> of the function

2. If the function has a parent label, copy it to this frame

3. Bind the <formal parameters> to the arguments in this frame

4. Execute the body of the function in the environment that starts with this frame

Page 13: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

Example: http://goo.gl/5zcug

Page 14: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

Example: http://goo.gl/5zcug

Page 15: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

Example: http://goo.gl/5zcug

Page 16: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

Example: http://goo.gl/5zcug

Page 17: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

Example: http://goo.gl/5zcug

Page 18: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

Example: http://goo.gl/5zcug

Page 19: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

Example: http://goo.gl/5zcug

Page 20: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

Example: http://goo.gl/5zcug

Page 21: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

Example: http://goo.gl/5zcug

Page 22: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

2

1

3

Example: http://goo.gl/5zcug

Page 23: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

2

1

3

Example: http://goo.gl/5zcug

Page 24: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

2

1

3

Example: http://goo.gl/5zcug

Page 25: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Environment for Function Composition

2

1

3

1

2

3

Example: http://goo.gl/5zcug

Page 26: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

Page 27: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

Page 28: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

Page 29: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

An expression: this one evaluates to a number

Page 30: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

An expression: this one evaluates to a number

Page 31: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

An expression: this one evaluates to a number

Also an expression: evaluates to a function

Page 32: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

An expression: this one evaluates to a number

Also an expression: evaluates to a function

A function

Page 33: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

An expression: this one evaluates to a number

Also an expression: evaluates to a function

with formal parameter xA function

Page 34: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

An expression: this one evaluates to a number

Also an expression: evaluates to a function

and body "return x * x"with formal parameter x

A function

Page 35: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

An expression: this one evaluates to a number

Also an expression: evaluates to a function

and body "return x * x"with formal parameter x

A functionNotice: no "return"

Page 36: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

An expression: this one evaluates to a number

Also an expression: evaluates to a function

and body "return x * x"with formal parameter x

A functionNotice: no "return"

Must be a single expression

Page 37: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

>>> square(4)16

An expression: this one evaluates to a number

Also an expression: evaluates to a function

and body "return x * x"with formal parameter x

A functionNotice: no "return"

Must be a single expression

Page 38: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda Expressions

>>> ten = 10

>>> square = x * x

>>> square = lambda x: x * x

>>> square(4)16

An expression: this one evaluates to a number

Also an expression: evaluates to a function

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 39: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

 

 

 

 

Page 40: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

 

 

 

 

lambda x: x * x

Page 41: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

 

 

 

 

lambda x: x * x VS

Page 42: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

 

 

 

 

lambda x: x * xdef square(x):

return x * xVS

Page 43: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

Execution procedure for def statements: 

 

 

 

lambda x: x * xdef square(x):

return x * xVS

Page 44: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

Execution procedure for def statements:1. Create a function value with signature 

<name>(<formal parameters>)and the current frame as parent

 

 

 

lambda x: x * xdef square(x):

return x * xVS

Page 45: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

Execution procedure for def statements:1. Create a function value with signature 

<name>(<formal parameters>)and the current frame as parent

2. Bind <name> to that value in the current frame

 

 

lambda x: x * xdef square(x):

return x * xVS

Page 46: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

Execution procedure for def statements:1. Create a function value with signature 

<name>(<formal parameters>)and the current frame as parent

2. Bind <name> to that value in the current frame

Evaluation procedure for lambda expressions: 

 

lambda x: x * xdef square(x):

return x * xVS

Page 47: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

Execution procedure for def statements:1. Create a function value with signature 

<name>(<formal parameters>)and the current frame as parent

2. Bind <name> to that value in the current frame

Evaluation procedure for lambda expressions:1. Create a function value with signature 

(<formal parameters>)and the current frame as parent

 

lambda x: x * xdef square(x):

return x * xVS

Page 48: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

Execution procedure for def statements:1. Create a function value with signature 

<name>(<formal parameters>)and the current frame as parent

2. Bind <name> to that value in the current frame

Evaluation procedure for lambda expressions:1. Create a function value with signature 

(<formal parameters>)and the current frame as parent

 

lambda x: x * xdef square(x):

return x * xVS

Page 49: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Evaluation of Lambda vs. Def

Execution procedure for def statements:1. Create a function value with signature 

<name>(<formal parameters>)and the current frame as parent

2. Bind <name> to that value in the current frame

Evaluation procedure for lambda expressions:1. Create a function value with signature 

(<formal parameters>)and the current frame as parent

2. Evaluate to that value

lambda x: x * xdef square(x):

return x * xVS

Page 50: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

 

 

 

 

Page 51: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * x

 

 

 

 

Page 52: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * x VS 

 

 

 

Page 53: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * xdef square(x):

return x * xVS 

 

 

 

Page 54: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * xdef square(x):

return x * xVSBoth create a function with the same arguments & behavior

 

 

 

Page 55: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * xdef square(x):

return x * xVSBoth create a function with the same arguments & behavior

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

 

 

Page 56: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * xdef square(x):

return x * xVSBoth 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 57: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * xdef square(x):

return x * xVSBoth 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 58: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * xdef square(x):

return x * xVSBoth 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 59: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * xdef square(x):

return x * xVSBoth 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 60: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * xdef square(x):

return x * xVSBoth 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 61: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Lambda vs. Def Statements

square = lambda x: x * xdef square(x):

return x * xVSBoth 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 62: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method Background

Finds approximations to zeroes of differentiable functions 

 

 

Page 63: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method Background

Finds approximations to zeroes of differentiable functions 

f(x) = x2 ‐ 2

 

 

Page 64: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method Background

Finds approximations to zeroes of differentiable functions 

f(x) = x2 ‐ 2

 

 

Page 65: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method Background

Finds approximations to zeroes of differentiable functions 

f(x) = x2 ‐ 2 A “zero”

 

 

Page 66: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method Background

Finds approximations to zeroes of differentiable functions 

f(x) = x2 ‐ 2 A “zero”

 

 

x=1.414213562373095

Page 67: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method Background

Finds approximations to zeroes of differentiable functions 

f(x) = x2 ‐ 2 A “zero”

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

 

x=1.414213562373095

Page 68: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method Background

Finds approximations to zeroes of differentiable functions 

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 69: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method

Begin with a function f and an initial guess x

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

 

 

 

Page 70: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method

Begin with a function f and an initial guess x

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

 

 

 

Page 71: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method

Begin with a function f and an initial guess x

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

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

 

 

Page 72: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method

Begin with a function f and an initial guess x

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

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

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

 

Page 73: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method

Begin with a function f and an initial guess x

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

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

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

Update guess to be: 

Page 74: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method

Begin with a function f and an initial guess x

(x, f(x))

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

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

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

Update guess to be: 

Page 75: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method

Begin with a function f and an initial guess x

(x, f(x))

‐f(x)

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

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

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

Update guess to be: 

Page 76: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method

Begin with a function f and an initial guess x

(x, f(x))

‐f(x)/f'(x)

‐f(x)

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

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

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

Update guess to be: 

Page 77: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method

Begin with a function f and an initial guess x

(x, f(x))

‐f(x)/f'(x)

‐f(x)

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

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

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

Update guess to be: 

Page 78: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Using Newton’s Method

Page 79: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Using Newton’s Method

How to find the square root of 2?

Page 80: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Using Newton’s Method

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

How to find the square root of 2?

Page 81: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Using Newton’s Method

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

How to find the square root of 2?

f(x) = x2 ‐ 2

Page 82: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Using Newton’s Method

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

How to find the square root of 2?

f(x) = x2 ‐ 2

Page 83: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Using Newton’s Method

>>> 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 84: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Using Newton’s Method

>>> 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 85: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Using Newton’s Method

>>> 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 g(x) = 2x ‐ 1024

f(x) = x2 ‐ 2

Page 86: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Using Newton’s Method

>>> 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 g(x) = 2x ‐ 1024

f(x) = x2 ‐ 2

Page 87: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Square Roots

 

 

Page 88: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Square Roots

How to compute square_root(a)

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

 

 

Page 89: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Square Roots

How to compute square_root(a)

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

 

 

Update:

Page 90: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Square Roots

How to compute square_root(a)

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

 

 

Update:

Page 91: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Square Roots

How to compute square_root(a)

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

 

 

Update:

x ‐ f(x)/f'(x)

Page 92: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Square Roots

How to compute square_root(a)

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

 

 

Update:

Babylonian Method

x ‐ f(x)/f'(x)

Page 93: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Square Roots

How to compute square_root(a)

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

 

 

Implementation questions:

Update:

Babylonian Method

x ‐ f(x)/f'(x)

Page 94: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Square Roots

How to compute square_root(a)

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

What guess should start the computation?

 

Implementation questions:

Update:

Babylonian Method

x ‐ f(x)/f'(x)

Page 95: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Square Roots

How to compute square_root(a)

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

What guess should start the computation?

How do we know when we are finished?

Implementation questions:

Update:

Babylonian Method

x ‐ f(x)/f'(x)

Page 96: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Cube Roots

 

 

Page 97: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Cube Roots

How to compute cube_root(a)

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

 

 

Page 98: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Cube Roots

How to compute cube_root(a)

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

 

 

Update:

Page 99: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Cube Roots

How to compute cube_root(a)

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

 

 

Update:

Page 100: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Cube Roots

How to compute cube_root(a)

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

 

 

Update:

x ‐ f(x)/f'(x)

Page 101: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Cube Roots

How to compute cube_root(a)

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

 

 

Implementation questions:

Update:

x ‐ f(x)/f'(x)

Page 102: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Cube Roots

How to compute cube_root(a)

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

What guess should start the computation?

 

Implementation questions:

Update:

x ‐ f(x)/f'(x)

Page 103: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Special Case: Cube Roots

How to compute cube_root(a)

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

What guess should start the computation?

How do we know when we are finished?

Implementation questions:

Update:

x ‐ f(x)/f'(x)

Page 104: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Iterative Improvement

Page 105: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Iterative Improvement

First, identify common structure.

Page 106: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Iterative Improvement

First, identify common structure.Then define a function that generalizes the procedure. 

Page 107: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Iterative Improvement

def iter_improve(update, done, guess=1, max_updates=1000):"""Iteratively improve guess with update until donereturns a true value.

>>> iter_improve(golden_update, golden_test)1.618033988749895"""k = 0while not done(guess) and k < max_updates:

guess = update(guess)k = k + 1

return guess

First, identify common structure.Then define a function that generalizes the procedure. 

Page 108: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method for nth Roots

Page 109: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method for nth Roots

def nth_root_func_and_derivative(n, a):def root_func(x):

return pow(x, n) - adef derivative(x):

return n * pow(x, n-1)return root_func, derivative

def nth_root_newton(a, n):"""Return the nth root of a.

>>> nth_root_newton(8, 3)2.0"""root_func, deriv = nth_root_func_and_derivative(n, a)def update(x):

return x - root_func(x) / deriv(x)def done(x):

return root_func(x) == 0return iter_improve(update, done)

Page 110: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method for nth Roots

def nth_root_func_and_derivative(n, a):def root_func(x):

return pow(x, n) - adef derivative(x):

return n * pow(x, n-1)return root_func, derivative

def nth_root_newton(a, n):"""Return the nth root of a.

>>> nth_root_newton(8, 3)2.0"""root_func, deriv = nth_root_func_and_derivative(n, a)def update(x):

return x - root_func(x) / deriv(x)def done(x):

return root_func(x) == 0return iter_improve(update, done)

Exact derivative

Page 111: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method for nth Roots

def nth_root_func_and_derivative(n, a):def root_func(x):

return pow(x, n) - adef derivative(x):

return n * pow(x, n-1)return root_func, derivative

def nth_root_newton(a, n):"""Return the nth root of a.

>>> nth_root_newton(8, 3)2.0"""root_func, deriv = nth_root_func_and_derivative(n, a)def update(x):

return x - root_func(x) / deriv(x)def done(x):

return root_func(x) == 0return iter_improve(update, done)

x – f(x)/f’(x)

Exact derivative

Page 112: CS61A Lecture 7 - University of California, Berkeleyinst.eecs.berkeley.edu/~cs61a/sp13/slides/07-LambdaNewton_1pp.pdf · For nested definitions, label the parent as the first frame

Newton’s Method for nth Roots

def nth_root_func_and_derivative(n, a):def root_func(x):

return pow(x, n) - adef derivative(x):

return n * pow(x, n-1)return root_func, derivative

def nth_root_newton(a, n):"""Return the nth root of a.

>>> nth_root_newton(8, 3)2.0"""root_func, deriv = nth_root_func_and_derivative(n, a)def update(x):

return x - root_func(x) / deriv(x)def done(x):

return root_func(x) == 0return iter_improve(update, done)

x – f(x)/f’(x)

Definition of a function zero

Exact derivative