מבוא מורחב למדעי המחשב בשפת scheme

32
בבבב בבבבב בבבבב בבבבב בבבבScheme ללללל4

Upload: brody-wyatt

Post on 01-Jan-2016

43 views

Category:

Documents


0 download

DESCRIPTION

מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 4. Outline. High order procedures Finding Roots Compose Functions Accelerating Computations Fibonacci. Input : Continuous function f(x) a , b such that f(a)

TRANSCRIPT

Page 1: מבוא מורחב למדעי המחשב בשפת  Scheme

מבוא מורחב למדעי המחשב Schemeבשפת

4תרגול

Page 2: מבוא מורחב למדעי המחשב בשפת  Scheme

Outline

• High order procedures– Finding Roots– Compose Functions

• Accelerating Computations– Fibonacci

2

Page 3: מבוא מורחב למדעי המחשב בשפת  Scheme

Finding roots of equations

Input:

• Continuous function f(x)

• a, b such that f(a)<0<f(b)

Goal: Find a root of the equation f(x)=0

Relaxation: Settle with a “close-enough” solution

3

Page 4: מבוא מורחב למדעי המחשב בשפת  Scheme

General Binary Search• Search space: set of all potential solutions

– e.g. every real number in the interval [a b] can be a root

• Divide search space into halves– e.g. [a (a+b)/2) and [(a+b)/2 b]

• Identify which half has a solution – e.g. r is in [a (a+b)/2) or r is in [(a+b)/2 b]

• Find the solution recursively in reduced search space– [a (a+b)/2)

• Find solution for small search spaces– E.g. if abs(a-b)<e, r=(a+b)/2

4

Page 5: מבוא מורחב למדעי המחשב בשפת  Scheme

Back to finding roots

• Theorem: if f(a)<0<f(b) and f(x) is continuous, then there is a point c(a,b) such that f(c)=0– Note: if a>b then the interval is (b,a)

• Half interval method– Divide (a,b) into 2 halves– If interval is small enough – return middle point– Otherwise, use theorem to select correct half interval– Repeat iteratively

5

Page 6: מבוא מורחב למדעי המחשב בשפת  Scheme

Example

b

a

6

Page 7: מבוא מורחב למדעי המחשב בשפת  Scheme

Example (continued)

b

a

And again and again… 7

Page 8: מבוא מורחב למדעי המחשב בשפת  Scheme

(define (search f a b) (let ((x (average a b))) (if (close-enough? a b) (let ((y (f x))) (cond ((positive? y) ) ((negative? y) ) (else ))))))

x

(search f a x) (search f x b) x

Scheme implementation

8

Complexity?

Page 9: מבוא מורחב למדעי המחשב בשפת  Scheme

Determine positive and negative ends(define (half-interval-method f a b) (let ((fa (f a)) (fb (f b))) (cond ((and )

(search f a b)) ((and )

(search f b a)) (else

(display “values are not of opposite signs”))) ))

(negative? fa) (positive? fb)

(negative? fb) (positive? fa)

We need to define

(define (close-enough? x y)

(< (abs (- x y)) 0.001))

9

Page 10: מבוא מורחב למדעי המחשב בשפת  Scheme

sin(x)=0, x(2,4)(half-interval-method 2.0 4.0)

x3-2x-3=0, x(1,2)(half-interval-method

1.0 2.0)

Examples:

sin

3.14111328125…

(lambda (x) (- (* x x x) (* 2 x) 3))

1.89306640625

10

Page 11: מבוא מורחב למדעי המחשב בשפת  Scheme

Compose

• Compose f(x), g(x) to f(g(x))(define (compose f g) (lambda (x) (f (g x))))

(define (inc x) (+ x 1))((compose inc square) 3)10((compose square inc) 3)16

11

Page 12: מבוא מורחב למדעי המחשב בשפת  Scheme

(= n 1) f

(repeated f (- n 1))

f(x), f(f(x)), f(f(f(x))), …apply f, n times

(define (repeated f n) (if

(compose f )))

((repeated inc 5) 100) => 105((repeated square 2) 5) => 625

Repeated f

(define (compose f g)

(lambda (x) (f (g x)))) Compose now

Execute later

12

Page 13: מבוא מורחב למדעי המחשב בשפת  Scheme

(define (repeated f n)

(lambda (x) (repeated-iter f n x)))

Repeated f - iterative

(define (repeated-iter f n x) (if (= n 1)

(f x) (repeated-iter f (- n 1) (f x))))

Do nothing until called later

13

Page 14: מבוא מורחב למדעי המחשב בשפת  Scheme

Repeated f – Iterative II

(define (repeated f n)

(define (repeated-iter count accum)

(if (= count n)

accum

(repeated-iter (+ count 1)

(compose f accum))))

(repeated-iter 1 f))

Compose now

Execute later

14

Page 15: מבוא מורחב למדעי המחשב בשפת  Scheme

(define (smooth f) (let ((dx 0.1))

))

(define (average x y z) (/ (+ x y z) 3))

(lambda (x) (average (f (- x dx)) (f x)

(f (+ x dx))))

Smooth a function f:g(x) = (f(x – dx) + f(x) + f(x + dx)) / 3

((repeated smooth n) f)

Repeatedly smooth a function

(define (repeated-smooth f n) ) 15

Page 16: מבוא מורחב למדעי המחשב בשפת  Scheme

Normal Distribution

• The formula of the normal probability density function is based on two parameters: the mean (μ) and the standard deviation (σ). Its formula is:

2

0.5

2

1( )

2

y

f y e

16

Page 17: מבוא מורחב למדעי המחשב בשפת  Scheme

Normal Distribution2

0.5

2

1( )

2

y

f y e

17

Page 18: מבוא מורחב למדעי המחשב בשפת  Scheme

Normal Distribution

• The cumulative density function: ( ) ( )x

F x f y dy

18

Page 19: מבוא מורחב למדעי המחשב בשפת  Scheme

Standard Normal Distribution

• If μ=0 and σ=1 then we call it Standard Normal Distribution.

• The formula of the standard normal probability density function is:

• The cumulative density function is defined:

• Note: F(x)=Φ((x-μ)/σ)

20.5

( )2

yef y

( ) ( )x

x f y dy

19

Page 20: מבוא מורחב למדעי המחשב בשפת  Scheme

Standard Normal Distribution

• Let’s first recall two general functions:

; General sum procedure(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b))))

; General Integral function(define (integral f a b) (define dx 1.0e-4) (* (sum f a (lambda (x) (+ x dx)) b) dx))

20

Page 21: מבוא מורחב למדעי המחשב בשפת  Scheme

• Write a procedure std-normal that computes the standard normal cumulative function.

• Assume that Φ(x)=0 for x < -6, and Φ(x)=1 for x > 6.

(define (std-normal x) (let ((pi 3.14159) (e 2.71828) (sigma 6)) (define (phi x)

) (cond ((< x -sigma) ) ((> x sigma) ) (else )))

Standard Normal Distribution

(/ (expt e (* (- 0.5) (* x x))) (sqrt (* 2 pi)))

01

(integral phi (- sigma) x)

21

Page 22: מבוא מורחב למדעי המחשב בשפת  Scheme

• Write a procedure normalize which, given a function foo and two parameters a, b returns the function G such that G(x) = foo((x – a)/b). That is, you should have:

((normalize square 5 2) 1) ==> 4(define (normalize foo a b) (lambda (x) (if (= b 0) (display "Error in normalize: Divide by zero") )))

Standard Normal Distribution

(foo (/ (- x a) b))

22

Page 23: מבוא מורחב למדעי המחשב בשפת  Scheme

• Write a procedure normal that computes the normal cumulative function for any μ and σ.

• Remember: F(x)=Φ((x-μ)/σ)(define (normal x miu sigma)

)

Normal Distribution

((normalize std-normal miu sigma) x)

23

Page 24: מבוא מורחב למדעי המחשב בשפת  Scheme

AcceleratingComputations

24

Page 25: מבוא מורחב למדעי המחשב בשפת  Scheme

Iterative Fibonacci(define (fib n)

(define (fib-iter a b count)

(if (= count 0)

b

(fib-iter (+ a b) a (- count 1)))

(fib-iter 1 0 n))

• Computation time: (n)• Much better than Recursive implementation,

but…• Can we do better?

25

Page 26: מבוא מורחב למדעי המחשב בשפת  Scheme

Slow vs Fast Expt

• Slow (linear)– b0=1– bn=bbn-1

• Fast (logarithmic)– bn=(b2)n/2 if n is even– bn=bbn-1 if n is odd

• Can we do the same with Fibonacci?

26

Page 27: מבוא מורחב למדעי המחשב בשפת  Scheme

Double Steps

• Fibonacci Transformation:

ab

baaT

0 1 1 2 3 5 8 13 21

b a a+b 2a+b 3a+2b …

• Double Transformation:

bab

baaT

22

27

Page 28: מבוא מורחב למדעי המחשב בשפת  Scheme

A Squaring Algorithm

• If we can square (or multiply) linear transformations, we have an algorithm:– Apply Tn on (a,b), where:– Tn=(T2)n/2 If n is even– Tn=TTn-1 If n is odd

28

Page 29: מבוא מורחב למדעי המחשב בשפת  Scheme

Squaring Transformations

• General Linear Transformation:

wbzab

ybxaaT wzyx ,,,

• Squared:

22

2

2

2,,,

wyzzwxzywxyyzx

wzyx

T

bwyz

azwxz

wbzaw

ybxazb

bywxy

ayzx

wbzay

ybxaxa

T

29

Page 30: מבוא מורחב למדעי המחשב בשפת  Scheme

Iterative Algorithm

• Initialize:

ab

baaTncountba 01

• Stop condition: If count=0 return b

• Step

2/1

,, 2

countcountcountcount

TTbaTba

count is odd count is even

30

Page 31: מבוא מורחב למדעי המחשב בשפת  Scheme

Representing Transformations

• We need to remember x, y, z, w

• Fibonacci Transformations belong to a simpler family:

aqbpb

apaqbqaTpq

• T01 is the basic Fibonacci transformation

• Squaring (verify on your own!):

222 2

2

qpqqppq TT

31

Page 32: מבוא מורחב למדעי המחשב בשפת  Scheme

Implementation (finally)(define fib n)

(fib-iter 1 0 0 1 n))

(define (fib-iter a b p q count)

(cond ((= count 0) b)

((even? count)

(fib-iter a

b

(/ count 2)

(else (fib-iter

p

q

(- count 1))))

(+ (square p) (square q))

(+ (* 2 p q) (square q))

(+ (* b q) (* a q) (* a p))

(+ (* b p) (* a q))

32