Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complex Number Arithmetic

Similar presentations


Presentation on theme: "Complex Number Arithmetic"— Presentation transcript:

1 Complex Number Arithmetic
Two natural representations: • rectangular: (real,imag) • polar: (norm,arg) z = (z) + i ·(z) = |z| ·e i ·arg(z)

2 imag z |z| (z) arg(z) (z) real z = (z) + i ·(z) = |z| ·e i ·arg(z)

3 Abstract Types (define-class <complex> (<number>))
(define-class <rect> (<complex>) (real <float>) (imag <float>)) (define-class <polar> (<complex>) (norm <float>) (arg <float>)) <object> <number> <integer> <float> <rat> <complex> <rect> <polar>

4 (define (make-polar <function>)
(method ((n <number>) (a <number>)) (make <polar> norm: (as <float> n) arg: (as <float> a)))) (define (make-rect <function>) (method ((r <number>) (i <number>)) (make <rect> real: (as <float> r) imag: (as <float> i))))

5 <rect> <polar>
real slot convert reference imag slot convert norm convert slot arg convert slot

6 (define-generic-function real (x))
(add-method real (method ((x <rect>)) (get-slot real: x))) (define-generic-function imag (x)) (add-method imag (get-slot imag: x)))

7 Dylan does this for you! (define-generic-function real (x))
(add-method real (method ((x <rect>)) (get-slot real: x))) (define-generic-function imag (x)) (add-method imag (get-slot imag: x))) Dylan does this for you!

8 (add-method real (method ((z <polar>)) (* (norm z) (cos (arg z))))) (add-method imag (* (norm z) (sin (arg z))))) (add-method norm (method ((z <rect>)) (sqrt (+ (square (real z)) (square (imag z)))))) (add-method arg (arctan (/ (imag z) (real z)))))

9 (define (mul-complex <function>)
(method ((x <complex>) (y <complex>)) (make-polar (* (norm x) (norm y)) (+ (arg x) (arg y))))) (define (add-complex <function>) (make-rect (+ (real x) (real y)) (+ (imag x) (imag y)))))

10 or better... (define-generic-function add (x y)) (add-method add
(method ((x <number>) (y <number>)) (+ x y))) (add-method add add-rat) (add-method add add-complex) or better... (add-method binary+ add-rat) (add-method binary+ add-complex)

11 Coercions — the generic function as
(as <float> 1) => 1.0 (as <character> 100) => d <integer>  <rat>  <float>  <complex> (add-method as (method ((t (singleton <rat>)) (a <integer>)) (make-rat a 1))) (add-method binary+ (method ((a <integer>) (b <rat>)) (+ (as <rat> a) b))) (method ((a <rat>) (b <integer>)) (+ a (as <rat> b))))

12 Polynomial Arithmetic
(define-class <term> (<object>) (degree <integer>) (coeff <object>)) (define (add-term <function>) (method ((t1 <term>) (t2 <term>)) (make <term> degree: (degree t1) coeff: (+ (coeff t1) (coeff t2)))))

13 (define <termlist> <list>)
(define (first-term <function>) (method ((t <termlist>)) (head t))) (define (rest-terms <function>) (method ((t <termlist>)) (tail t))) (define empty-termlist? null?)

14 (define (add-terms <function>)
(method ((l1 <termlist>) (l2 <termlist>)) (cond ((empty-termlist? l1) l2) ((empty-termlist? l2) l1) (else: (bind (((t1 <term>) (first-term l1)) ((t2 <term>) (first-term l2)) ((r1 <termlist>) (rest-terms l1)) ((r2 <termlist>) (rest-terms l2))) (cond ((< (degree t1) (degree t2)) (adjoin-term t1 (add-terms r1 l2))) ((< (degree t2) (degree t1)) (adjoin-term t2 (add-terms l1 r2))) (adjoin-term (add-term t1 t2) (add-terms r1 r2)))))))))

15 (define-class <poly> (<object>)
(poly-variable <symbol>) (poly-terms <termlist>)) (define (same-variable? <function>) (method ((p1 <poly>) (p2 <poly>)) (= (poly-variable p1) (poly-variable p2)))) (define (add-poly <function>) (if (same-variable? p1 p2) (make <poly> poly-variable: (poly-variable p1) poly-terms: (add-terms (poly-terms p1) (poly-terms p2))) (error "Polynomials not in same variable")))) (add-method binary+ add-poly)


Download ppt "Complex Number Arithmetic"

Similar presentations


Ads by Google