Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
(define (square x) | ||
(* x x)) | ||
|
||
;; Type tagging | ||
|
||
(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))) | ||
|
||
(define (rectangular? z) | ||
(eq? (type-tag z) 'rectangular)) | ||
(define (polar? z) | ||
(eq? (type-tag z) 'polar)) | ||
|
||
|
||
;; Ben representation | ||
|
||
(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 r i) | ||
(attach-tag 'rectangular (cons r i))) | ||
|
||
(define (make-from-mag-ang-rectangular r a) | ||
(attach-tag 'rectangular | ||
(cons (* r (cos a)) (* r (sin a))))) | ||
|
||
;; Alyssa representation | ||
|
||
(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))) | ||
|
||
;; Selectors | ||
|
||
(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)))) | ||
|
||
;; Interface | ||
|
||
(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)))) |