61A Lecture 6. Friday, September 7

Similar documents
Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

61A Lecture 4. Monday, September 9

61A Lecture 25. Friday, October 28

Lecture #6: Higher-Order Functions at Work

RECAP: HIGHER ORDER FUNCTIONS

CS61A Lecture 5 Applications of Higher Order Functions. Jon Kotker and Tom Magrino UC Berkeley EECS June 25, 2012

61A Lecture 3. Friday, September 5

61A LECTURE 7 DATA ABSTRACTION. Steven Tang and Eric Tzeng July 3, 2013

ENVIRONMENT DIAGRAMS AND RECURSION 2

HIGHER-ORDER FUNCTIONS 2

61A Lecture 2. Wednesday, September 4, 2013

CONTROL AND HIGHER ORDER FUNCTIONS 2

Midterm Exam 1 Solutions

CONTROL AND HIGHER ORDER FUNCTIONS 1

61A Lecture 2. Friday, August 28, 2015

ITERATION AND RECURSION 3

Introduction to Functional Programming

Chapter 1. Fundamentals of Higher Order Programming

CS61A Lecture 6 Recursion. Tom Magrino and Jon Kotker UC Berkeley EECS June 26, 2012

Structure and Interpretation of Computer Programs

callback, iterators, and generators

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

functional programming in Python, part 2

Working with Lists 4

Numerical Methods Lecture 7 - Optimization

CS61A Lecture 4 Higher Order Functions. Tom Magrino and Jon Kotker UC Berkeley EECS June 21, 2012

Higher Order Functions in Haskell

Principles of Programming Languages

Chapter 15 Functional Programming Languages

Numerical Integration

Generic descent algorithm Generalization to multiple dimensions Problems of descent methods, possible improvements Fixes Local minima

LINEARIZATION, NEWTON S METHOD BACKGROUND PREPARATIONS

Defining Functions III: Nested Definitions

CONTROL AND ENVIRONMENTS 1

Concepts of Programming Languages

Today s class. Roots of equation Finish up incremental search Open methods. Numerical Methods, Fall 2011 Lecture 5. Prof. Jinbo Bi CSE, UConn

Lecture #3: Environments

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Structure and Interpretation of Computer Programs

Computational Mathematics/Information Technology. Worksheet 2 Iteration and Excel

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

Section 1.6 & 1.7 Parent Functions and Transformations

Structure and Interpretation of Computer Programs

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, Higher Order Functions. HOFs in Environment Diagrams

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Introduction to Scheme

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Lecture 21: Functional Programming in Python. List Comprehensions

CIS192 Python Programming. Robert Rand. August 27, 2015

And Now to Something Completely Different: Finding Roots of Real Valued Functions

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, 2018 Solutions. 1 Higher Order Functions. HOFs in Environment Diagrams

ENVIRONMENT DIAGRAMS AND RECURSION 2

C ONTROL AND H IGHER O RDER F UNCTIONS

Introduction to Functions

Introductory Example

CS Programming Languages: Scala

Defining Functions. turning expressions into functions. writing a function definition defining and using modules

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

Arbitrary Precision and Symbolic Calculations

! The simplest building blocks of a language. ! Compound elements are built from simpler ones

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

The Bisection Method versus Newton s Method in Maple (Classic Version for Windows)

Functional Programming in Scala. Raj Sunderraman

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Functional Programming

Lecture 5: Lazy Evaluation and Infinite Data Structures

September 18, B Math Test Chapter 1 Name: x can be expressed as: {y y 0, y R}.

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

DECOMPOSITION, ABSTRACTION, FUNCTIONS

This document describes how I implement the Newton method using Python and Fortran on the test function f(x) = (x 1) log 10 (x).

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

operator overloading algorithmic differentiation the class DifferentialNumber operator overloading

1. Practice the use of the C ++ repetition constructs of for, while, and do-while. 2. Use computer-generated random numbers.

Types, Type Inference and Unification

SCHEME AND CALCULATOR 5b

Sect Graphing Techniques: Transformations

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Higher-Order Procedures

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

Introduction to the l-calculus

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples

Introduction to Programming

Lecture #5: Higher-Order Functions. A Simple Recursion. Avoiding Recalculation. Redundant Calculation. Tail Recursion and Repetition

HIGHER ORDER FUNCTIONS AND ENVIRONMENT DIAGRAMS 2

CSci 4223 Principles of Programming Languages

Function Transformations and Symmetry

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

REVIEW I MATH 254 Calculus IV. Exam I (Friday, April 29) will cover sections

61A Lecture 28. Friday, November 4

Unit testing with pytest and nose 1

Organization of Programming Languages CS3200/5200N. Lecture 11

Hw 4 Due Feb 22. D(fg) x y z (

Functional Programming Languages (FPL)

Ruby: Introduction, Basics

61A Lecture 9. Friday, September 20

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

DRAWING ENVIRONMENT DIAGRAMS

Logical and Function Constructions

HW DUE Floating point

Transcription:

61A Lecture 6 Friday, September 7

Lambda Expressions >>> ten = 10 An expression: this one evaluates to a number >>> square = x * x Also an expression: evaluates to a function >>> square = lambda x: x * x >>> square(4) 16 A function with formal parameter x Notice: no "return" and body "return x * x" Must be a single expression Lambda expressions are rare in Python, but important in general 2

Lambda Expressions Versus Def Statements square = lambda x: x * x VS def square(x): return x * x 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 3

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

Newton's Method Background Finds approximations to zeroes of differentiable functions 2.5 f(x) = x 2-2 A "zero" 5-2.5 0 2.5 5-2.5 x=1.414213562373095 Application: a method for (approximately) computing square roots, using only basic arithmetic. The positive zero of f(x) = x 2 - a is 5

Newton's Method Begin with a function f and an initial guess x -f(x)/f'(x) -f(x) (x, f(x)) 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

Visualization of Newton's Method (Demo) http://en.wikipedia.org/wiki/file:newtoniteration_ani.gif 7

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

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 Implementation questions: What guess should start the computation? How do we know when we are finished? 9

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

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

Derivatives of Single-Argument Functions =lim 1 2-1 0 1 2-1 x = 1 x + h = 1.1-2 (Demo) http://en.wikipedia.org/wiki/file:graph_of_sliding_derivative_line.gif 12

Approximating Derivatives (Demo) 13

Implementing Newton's Method def newton_update(f): """Return an update function for f using Newton's method.""" def update(x): return x f(x) / approx_derivative(f, x) return update Could be replaced with the exact derivative 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 Limit approximated by a small value def find_root(f, guess=1): """Return a guess of a zero of the function f, near guess. >>> from math import sin Definition of a >>> find_root(lambda y: sin(y), 3) 3.141592653589793 function zero """ return iter_improve(newton_update(f), lambda x: f(x) == 0, guess) 14