Download presentation
Presentation is loading. Please wait.
1
Lecture #6 section 1.3.3 pages 68-70 1.3.4 pages72-77
מבוא מורחב
2
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) מבוא מורחב
3
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) מבוא מורחב
4
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 ) (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)) Why does it work: One easy special case: f is monotone, x_0 is a fixed point, for x<x_0 f(x)>x and we start with x<x_0. We always get closer to the answer, we are always below the answer, and if we get close to something stable it has to be a fixed point.
5
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) = X5 = f(x4) = 1.6 X6 = f(x5) = 1.625 X7 = f(x6) = … Real answer: … Note how odd guesses underestimate And even guesses Overestimate. Here f monotone decreasing. F(x0)=x0, for x<x0, f(x)>f(x0)=x0. That’s why we get above and below the results. But: why do we get closer??? מבוא מורחב
6
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: … Again, its monotone decreasing. Above and below as before. We don’t get closer. Can we get further from the result? Does this mean we will absorbed into another result? מבוא מורחב
7
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 מבוא מורחב
8
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 = 2 G = 1 X/G = 2 G = ½ (1+ 2) = 1.5 X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = X/G = 24/17 G = ½ (17/ /17) = 577/408 = מבוא מורחב
9
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 מבוא מורחב
10
Fixed-point II (define (fixed-point-II f first-guess)
(define tolerance ) (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)) מבוא מורחב
11
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)) Why did we get to sqrt(2) and not –(sqrt(2)) How do we choose the starting point in general? מבוא מורחב
12
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) Is it a better method? Why do we use derivative. What we did was aimed at: Looking the black magic algorithm for sqrt from previous classes in a broader way. Understanding that is is a fixed-point method That, fixed-point methods re very general and very powerful That they can be used in different ways (e.g. Newton’s method) There is analysis behind the methods and this is only the beginning the tip of a tip of a whole subject (numerical analysis). מבוא מורחב
13
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) Here I want to emphasize that composeof can get functions f,g of many different types. Scheme is fine with it as long as The interpreter will be happy when he evaluates the term. We do not declare types anywhere. composef: ( INT INT), (BOOL INT) ( BOOL INT) מבוא מורחב
14
Chapter 2 – Building abstractions with data
So far we saw how to write procedures. Now we shift our focus into data structures. Very often we need to keep data in an organized way to faciliate handling of it. For example we might want to keep data about students including name, id, login, phone-number etc. We will an appropriate data structure to keep the data. Scheme has a primitive data type of a ‘pair’ We will see how to build more complex data types with it. מבוא מורחב
15
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. מבוא מורחב
16
Data abstraction Publish: name, constructor, selectors,
ways of handling it Guarantee: the behavior Hide: local variables and procedures, way of implementation, internal details, etc. This is what we want. Scheme doesn’t fully support that. Export only what is needed. מבוא מורחב
17
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) Guarantee: (numer (make-rat a b)) a (denom (make-rat a b)) b a,b have no common divisor. = Scheme knows how to work with rationals. This example is intended for demonstrating how to write a data type. We assume Integers we build rationals. We are going to build it using a primitive data type Scheme has which is pairs. For now on we will assume we have make-rat, numer, denom and see how to build the data type with them. מבוא מורחב
18
Public Methods Methods should come with a guarantee of their actions:
(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)) In equal-rat? We use the previous layer. We can make use of the fact that we keep things reduced to lowest terms. מבוא מורחב
19
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)))) We use the fact that we keep thing in lowest terms, which is a canonical form. (define (equal-rat? x y) (and (= (numer x) (numer y)) (= (denom x) (denom y)))) מבוא מורחב
20
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. מבוא מורחב
21
Implementing make-rat, numer, denom
(define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x)) מבוא מורחב
22
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. מבוא מורחב
23
Programs that use rational numbers
Abstraction barriers Programs that use rational numbers add-rat sub-rat ……. make-rat numer denom car cdr cons מבוא מורחב
24
Alternative implementation
Abstraction Violation (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. Consider what would have happened if we would like to change our implementation of rationals and keep denom as the first item and numer as the second. If we respect the abstraction barriers it’s very easy. If we don’t respect them (as with the example above) we need to make many changes, and we are never sure we are done. We actually can not do the change. Maintenance is much harder. מבוא מורחב
25
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) 3 2 1 Now that we have pairs, we will build with pairs many other things. We will first build lists. The reason we can use pairs for more complex data types is the closure property above. מבוא מורחב
26
Box and pointer diagram
(cons (cons 1 (cons 2 3)) 4) 4 1 3 2 מבוא מורחב
27
Lists (cons 1 (cons 3 (cons 2 nil))) Syntactic sugar: (list 1 3 2) 1 3
מבוא מורחב
28
… Lists (list <x1> <x2> ... <xn>) Same as
(cons <x1> (cons <x2> ( … (cons <xn> nil)))) <x1> <x2> <xn> … מבוא מורחב
29
And so, (cons 3 (list 1 2)) is the same as
(cons 3 (cons 1 (cons 2 nil))) which is (3 1 2) And (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 On the other hand (cons (list 1 2) (list 3 4)) is not (list ) (car (list 1 2 3)) is (car (cons 1 (cons 2 (cons 3 nil)))) which is 1 מבוא מורחב
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.