1 / 28

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

מבוא מורחב למדעי המחשב בשפת Scheme. תרגול 3. Outline. High order procedures Finding Roots Compose Functions Accelerating Computations Fibonacci. Input : Continuous function f(x) a , b such that f(a)<0<f(b) Goal : Find a root of the equation f(x)=0

lilike
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3

  2. Outline • High order procedures • Finding Roots • Compose Functions • Accelerating Computations • Fibonacci

  3. 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 Finding roots of equations

  4. 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

  5. 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

  6. Example a b

  7. Example (continued) a b And again and again…

  8. Scheme implementation x (search f a x) (search f x b) x (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 )))))) Complexity?

  9. We need to define (define (close-enough? x y) (< (abs (- x y)) 0.001)) 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)

  10. Examples: 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) sin 3.14111328125… (lambda (x) (- (* x x x) (* 2 x) 3)) 1.89306640625

  11. (define (double f) (lambda (x) (f (f x)))) (define (inc x) (+ x 1)) Double ((double inc) 5) => 7 inc(x) = x + 1 f(f(x)) = x + 2 ((double square) 5) => 625 square(x) = x^2 f(f(x)) = x^4 11

  12. (((double double) inc) 5) => x +4 9 (((double double) square) 5) => x16 => 152587890625= 516 Double Double 12

  13. inc = x + 1 (double (double(double inc))) = x + 8 ((double (double double)) inc) = x + 16 (((double (double double)) inc) 5) 21 Double Double Double 13

  14. Double Double Double • Let D = double • ((double (double double)) = D(D(D))= D(D◦D)=(D◦D)◦(D◦D)= D◦D◦D◦D=D4 • D4 (f)=D3 (f2)= D2 (f4)=D(f8)=f8◦f8=f16

  15. 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 15

  16. f(x), f(f(x)), f(f(f(x))), … apply f, n times Repeated f Compose now Execute later (define (compose f g) (lambda (x) (f (g x)))) (= n 1) f (repeated f (- n 1)) (define (repeated f n) (if (compose f ))) ((repeated inc 5) 100) => 105 ((repeated square 2) 5) => 625 16

  17. Repeated f - iterative Do nothing until called later (define (repeated-iter f n x) (if (= n 1) (f x) (repeated-iter f (- n 1) (f x)))) (define (repeated f n) (lambda (x) (repeated-iter f n x))) 17

  18. 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 18

  19. Smooth a function f: g(x) = (f(x – dx) + f(x) + f(x + dx)) / 3 Repeatedly smooth a function (define (repeated-smooth f n) ) (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)))) ((repeated smooth n) f) 19

  20. AcceleratingComputations

  21. 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?

  22. Slow vs Fast Expt • Slow (linear) • b0=1 • bn=bbn-1 • Fast (logarithmic) • bn=(b2)n/2if n is even • bn=bbn-1if n is odd • Can we do the same with Fibonacci?

  23. b a a+b 2a+b 3a+2b … Double Steps • Fibonacci Transformation: • 0 1 1 2 3 5 8 13 21 • Double Transformation:

  24. 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=TTn-1 If n is odd

  25. Squaring Transformations • General Linear Transformation: • Squared:

  26. Iterative Algorithm • Initialize: • Stop condition: If count=0 return b • Step count is odd count is even

  27. Representing Transformations • We need to remember x, y, z, w • Fibonacci Transformations belong to a simpler family: • T01 is the basic Fibonacci transformation • Squaring (verify on your own!):

  28. 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))

More Related