読者です 読者をやめる 読者になる 読者になる

牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

2.4 Multiple Representations for Abstract DataをPythonでやってみた

SICP Python

SICP2.4 Multiple Representations for Abstract Data2.4.1 Representations for Complex Numbers あたりをPythonでやってみた

scheme(original code)

;2.4  Multiple Representations for Abstract Data
;2.4.1  Representations for Complex Numbers
(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))))

(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))))

;test
(add-complex (cons 1 2) (cons 3 4))
(sub-complex (cons 1 2) (cons 3 4))
(mul-complex (cons 1 2) (cons 3 4))
(div-complex (cons 1 2) (cons 3 4))

結果

(4 . 6)
(-2 . -2)
(-5.000025231294909 . 10.000050462589819)
(0.4399981409890373 . 0.07999966199800675)

続き

(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))

;test
(add-complex (cons 1 2) (cons 3 4))
(sub-complex (cons 1 2) (cons 3 4))
(mul-complex (cons 1 2) (cons 3 4))
(div-complex (cons 1 2) (cons 3 4))

結果

(2.7392084611350116 . -2.6215653753294097)
(3.535092846339735 . 1.118551898866421)
(3 . 6)
(1/3 . -2)
python
#2.4  Multiple Representations for Abstract Data
#2.4.1  Representations for Complex Numbers
from __future__ import division
import math

def add_complex(z1,z2):
    return make_from_real_imag(real_part(z1) + real_part(z2), \
                                   imag_part(z1) + imag_part(z2))
def sub_complex(z1, z2):
    return make_from_real_imag(real_part(z1) - real_part(z2), \
                                   imag_part(z1) - imag_part(z2))
def mul_complex(z1, z2):
    return make_from_mag_ang(magnitude(z1) * magnitude(z2), \
                                 angle(z1) + angle(z2))
def div_complex(z1, z2):
    return make_from_mag_ang(magnitude(z1) / magnitude(z2), \
                                 angle(z1) - angle(z2))

def real_part(z):
    return z[0]
def imag_part(z):
    return z[1]
def magnitude(z):
    return math.sqrt(real_part(z) ** 2 + imag_part(z) ** 2)
def angle(z):
    return math.atan2(imag_part(z), real_part(z))
def make_from_real_imag(x, y):
    return [x, y]
def make_from_mag_ang(r,a):
    return [r * math.cos(a), r * math.sin(a)]

#test
print add_complex([1,2],[3,4])
print sub_complex([1,2],[3,4])
print mul_complex([1,2],[3,4])
print div_complex([1,2],[3,4])

結果

[4, 6]
[-2, -2]
[-5.0, 10.0]
[0.44, 0.079999999999999988]


続き

def real_part(z):
    return magnitude(z) * math.cos(angle(z))
def imag_part(z):
    return magnitude(z) * math.sin(angle(z))
def magnitude(z):
    return z[0]
def angle(z):
    return z[1]
def make_from_real_imag(x, y):
    return [math.sqrt(x ** 2 + y ** 2), math.atan2(y, x)]
def make_from_mag_ang(r, a):
    return [r, a]

#test
print add_complex([1,2],[3,4])
print sub_complex([1,2],[3,4])
print mul_complex([1,2],[3,4])
print div_complex([1,2],[3,4])

結果

[2.7391821737002355, -2.6215653753294097]
[3.5350927879311533, 1.1185518988664209]
[3, 6]
[0.33333333333333331, -2]