290 likes | 306 Views
This lecture explores computing fixed points of functions and building abstractions with data. It covers topics such as finding fixed points, Newton's method, and procedural and data abstraction.
E N D
Lecture #6 section 1.3.3 pages 68-70 1.3.4 pages72-77 2.1.1 pages 83-87 2.1.2 pages 87-89 2.2 pages97-100 מבוא מורחב
Computing (SQRT a) 1. Find a fixed point of the function f(x) = a/x. 2. Define g(x)=x2 –a Find a fixed point of f(x)=x - g(x)/g’(x) מבוא מורחב
The derivative. • We want to write a procedure with: • Input: a function f: REAL REAL • Output: the function f’: REAL REAL deriv: (REAL REAL) (REAL REAL) • (define (deriv f) • (lambda (x) • (define dx 0.001) • (/ (- (f (+ x dx)) (f x)) dx))) > ((deriv square) 3) 6.000999999999479 מבוא מורחב
Finding fixed points for f(x) Start with an arbitrary first guess x1 Each time: • try the guess, f(x) ~ x ?? • If it’s not a good guess try the next guess xi+1 =f(xi) (define (fixed-point f first-guess) (define tolerance 0.00001) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) guess (try next)))) (try first-guess))
An example: f(x) = 1+1/x (define (f x) (+ 1 (/ 1 x))) (fixed-point f 1.0) X1 = 1.0 X2 = f(x1) = 2 X3 = f(x2) = 1.5 X4 = f(x3) = 1.666666666.. X5 = f(x4) = 1.6 X6 = f(x5) = 1.625 X7 = f(x6) = 1.6153846… Real answer: 1.6180339… Note how odd guesses underestimate And even guesses Overestimate. מבוא מורחב
Another example: f(x) = y/x (define (f x) (/ 2 x)) (fixed-point f 1.0) x1 = 1.0 x2 = f(x1) = 2 x3 = f(x2) = 1 x4 = f(x3) = 2 x5 = f(x4) = 1 x6 = f(x5) = 2 x7 = f(x6) = 1 Real answer: 1.414213562… מבוא מורחב
How do we deal with oscillation? Consider f(x)=2/x. If x is a point such that guess < sqrt(2) then 2/guess > sqrt(2) So the average of guess and 2/guess is always an even Better guess. So, we will try to find a fixed point of g(x)= (x + f(x))/2 Notice that g(x) = (x +f(x)) /2 has the same fixed points as f. For f(x)=2/x this gives: g(x)= x + 2/x)/2 מבוא מורחב
To find an approximation of x: • Make a guess G • Improve the guess by averaging G and x/G • Keep improving the guess until it is good enough X/G = 2 G = ½ (1+ 2) = 1.5 X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = 1.416666 X/G = 24/17 G = ½ (17/12 + 24/17) = 577/408 = 1.4142156 מבוא מורחב
average-damp (define (average-damp f) ;outputs g(x)=(x+f(x)/2 (lambda (x)(average x (f x)))) average-damp: (number number) (number number) ((average-damp square) 10) ((lambda (x) (average x (square x))) 10) (average 10 (square 10)) 55 מבוא מורחב
Fixed-point II (define (fixed-point-II f first-guess) (define tolerance 0.00001) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next ((average-damp f) guess))) (if (close-enough? guess next) guess (try next)))) (try first-guess)) מבוא מורחב
Computing sqrt and cube-roots. (define (sqrt x) (fixed-point-II (lambda (y) (/ x y)) 1)) For cube root we want fix point of x=a/x2. So, (define (cbrt x) (fixed-point (lambda (y) (/ x (square y))) 1)) מבוא מורחב
Newton’s method A solution to g(x) = 0 is a fixed point of f(x) = x - g(x)/g’(x) (define (newton-transform g) (lambda (x) (- x (/ (g x) ((deriv g) x))))) (define (newton-method g guess) (fixed-point (newton-transform g) guess)) (define (sqrt x) (newton-method (lambda (y) (- (square y) x)) 1.0)) > (sqrt 2) 1.4141957539304906 מבוא מורחב
composef (define composef (lambda (f g) (lambda (x) (f (g x))))) composef: (A B), (C A) (C B) composef: (STRING INT), (INT STRING) ( INT INT) composef: ( INT INT), (BOOL INT) ( BOOL INT) מבוא מורחב
Chapter 2 – Building abstractions with data מבוא מורחב
Export only what is needed. Procedural abstraction • Publish: name, number and type of arguments • type of answer • Guarantee: the procedure behavior • Hide: local variables and procedures, • way of implementation, • internal details, etc. מבוא מורחב
Export only what is needed. Data abstraction • Publish: name, • constructor, • selectors, • ways of handling it • Guarantee: the behavior • Hide: local variables and procedures, • way of implementation, • internal details, etc. מבוא מורחב
Guarantee:(numer (make-rat a b)) a (denom (make-rat a b)) b a,b have no common divisor. = An example: Rational numbers We would like to represent rational numbers. A rational number is a quotient a/b of two integers. Constructor: (make-rat a b) Selectors: (numer r) (denom r) מבוא מורחב
Public Methods (add-rat x y) (sub-rat x y) (mul-rat x y) (div-rat x y) (print-rat x) (equal-rat? x y) Methods should come with a guarantee of their actions: (equal-rat? (make-rat 2 4) (make-rat 5 10)) מבוא מורחב
Implementing methods with the constructor and selectors. (define (mul-rat x y) (make-rat (* (numer x) (numer y)) (* (denom x) (denom y)))) (define (sub-rat x y) … (define (add-rat x y)… (define (div-rat x y) (make-rat (* (numer x) (denom y)) (* (denom x) (numer y)))) (define (equal-rat? x y) (and (= (numer x) (numer y)) (= (denom x) (denom y)))) מבוא מורחב
Pair: A primitive data type. Constructor: (cons a b) Selectors: (car p) (cdr p) Guarantee:(car (cons a b)) = a (cdr (cons a b)) = b Abstraction barrier: We say nothing about the representation or implementation of pairs. מבוא מורחב
Implementing make-rat, numer, denom (define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x)) מבוא מורחב
Reducing to lowest terms • In our current implementation we keep 10000/20000 • As such and not as ½. • This: • Makes the computation more expensive. • Prints out clumsy results. A solution: change the constructor (define (make-rat a b) (let ((g (gcd a b))) (cons (/ a g) (/ b g)))) Note that we do not need to change our program anywhere else. מבוא מורחב
Abstraction barriers Programs that use rational numbers add-rat sub-rat ……. make-rat numer denom car cdr cons מבוא מורחב
Abstraction Violation Alternative implementation (define (add-rat x y) (cons (+ (* (car x) (cdr y)) (* (car y) (cdr x))) (* (cdr x) (cdr y)))) If we bypass an abstraction barrier, Changes to one level may affect many levels above it. Maintenance becomes more difficult. מבוא מורחב
3 2 1 Compound data A closure property: The result obtained by creating a compound data structure can itself be treated as a primitive object and thus be input to the creation of another compound object. • Pairs have the closure property: • We can pair pairs, pairs of pairs etc. • (cons (cons 1 2) 3) מבוא מורחב
4 3 2 1 Box and pointer diagram • (cons (cons 1 (cons 2 3)) 4) מבוא מורחב
1 2 3 Lists • (cons 1 (cons 3 (cons 2 nil))) • Syntactic sugar:(list 1 3 2) מבוא מורחב
… <x1> <x2> <xn> Lists (list <x1> <x2> ... <xn>) Same as (cons <x1> (cons <x2> ( … (cons <xn> nil)))) מבוא מורחב
And so, (cons 3 (list 1 2)) is the same as (cons 3 (cons 1 (cons 2 nil))) which is (3 1 2) (cdr (list 1 2 3)) is (cdr (cons 1 (cons 2 (cons 3 nil)))) which is (cons 2 (cons 3 nil)) which is (list 2 3) And And (car (list 1 2 3)) is (car (cons 1 (cons 2 (cons 3 nil)))) which is 1 מבוא מורחב