Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. ;;複素数zがあるとして
  2.  
  3. ;;直交座標形式:実数部を取得
  4. (real-part z)
  5. ;;直交座標形式:虚数部を取得
  6. (imag-part z)
  7.  
  8. ;;直交座標形式で複素数を構成するコンストラクタ
  9. (make-from-real-imag (real-part z) (imag-part z))
  10.  
  11. ;;同様に極座標形式のコンストラクタと選択子
  12. (make-from-mag-ang (magnitude z) (angle z))
  13.  
  14. ;;それらを組み合わせて、抽象的な四則演算の手続きを実装する
  15. (define (add-complex z1 z2)
  16. (make-from-real-imag (+ (real-part z1) (real-part z2))
  17. (+ (imag-part z1) (imag-part z2))))
  18.  
  19. (define (sub-complex z1 z2)
  20. (make-from-real-imag (- (real-part z1) (real-part z2))
  21. (- (imag-part z1) (imag-part z2))))
  22.  
  23. (define (mul-complex z1 z2)
  24. (make-from-mag-ang (* (magnitude z1) (magnitude z2))
  25. (+ (angle z1) (angle z2))))
  26.  
  27. (define (div-complex z1 z2)
  28. (make-from-mag-ang (/ (magnitude z1) (magnitude z2))
  29. (- (angle z1) (angle z2))))
  30.  
  31. (trace add-complex)
  32. (trace sub-complex)
  33. (trace mul-complex)
  34. (trace div-complex)
  35.  
  36. ;;;;;直交座標形式ベースの実装
  37. (define (square n)
  38. (* n n))
  39.  
  40. (define (real-part z)
  41. (car z))
  42.  
  43. (define (imag-part z)
  44. (cdr z))
  45.  
  46. (define (magnitude z)
  47. (sqrt (+ (square (real-part z))
  48. (square (imag-part z)))))
  49.  
  50. (define (angle z)
  51. (atan (imag-part z) (real-part z)))
  52.  
  53. (define (make-from-real-imag x y)
  54. (cons x y))
  55.  
  56. (define (make-from-mag-ang r a)
  57. (cons (* r (cos a))
  58. (* r (sin a))))
  59.  
  60. ;;テスト
  61. (let ((z (make-from-real-imag 100 50)))
  62. (let ((z2 (make-from-mag-ang (magnitude z) (angle z))))
  63. (display z)
  64. (display z2)))
  65.  
  66. ;;CALL magnitude (100 . 50)
  67. ;;RETN magnitude 111.80339887498948
  68. ;;CALL angle (100 . 50)
  69. ;;RETN angle 0.4636476090008061
  70. ;;CALL make-from-mag-ang 111.80339887498948 0.4636476090008061
  71. ;;RETN make-from-mag-ang (100.0 . 50.0)
  72. ;;z = (100 . 50)
  73. ;;z2 = (100.0 . 50.0)
  74.  
  75. ;;四則演算をテスト
  76. (let ((z1 (make-from-real-imag 100 50))
  77. (z2 (make-from-real-imag 30 20)))
  78. (add-complex z1 z2)
  79. ;;CALL add-complex (100 . 50) (30 . 20)
  80. ;;RETN add-complex (130 . 70)
  81. (sub-complex z1 z2)
  82. ;;CALL sub-complex (100 . 50) (30 . 20)
  83. ;;RETN sub-complex (70 . 30)
  84. (mul-complex z1 z2)
  85. ;;CALL mul-complex (100 . 50) (30 . 20)
  86. ;;RETN mul-complex (2000.0000000000005 . 3499.999999999999)
  87. (div-complex z1 z2)
  88. ;;CALL div-complex (100 . 50) (30 . 20)
  89. ;;RETN div-complex (3.076923076923077 . -0.3846153846153845)
  90. )
  91.  
  92. ;;;;;;;;;極座標形式ベースの実装
  93. (define (real-part z)
  94. (* (magnitude z) (cos (angle z))))
  95.  
  96. (define (imag-part z)
  97. (* (magnitude z) (sin (angle z))))
  98.  
  99. (define (magnitude z)
  100. (car z))
  101.  
  102. (define (angle z)
  103. (cdr z))
  104.  
  105. (define (make-from-real-imag x y)
  106. (cons (sqrt (+ (square x) (square y)))
  107. (atan y x)))
  108.  
  109. (define (make-from-mag-ang r a)
  110. (cons r a))
  111.  
  112. ;;テスト
  113. (trace imag-part)
  114. (trace real-part)
  115. (let ((z1 (make-from-mag-ang 111.8 0.463)))
  116. (let ((z2 (make-from-real-imag (real-part z1) (imag-part z1))))
  117. (display z1)
  118. (display z2)))
  119.  
  120. ;;CALL real-part (111.8 . 0.463)
  121. ;;RETN real-part 100.02931844795427
  122. ;;CALL imag-part (111.8 . 0.463)
  123. ;;RETN imag-part 49.93371056548626
  124. ;;z1 = (111.8 . 0.463)
  125. ;;z2 = (111.8 . 0.463)
  126.  
  127. ;;四則演算をテスト
  128. (let ((z1 (make-from-real-imag 100 50))
  129. (z2 (make-from-real-imag 30 20)))
  130. (add-complex z1 z2)
  131. ;;CALL add-complex (111.80...98948 . 0.463...8061) (36.055...63989 . 0.58...5675)
  132. ;;RETN add-complex (147.648230602334 . 0.49394136891958124)
  133. (sub-complex z1 z2)
  134. ;;CALL sub-complex (111.80...98948 . 0.463...8061) (36.055...63989 . 0.58...5675)
  135. ;;RETN sub-complex (76.15773105863909 . 0.4048917862850835)
  136. (mul-complex z1 z2)
  137. ;;CALL mul-complex (111.80...98948 . 0.463...8061) (36.055...63989 . 0.58...5675)
  138. ;;RETN mul-complex (4031.1288741492745 . 1.0516502125483735)
  139. (div-complex z1 z2)
  140. ;;CALL div-complex (111.80...98948 . 0.463...8061) (36.055...63989 . 0.58...5675)
  141. ;;RETN div-complex (3.1008683647302115 . -0.12435499454676141)
  142. )
  143.  
  144. ;;確認
  145. (real-part (make-from-mag-ang 147.648230602334 0.49394136891958124))
  146. ;;129.99999999999997
  147.  
  148. (imag-part (make-from-mag-ang 147.648230602334 0.49394136891958124))
  149. ;;70.0
Add Comment
Please, Sign In to add comment