מבוא מורחב 1 multiple representations of data. מבוא מורחב 2 complex numbers...

30
בבבב בבבבב1 Multiple representations of data

Upload: darren-ferguson

Post on 19-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב1

Multiple representations of data

Page 2: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב2

Complex numbers

imaginary

real

3

23 + 2i

13atan(2/3)

Page 3: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב3

Complex numbers arithmetic

a + bi + (c + di) = a+c + (b+d)i

r1e(i1) * r2e(i 2) = (r1r2)e(i(1+ 2))

Page 4: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב4

High level usage

(define (add-complex z1 z2) (make-from-real-imag (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2))))

(define (sub-complex z1 z2) (make-from-real-imag (- (real-part z1) (real-part z2)) (- (imag-part z1) (imag-part z2))))

(define (mul-complex z1 z2) (make-from-mag-ang (* (magnitude z1) (magnitude z2)) (+ (angle z1) (angle z2))))

(define (div-complex z1 z2) (make-from-mag-ang (/ (magnitude z1) (magnitude z2)) (- (angle z1) (angle z2))))

Page 5: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב5

המימוש שמדמיין אריאל

(define (real-part z) (car z))

(define (imag-part z) (cdr z))

(define (make-from-real-imag x y) (cons x y))

(define (magnitude z) (sqrt (+ (square (real-part z)) (square (imag-part z)))))

(define (angle z) (atan (imag-part z) (real-part z)))

(define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a))))

Page 6: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב6

המימוש מהזווית של מני

(define (make-from-mag-ang r a) (cons r a))

(define (magnitude z) (car z))

(define (angle z) (cdr z))

(define (real-part z) (* (magnitude z) (cos (angle z))))

(define (imag-part z) (* (magnitude z) (sin (angle z))))

(define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x)))

Page 7: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב7

How do we have them both ?

('rectangular 3 2)

('polar (sqrt 13) (atan 2 3))

We tag the data:

Page 8: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב8

How do we have them both ?

(define (attach-tag type-tag contents) (cons type-tag contents))

(define (type-tag datum) (if (pair? datum) (car datum) (error "Bad tagged datum -- TYPE-TAG" datum)))

(define (contents datum) (if (pair? datum) (cdr datum) (error "Bad tagged datum -- CONTENTS" datum)))

We tag the data:

Page 9: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב9

Complex numbers: having both reps.

(define (rectangular? z) (eq? (type-tag z) 'rectangular))

(define (polar? z) (eq? (type-tag z) 'polar))

(define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) (else (error "Unknown type -- REAL-PART" z))))

Page 10: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב10

Complex numbers: having both reps.

(define (magnitude z) (cond ((rectangular? z) (magnitude-rectangular (contents z))) ((polar? z) (magnitude-polar (contents z))) (else (error "Unknown type -- MAGNITUDE" z))))

angle, imag-part are similar

Page 11: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב11

and we have to rename

(define (real-part-rectangular z) (car z))

(define (imag-part-rectangular z) (cdr z))

(define (magnitude-rectangular z) (sqrt (+ (square (real-part-rectangular z)) (square (imag-part-rectangular z)))))

(define (angle-rectangular z) (atan (imag-part-rectangular z) (real-part-rectangular z)))

(define (make-from-real-imag-rectangular x y) (attach-tag 'rectangular (cons x y)))

(define (make-from-mag-ang-rectangular r a) (attach-tag 'rectangular (cons (* r (cos a)) (* r (sin a)))))

Page 12: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב12

and we have to rename

(define (real-part-polar z) (* (magnitude-polar z) (cos (angle-polar z))))

(define (imag-part-polar z) (* (magnitude-polar z) (sin (angle-polar z))))

(define (magnitude-polar z) (car z))

(define (angle-polar z) (cdr z))

(define (make-from-real-imag-polar x y) (attach-tag 'polar (cons (sqrt (+ (square x) (square y))) (atan y x))))

(define (make-from-mag-ang-polar r a) (attach-tag 'polar (cons r a)))

Page 13: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב13

Constructors

(define (make-from-real-imag x y) (make-from-real-imag-rectangular x y))

(define (make-from-mag-ang r a) (make-from-mag-ang-polar r a))

Page 14: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב14

add-complex, sub-complex

real-part imag-part magnitude angle

polarrepresentation

Rectangularrepresentation

Page 15: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב15

Difficulties ?

• The generic selectors must know about all reps. Bureacracy (define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z)))

(else (error "Unknown type -- REAL-PART" z))))

• Care about name clashes

==>

Implementation is not additive

• Care about when there is a tag and when there isn’t

Page 16: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב16

Data directed programming

work directly with the table:

types

oper

atio

ns

Polar Rectangular

real-part

imag-part

magnitude

angle

real-part-polar

imag-part-polar

magnitude-polar

angle-polar

real-part-rectangular

imag-part-rectangular

magnitude-rectangular

angle-rectangular

Page 17: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב17

Data-directed programming (Cont)

Assume we have

(put <op> <type> <item>)

(get <op> <type>)

will do them later in the course.

Page 18: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב18

Data-directed programming (Cont)

Assume we have

(put <op> <type> <item>)

(get <op> <type>)

will do them later in the course.

Page 19: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

19

Rectangular implementation(define (install-rectangular-package) ;; internal procedures (define (real-part z) (car z)) (define (imag-part z) (cdr z)) ... (define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a))))

;; interface to the rest of the system (define (tag x) (attach-tag 'rectangular x)) (put 'real-part '(rectangular) real-part) (put 'imag-part '(rectangular) imag-part) (put 'magnitude '(rectangular) magnitude) (put 'angle '(rectangular) angle) (put 'make-from-real-imag 'rectangular (lambda (x y) (tag (make-from-real-imag x y)))) (put 'make-from-mag-ang 'rectangular (lambda (r a) (tag (make-from-mag-ang r a)))) 'done)

Page 20: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

20

Polar implementation(define (install-polar-package) ;; internal procedures (define (magnitude z) (car z)) (define (angle z) (cdr z)) ... (define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x)))

;; interface to the rest of the system (define (tag x) (attach-tag 'polar x)) (put 'real-part '(polar) real-part) (put 'imag-part '(polar) imag-part) (put 'magnitude '(polar) magnitude) (put 'angle '(polar) angle) (put 'make-from-real-imag 'polar (lambda (x y) (tag (make-from-real-imag x y)))) (put 'make-from-mag-ang 'polar (lambda (r a) (tag (make-from-mag-ang r a)))) 'done)

Page 21: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב21

Generic selectors

(define (real-part z) (apply-generic 'real-part z))(define (imag-part z) (apply-generic 'imag-part z))(define (magnitude z) (apply-generic 'magnitude z))(define (angle z) (apply-generic 'angle z))

Page 22: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב22

apply-generic (simple version)

(define (simple-apply-generic op arg) (let ((type-tag (type-tag arg))) (let ((proc (get op (list type-tag)))) (if proc (proc (contents arg)) (error "No method for these types -- APPLY-GENERIC" (list op type-tag))))))

Page 23: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב23

apply-generic

(define (apply-generic op . args) (let ((type-tags (map type-tag args))) (let ((proc (get op type-tags))) (if proc (apply proc (map contents args)) (error "No method for these types -- APPLY-GENERIC" (list op type-tags))))))

(apply + (list 1 2 3 4)) ==> 10

Page 24: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב24

Finally

(define (make-from-real-imag x y) ((get 'make-from-real-imag 'rectangular) x y))

(define (make-from-mag-ang r a) ((get 'make-from-mag-ang 'polar) r a))

Page 25: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב25

Alternative -- message passing style

Make “intelligent” data objects.decompose the table by column

types

oper

atio

ns

Polar Rectangular

real-part

imag-part

magnitude

angle

real-part-polar

imag-part-polar

magnitude-polar

angle-polar

real-part-rectangular

imag-part-rectangular

magnitude-rectangular

angle-rectangular

Page 26: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב26

Message passing style

(define (make-from-real-imag x y) (define (dispatch op) (cond ((eq? op 'real-part) x) ((eq? op 'imag-part) y) ((eq? op 'magnitude) (sqrt (+ (square x) (square y)))) ((eq? op 'angle) (atan y x)) (else (error "Unknown op -- MAKE-FROM-REAL-IMAG" op)))) dispatch)

(define (apply-generic op arg) (arg op))

Page 27: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב27

Midterm last spring

( initשמקבלת כקלט אינדקס התחלתי ( add-ind init lstממש/י שגרה הראשונים lstורשימה האיברים זוגות. הם שאיבריה רשימה ומחזירה

ב- שמתחילים רצים אינדקסים הם בזוגות . initבזוגות השניים האיברים לדוגמא:. lstהם איברי הרשימה

(add-ind 0 (list 5 2 7)) ==> ((0 . 5) (1 . 2) (2 . 7))

(define (add-ind init lst) (if (null? lst) nil (cons (cons init (car lst)) (add-ind (+ 1 init) (cdr lst)))))

Page 28: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב28

Midterm last spring (cont)

( השגרה מימוש את רשימה ( max-ind lst-indהשלם/י שמקבלת על רשימת מספרים, ומחזירה את הזוג שבו add-indשהתקבלה מהפעלת

האיבר השני הוא הגדול ביותר. לדוגמא:

(max-ind (add-ind 0 (list 10 17 2 5))) ==> (1 . 17)

(define (max-ind lst-ind)

(accumulate ________________________________

____________

____________

))

lst-ind

(lambda (x y) (if (> (cdr x) (cdr y)) x y))

(car lst-ind)

Page 29: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב29

Midterm last spring (Cont)

) lstתהא ( break-list lstרשימת מספרים שכל איבריה שונים. הגדר שגרה היא שמוחזרת ברשימה הראשון האיבר שלוש. באורך רשימה שמחזירה

איברי של lstרשימת המירבי האיבר לפני השני . lstשמופיעים האיבר lstהאיבר השלישי הוא רשימת איברי . lstברשימה הוא האיבר המירבי ב-

שמופיעים לאחר האיבר המירבי. לדוגמא:

(break-list ‘(1 8 3 6 4 9 2 5 7)) ( (1 8 3 6 4) 9 (2 5 7) ) (break-list ‘(5 4 3 2 1)) ( () 5 (4 3 2 1) ) (break-list ‘(7)) ( () 7 () )

Page 30: מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

מבוא מורחב30

Midterm last spring (Cont)

(define (break-lst lst) (let ((lst-ind (add-ind lst))) (let ((maxi (max-ind lst-ind))) (list (map ______________________________ (filter _______________________________ lst-ind))

____________ (map ______________________________ (filter _______________________________ lst-ind)) ))))

(cdr maxi)

cdr

cdr

(lambda (x) (< (car x) (car maxi)))

(lambda (x) (> (car x) (car maxi)))