我有复数的数据表示形式,但是我不知道如何将两个复数相乘。也许有人可以帮我吗?
(define (complex-num a b)
(cons a b))
(define (real x)
(car x))
(define (imag x)
(cdr x))
答案 0 :(得分:1)
(define (complex-mult a b)
(make-rectangular (- (* (real-part a) (real-part b)) (* (imag-part a) (imag-part b)))
(+ (*(real-part a) (imag-part b)) (*(real-part b) (imag-part a)))))
e,g:
]=> (complex-mult 2+3i 5+2i)
;Value: 4+19i
]=> (complex-mult -i +i)
;Value: 1
答案 1 :(得分:1)
让我们尝试将两个复数相乘
(a + bi) * (c + di) = (a + bi) * c + (a + bi) * di
= ac + bci + adi + bdii
= ac + bci + adi - bd (here we use that i*i = -1)
= ac-bd + (bc+ad)i
如果我们将z1=a+bi
和z2=c+di
放进去,我们可以将其翻译为Scheme:
(define (multiply z1 z2)
(let ([a (real z1)]
[b (imag z1)]
[c (real z2)]
[d (imag z2)])
(complex-num ..compute ac-bd.. ..compute bc+ad.. )))