Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;複素数zがあるとして
- ;;直交座標形式:実数部を取得
- (real-part z)
- ;;直交座標形式:虚数部を取得
- (imag-part z)
- ;;直交座標形式で複素数を構成するコンストラクタ
- (make-from-real-imag (real-part z) (imag-part z))
- ;;同様に極座標形式のコンストラクタと選択子
- (make-from-mag-ang (magnitude z) (angle z))
- ;;それらを組み合わせて、抽象的な四則演算の手続きを実装する
- (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))))
- (trace add-complex)
- (trace sub-complex)
- (trace mul-complex)
- (trace div-complex)
- ;;;;;直交座標形式ベースの実装
- (define (square n)
- (* n n))
- (define (real-part z)
- (car z))
- (define (imag-part z)
- (cdr z))
- (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-real-imag x y)
- (cons x y))
- (define (make-from-mag-ang r a)
- (cons (* r (cos a))
- (* r (sin a))))
- ;;テスト
- (let ((z (make-from-real-imag 100 50)))
- (let ((z2 (make-from-mag-ang (magnitude z) (angle z))))
- (display z)
- (display z2)))
- ;;CALL magnitude (100 . 50)
- ;;RETN magnitude 111.80339887498948
- ;;CALL angle (100 . 50)
- ;;RETN angle 0.4636476090008061
- ;;CALL make-from-mag-ang 111.80339887498948 0.4636476090008061
- ;;RETN make-from-mag-ang (100.0 . 50.0)
- ;;z = (100 . 50)
- ;;z2 = (100.0 . 50.0)
- ;;四則演算をテスト
- (let ((z1 (make-from-real-imag 100 50))
- (z2 (make-from-real-imag 30 20)))
- (add-complex z1 z2)
- ;;CALL add-complex (100 . 50) (30 . 20)
- ;;RETN add-complex (130 . 70)
- (sub-complex z1 z2)
- ;;CALL sub-complex (100 . 50) (30 . 20)
- ;;RETN sub-complex (70 . 30)
- (mul-complex z1 z2)
- ;;CALL mul-complex (100 . 50) (30 . 20)
- ;;RETN mul-complex (2000.0000000000005 . 3499.999999999999)
- (div-complex z1 z2)
- ;;CALL div-complex (100 . 50) (30 . 20)
- ;;RETN div-complex (3.076923076923077 . -0.3846153846153845)
- )
- ;;;;;;;;;極座標形式ベースの実装
- (define (real-part z)
- (* (magnitude z) (cos (angle z))))
- (define (imag-part z)
- (* (magnitude z) (sin (angle z))))
- (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)))
- (define (make-from-mag-ang r a)
- (cons r a))
- ;;テスト
- (trace imag-part)
- (trace real-part)
- (let ((z1 (make-from-mag-ang 111.8 0.463)))
- (let ((z2 (make-from-real-imag (real-part z1) (imag-part z1))))
- (display z1)
- (display z2)))
- ;;CALL real-part (111.8 . 0.463)
- ;;RETN real-part 100.02931844795427
- ;;CALL imag-part (111.8 . 0.463)
- ;;RETN imag-part 49.93371056548626
- ;;z1 = (111.8 . 0.463)
- ;;z2 = (111.8 . 0.463)
- ;;四則演算をテスト
- (let ((z1 (make-from-real-imag 100 50))
- (z2 (make-from-real-imag 30 20)))
- (add-complex z1 z2)
- ;;CALL add-complex (111.80...98948 . 0.463...8061) (36.055...63989 . 0.58...5675)
- ;;RETN add-complex (147.648230602334 . 0.49394136891958124)
- (sub-complex z1 z2)
- ;;CALL sub-complex (111.80...98948 . 0.463...8061) (36.055...63989 . 0.58...5675)
- ;;RETN sub-complex (76.15773105863909 . 0.4048917862850835)
- (mul-complex z1 z2)
- ;;CALL mul-complex (111.80...98948 . 0.463...8061) (36.055...63989 . 0.58...5675)
- ;;RETN mul-complex (4031.1288741492745 . 1.0516502125483735)
- (div-complex z1 z2)
- ;;CALL div-complex (111.80...98948 . 0.463...8061) (36.055...63989 . 0.58...5675)
- ;;RETN div-complex (3.1008683647302115 . -0.12435499454676141)
- )
- ;;確認
- (real-part (make-from-mag-ang 147.648230602334 0.49394136891958124))
- ;;129.99999999999997
- (imag-part (make-from-mag-ang 147.648230602334 0.49394136891958124))
- ;;70.0
Add Comment
Please, Sign In to add comment