130 likes | 150 Views
Explore various methods of implementing complex number data, maintaining abstraction principles, with examples using real and imaginary parts, magnitudes, and angles. Discover how to hide internal details and represent data in rectangular and polar forms while ensuring consistency.
E N D
데이타를 여러방식으로 구현할 때Multiple Representations • 데이터 구현방식마다 꼬리표를 붙이고. • 데이터 속내용 감추기 원리를 유지하면서. • 예를 들어 complex number data 구현방안들을 생각해 보자 • complex number 만들기 • make-from-real-imag: real * real -> complex • make-from-mag-angle: real * real -> complex • complex number 사용하기 • is-complex?: -> bool • real: complex -> real • imag: complex -> real • mag: complex -> real • angle: complex -> real
add-complex mul-complex make-from-real-imag make-from-mag-angle real img mag angle is-complex? rectangular representation
(define (make-from-real-imag x y) (cons x y)) (define (make-from-mag-angle m a) (cons (* m (cos a)) (* m (sin a)))) (define (is-complex? c) (and (pair? c) (real? (car c)) (real? (cdr c))) (define (real c) (cond ((not (is-complex? c)) (error)) (else (car c)))) (define (imag c) (cond ((not (is-complex? c) (error)) (else (cdr c)))) (define (mag c) (cond ((not (is-complex? c) (error)) (else (sqrt (+ (square (real c)) (square (imag c))))) )) (define (angle c) (cond ((not (is-complex? c) (error)) (else (atan (imag c) (real c)))))
외부에서는, (define (add-complex c1 c2) (make-from-real-imag (+ (real c1) (real c2)) (+ (imag c1) (imag c2)) )) (define (mul-complex c1 c2) (make-from-mag-angle (* (mag c1) (mag c2)) (+ (angle c1) (angle c2)) ))
add-complex mul-complex make-from-real-imag make-from-mag-angle real imag mag angle is-complex? make… make … rectangular representation polar representation
rectagular complex 속내용 감추기data abstraction • make-from-real-imag-rectangular: real * real -> complex • make-from-mag-angle-rectangular: real * real -> complex • is-rectangular?: complex -> bool • real-rectangular: complex -> real • imag-rectangular: complex -> real • mag-rectangular: complex -> real • angle-rectangular: complex -> real (define rectangular-tag ‘rectangular) (define (make-from-real-imag-rectangular x y) (cons rectangular-tag (cons x y))) (define (make-from-mag-angle-rectangular m a) (cons rectangular-tag (cons (* m (cos a)) (* m (sin a))))) (define (is-rectangular? c) (equal? (car c) rectangular-tag)) (define (real-rectangular c) (car (cdr c))) (define (imag-rectangular c) (cdr (cdr c))) (define (mag-rectangular c) (sqrt (+ (square (real-rectangular c))) (+ (square (imag-rectangular c))))) (define (angle-rectangular c) (atan (imag-rectangular c) (real-rectangular c)))
polar complex 속내용 감추기data abstraction • make-from-real-imag-polar: real * real -> complex • make-from-mag-angle-polar: real * real -> complex • is-polar?: complex -> bool • real-polar: complex -> real • imag-polar: complex -> real • mag-polar: complex -> real • angle-polar: complex -> real (define polar-tag ‘polar) (define (make-from-real-imag-polar x y) (cons polar-tag (cons …))) (define (make-from-mag-angle-polar m a) (cons polar-tag (cons m a))) (define (is-polar? c) (equal? (car c) polar-tag)) (define (mag-polar c) (car (cdr c))) (define (angle-polar c) (cdr (cdr c))) (define (real-polar c) (* (mag-polar c) (cos (angle-polar c)))) (define (imag-polarr c) (* (mag-polar c) (sin (angle-polar c))))
(define (make-from-real-imag x y) (make-from-real-imag-rectangular x y)) (define (make-from-mag-angle m a) (make-from-mag-angle-polar m a)) (define (is-complex? c) (and (pair? c) (cond ((is-rectangular? c) (and (real? (real-rectangular c)) (real? (imag-rectangular c)))) ((is-polar? c) (and (real? (mag-polar c)) (real? (angle-polar c)))) (else #f)) ))
(define (real c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (real-rectangular c)) ((is-polar? c) (real-polar c)))) (define (imag c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (imag-rectangular c)) ((is-polar? c) (imag-polar c)))) (define (mag c) (cond ((not (is-complex? c)) (error)) ((is-rectangllar? c) (mag-rectangular c)) ((is-polar? c) (mag-polar c)))) (define (angle c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (angle-rectangular c)) ((is-polar? c) (angle-polar c))))
외부에서는, 변함없이 (define (add-complex c1 c2) (make-from-real-imag (+ (real c1) (real c2)) (+ (imag c1) (imag c2)) )) (define (mul-complex c1 c2) (make-from-mag-angle (* (mag c1) (mag c2)) (+ (angle c1) (angle c2)) ))
데이타를 여러방식으로 구현할 때Multiple Representations • 데이터 구현방식마다 꼬리표를 붙이고. • 데이터 속내용 감추기 원리를 유지하면서. add-complex mul-complex make-from-real-imag make-from-mag-angle real imag mag angle is-complex? make … make… rectangular representation polar representation
또다른 complex number 구현방식을 품고 있게 하려면? add-complex mul-complex make-from-real-imag make-from-mag-angle real imag mag angle is-complex? make … make… make… polar representation xyz representation rectangular representation 어디를 어떻게 바꾸어야 할까? 적어도real, imag, mag, angle, is-complex?를 확장해야.
(define (real c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (real-rectangular c)) ((is-polar? c) (real-polar c)) ((is-xyz? c) (real-xyz c)) )) (define (imag c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (imag-rectangular c)) ((is-polar? c) (imag-polar c)) ((is-xyz? c) (imag-xyz c)) )) (define (mag c) (cond ((not (is-complex? c)) (error)) ((is-rectangllar? c) (mag-rectangular c)) ((is-polar? c) (mag-polar c)) ((is-xyz? c) (mag-xyz c)) )) (define (angle c) (cond ((not (is-complex? c)) (error)) ((is-rectangular? c) (angle-rectangular c)) ((is-polar? c) (angle-polar c)) ((is-xyz? c) (angle-xyz c)) ))