lecture6-tau

Download Report

Transcript lecture6-tau

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
‫מבוא מורחב‬
1
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)
‫מבוא מורחב‬
2
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
‫מבוא מורחב‬
3
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))
4
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…
Note how odd guesses
underestimate
And even guesses
Overestimate.
Real answer: 1.6180339…
‫מבוא מורחב‬
5
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…
‫מבוא מורחב‬
6
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
‫מבוא מורחב‬
7
Example :
x for 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=2
G=1
X/G = 2
X/G = 4/3
G = ½ (1+ 2) = 1.5
G = ½ (3/2 + 4/3) = 17/12 = 1.416666
X/G = 24/17 G = ½ (17/12 + 24/17) = 577/408 = 1.4142156
‫מבוא מורחב‬
8
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
‫מבוא מורחב‬
9
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))
‫מבוא מורחב‬
10
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))
‫מבוא מורחב‬
11
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
‫מבוא מורחב‬
12
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)
‫מבוא מורחב‬
13
Chapter 2 – Building abstractions with data
‫מבוא מורחב‬
14
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.
‫מבוא מורחב‬
15
Data abstraction
• Publish:
name,
constructor,
selectors,
ways of handling it
• Guarantee: the behavior
• Hide:
local variables and procedures,
way of implementation,
internal details, etc.
Export only what is needed.
‫מבוא מורחב‬
16
An example: Rational numbers
We would like to represent rational numbers.
A rational number is a quotient a/b of two integers.
Constructor:
Selectors:
(make-rat a b)
(numer r)
(denom r)
Guarantee:
(numer (make-rat a b)) = a
(denom (make-rat a b)) = b
‫מבוא מורחב‬
17
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))
(equal-rat? x y) iff
x and y have the same numerator and denominator
(equal-rat? x y) iff
x and y have the same value
‫מבוא מורחב‬
18
Implementing methods with the constructor
and selectors.
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y) …
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(define (div-rat x y)
(make-rat (* (numer x) (denom y))
(* (denom x) (numer y))))
(define (equal-rat? x y)
(= (* (numer x) (denom y)) (* (numer y) (denom x))))
‫מבוא מורחב‬
19
Pair: A primitive data type.
Constructor:
Selectors:
(cons a b)
(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.
‫מבוא מורחב‬
20
Implementing make-rat, numer, denom
(define (make-rat n d) (cons n d))
(define (numer x) (car x))
(define (denom x) (cdr x))
‫מבוא מורחב‬
21
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.
‫מבוא מורחב‬
22
Abstraction barriers
Programs that use rational numbers
add-rat sub-rat …….
make-rat numer denom
car cdr cons
‫מבוא מורחב‬
23
Alternative implementation
(define (add-rat x y)
(cons (+ (* (car x) (cdr y))
(* (car y) (cdr x)))
(* (cdr x) (cdr y))))
Abstraction
Violation
If we bypass an abstraction barrier,
Changes to one level may affect many levels above it.
Maintenance becomes more difficult.
‫מבוא מורחב‬
24
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.
3
Pairs have the closure property:
We can pair pairs, pairs of pairs etc.
2
(cons (cons 1 2) 3)
1
‫מבוא מורחב‬
25
Box and pointer diagram
(cons (cons 1 (cons 2 3)) 4)
4
3
1
2
‫מבוא מורחב‬
26
Lists
(cons 1 (cons 3 (cons 2 nil)))
1
3
2
Syntactic sugar: (list 1 2 3)
‫מבוא מורחב‬
27
Lists
(list <x1> <x2> ... <xn>)
Same as
(cons <x1> (cons <x2> ( … (cons <xn> nil))))
…
<x1>
<x2>
<xn>
‫מבוא מורחב‬
28
And so,
(cons 3 (list 1 2))
(cons 3 (cons 1 (cons 2 nil)))
(3 1 2)
is the same as
which is
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)
‫מבוא מורחב‬
29